The LAMA Stack
Run Dynamic Web Sites or Servers Using Linux-Apache-MySQL-ASP.NET
October 30, 2009
CoverStory
LANGUAGES:C#
ASP.NETVERSIONS: 2.0 +
The LAMA Stack
Run Dynamic Web Sites or Servers Using Linux-Apache-MySQL-ASP.NET
By Oscar Peli
The Mono Project released a new version (2.0) of Mono lastfall. Mono is an open source project that implements a .NET Framework portingon platforms like Linux, Solaris, and MacOS. With this version, the projectoffers many opportunities to .NET developers. For example, it is now possibleto develop an ASP.NET 2.0+ application on your preferred IDE and run it onLinux or MacOS, simply with a copy of files (assemblies included).
In this article we ll develop a data-driven applicationbased on a MySQL database. You ll analyze a MySQL provider and understand howto deploy an application to an openSUSE server. At the end of the article we lllook through some advanced features regarding LINQ (language-integrated query)that also is supported by Mono.
The LAMP Stack
If you are a Microsoft-based developer, accustomed to workingwith Windows, Internet Information Server, and SQL Server, you may not befamiliar with the LAMP stack. A quick search on Wikipedia will show that theacronym LAMP refers to a solution stack of software, usually free and opensource software, used to run dynamic Web sites or servers. The originalexpansion is as follows:
Linux, referring to the operatingsystem;
Apache, the Web server;
MySQL, the database management system(or database server);
PHP or others, i.e. Perl, Python, theprogramming languages.
Linux-Apache-MySQL-PHP (or Perl or Python). The goal ofthis article is to build a new version of the stack where the language ( P ) issubstituted by ASP.NET (Mono). So we are going to build the new LAMA stack:Linux-Apache-MySQL-ASP.NET.
To get started, you must understand how to manage all thecomponents of the stack that (probably) are new for you (at least the firstthree).
Linux-Apache
Space constraints and the scope of this article limit mefrom being able to provide a thorough discussion about Linux and Apache. Forthis article we ll provide the bare essentials for you to run your applicationunder the new LAMA stack. If you already own a Linux-based server, you candownload the latest Mono version from http://www.go-mono.com/mono-downloads/download.html.(Version 2.0.1 was available at the time of this writing, but it is possiblethat by the time you read this a newer version will be available.) Browsing thelink, you ll find versions of Mono for several supported Linux flavors:openSUSE, RedHat, and Solaris; but if you are absolutely new to Linux, you havethe better choice in an openSUSE VMware image that contains the latest Monoversion pre-installed and pre-configured, and all the other components (Apache,MySQL) you ll need for your application to run. To execute the VMware image,download the free VMware player from http://www.vmware.com/products/player/.You also can use this image with any VMware product compatible with VMwareWorkstation. Read carefully the instructions for using the VMware image (http://mono-project.com/VMware_Image).
Once you ve downloaded and unzipped the image and theplayer, you can run your first Linux machine by simply double-clicking themono.vmx file. The image is pre-configured, so you ll log in as the linux user (with the password mono ).
The next step you must perform is the configuration of theApache Web server. As with Internet Information Server, you also must configurethe virtual directory of your application on the Apache Web server. To do so,add some lines to the httpd.conf file that you ll find under the /etc/apache2directory. You need administrator rights to modify this system file, so you mustlog in as the root user (with the password mono ). Once logged-in as root ,open a File Browser and go to the /etc/apache2 folder; find the httpd.conf fileand edit it with the pre-installed gedit editor (right-click on the file and select Open with gedit ); finally, add these lines:
AddMonoApplications default"/lama:/srv/www/htdocs/LAMA"
SetHandler mono
In this way, you define your virtual directory lama thatpoints to the /srv/www/htdocs/LAMA folder. You also tell Apache that all thefiles inside this virtual directory will be handled by Mono. Save the file andrestart Apache running the following command inside a shell (to open a shellclick on Gnome Terminal from the Computer main menu):
/etc/init.d/apache2 graceful
Now you can find a nice database with which to work.
MySQL
Already installed and pre-configured in your VMware imageis a MySQL database (version 5.0.51a). To run a data-driven application, we canuse the world sample database that you can download starting from the MySQLdocumentation page (http://dev.mysql.com/doc/).Once you ve extracted the world.sql file, you must load it into MySQL; open acommand shell (use the root Linux user), then change to the directory whereyou stored the world.sql file. From this location type:
mysql -u root -p
This command starts the MySQL monitor, a command line toolto interact with MySQL. You connect MySQL as the root user, with all the rightsto manage the server. When prompted for the root password, simply leave itblank and press Enter (remember that the MySQL root user is different from theopenSUSE root user). You ll see something similar to Figure 1 if you ve done thatall correctly.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.0.51a SUSE MySQL RPM
Type 'help;' or 'h' for help. Type 'c' to clear
the buffer.
mysql>
Figure 1: The MySQLmonitor.
Type help if you need to know which commands are availablefrom the MySQL monitor. To create the world database and to use it as a defaultdatabase, type the following commands:
mysql> CREATE DATABASE world;
Query OK, 1 row affected (0.00 sec)
mysql> USE world;
Database changed
Finally, to load data into the new database, type:
mysql> SOURCE world.sql;
You ll see some output; at the end, type exit to returnto the command shell.
As usual in Linux, you can manage servers via command linetools. Of course, using a GUI tool is more suitable; present in your VMwareimage is a PHP application that lets you manage MySQL via a Web interface; fromyour host machine, simply type the following address inside Linux:http://localhost/phpMyAdmin or http://yourLinuxaddr/phpMyAdmin. Figure 2 showsthe application started from the host while the Linux server took the192.168.1.13 address.
Figure 2: A GUI tool to administerMySQL.
You can use phpMyAdmin to check which kind of data youloaded from the world.sql file, and you can create a new user we ll use toaccess the world database. Simply use the Privileges link to add the user lama with password lama . You can start to develop your data-driven application onceyou ve set up your data environment.
ASP.NET
First we must look for a suitable provider for MySQL. Startingfrom the MySQL page on the Mono project site (http://mono-project.com/MySQL), youcan find an advised provider to download: MySQL Connector/Net (http://dev.mysql.com/downloads/connector/net/).The downloaded zip file contains a Windows msi file that installs a lot ofstuff, like documentation, samples, etc. (see Figure 3).
Figure 3: MySQL Connector Netinstallation.
We can build the sample application once the MySQLprovider is installed (you ll know what the installation did as you continue throughthe article).