Setting up a Ubuntu, Apache, MySQL, Mono, ASP.NET, MVC 2 Development Server
Tuesday, 23 February 2010
To save a thousand google searches next time I setup a Mono web development machine I thought it might be a good idea to just write down the steps...
Recently I had the need to setup a Mono development server and I thought I'd document the process as I went.
The end result is a machine running:
- Apache (web server)
- MySQL (database server)
- Samba (file sharing, for direct file access from Windows)
- Mono (open-source equivalent of .NET)
- ASP.NET (Microsoft web framework)
- ASP.NET MVC 2 (Model-View-Controller framework for ASP.NET)
This article assumes installation on a VirtualBox VM, but there's no reason this wouldn't work on a physical machine, or other virtual machine platform. Also, this article assumes Ubuntu 9.10 Server edition.
What You'll Need
You'll need to following for this setup:
- VirtualBox - Download
- Ubuntu 9.10 Server i386 ISO - Download
- ASP.NET MVC 2 - Download
- MySQL .NET Connector - Download
- Microsoft Visual Studio 2008 (may also work with Visual Studio Express)
Setting up the Virtual Machine
Personally, I find VirtualBox the easiest way to run virtual machines, but any virtual machine type software should work. In VirtualBox, I typically create a machine with following spec:
- Operating System - Ubuntu/Linux (32-bit);
- RAM - 512Mb
- Hard Drive - 16Gb
- Network Adaptor - either Bridged or Host Only
You can use NAT for the network adaptor but you'll need to setup port forwarding for HTTP, SSH, Samba and MySQL which is not covered here.
- Create a new virtual machine.
- Load the ISO for Ubuntu 9.10 Server i386 in VirtualBox's storage settings and boot the machine.
- Follow the install, accept all the defaults but when prompted for which services to install select LAMP, Samba and OpenSSH.
You should now have a bootable Ubuntu server virtual machine. Next we'll install the VirtualBox guest additions which improve performance and allow the guest operating system to better integrate with the host.
- Bootup the virtual machine through VirtualBox
- Load the Guest Additions CD by selecting Install Guest Additions from VirtualBox's menu Devices menu.
- Login to the machine and run the following commands:
sudo apt-get update sudo apt-get install dkms sudo mount /dev/cdrom /media/cdrom sudo /media/cdrom/VBoxLinuxAdditions-x86.run
That's it for the basic server machine.
Setting up Mono and ASP.NET
Next we need to setup Mono and mod_mono. mod_mono is the Apache module for hosting ASP.NET/Mono websites.
sudo apt-get install libapache2-mod-mono mono-apache-server2
Setting up a Firewall
I find ufw the easiest way to manage the Linux firewall. We want to enable ports for SSH, HTTP, MySQL and Samba:
sudo ufw enable sudo ufw allow 22 sudo ufw allow 80 sudo ufw allow 3306 sudo ufw allow proto tcp to any port 135 sudo ufw allow proto udp to any port 137 sudo ufw allow proto udp to any port 138 sudo ufw allow proto tcp to any port 139 sudo ufw allow proto tcp to any port 445
Setting up Samba
Samba is the Linux equivalent of Windows file sharing and will allow us to publish files directly from Visual Studio to the server.
Firstly, edit the samba config file:
sudo vi /etc/samba/smb.conf # use nano instead of vi if you prefer
and append the following to the end of the file:
[www] comment = Web Root read only = no path = /var/www browseable = yes guest ok = yes
I find the easiest way to manage permissions to the shared folder is to make yourself a member of the
www-data group and give that group full read/write access to
sudo usermod -a -G www-data <yourusername> sudo chown -R www-data:www-data /var/www sudo smbpasswd <yourusername> sudo /etc/init.d/samba reload
sudo smbd reload if above reload command doesn't work. (eg: ubuntu 10.10)
NB: You may need to logout and back in to for the usermod change to take effect.
Creating a Virtual Host for your Website
Apache allows hosting multiple websites through virtual hosting. In this example, we're setting up a virtual host for
http://myapp.mono. Here's the commands to create a directory for the site and set it up:
sudo mkdir /var/www/myapp sudo chown -R www-data:www-data /var/www/myapp cd /etc/apache2/sites-available sudo cp default myapp sudo vi myapp # Use nano instead of vi if you prefer
Edit the contents of the newly created
myapp file to look like this:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName myapp.mono MonoAutoApplication disabled AddHandler mono .aspx ascx .asax .ashx .config .cs .asmx .axd MonoApplications "/:/var/www/myapp" MonoServerPath default /usr/bin/mod-mono-server2 DocumentRoot /var/www/myapp <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/myapp> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all SetHandler mono DirectoryIndex index.aspx index.html </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined </VirtualHost>
Now we need to enable the site and restart Apache:
sudo a2ensite myapp sudo /etc/init.d/apache2 restart
NB: Errors like "Failed to attach to existing dashboard,and removing dashboard file '/var/run/mod_mono_dashboard_XXGLOBAL_1' failed (Permission denied)." can be ignored, use /etc/init.d/apache2 reload instead of restart.
Creating an ASP.NET MVC Application
Although it's possible to create ASP.NET MVC websites through completely free tools, I find Visual Studio 2008 the easiest way. So, back on your Windows machine:
Install ASP.NET MVC 2.
Start Visual Studio
Create a new project MVC project: File -> New -> Project -> Visual C# -> Web -> ASP.NET MVC 2 Web Application
Press F5 to run the project locally and check it works. (You should see the template web app appear in your default web browser)
Close the browser and the web server (which should be in your task bar somewhere).
Deploying to the Linux Server
Finally, we're ready to deploy something to the server and see it work. Since we're using a made up domain name (http://myapp.mono) we can't rely on internet DNS to resolve it, we need to do some networking config on the Windows machine:
Login to your Linux box and type
ifconfigand note down its IP address.
Edit your hosts file -
C:\Windows\System32\etc\hosts- and append
x.x.x.x myapp.mono, replacing
x.x.x.xwith the noted ip address of the Linux machine. Note: on Vista and Windows 7 you'll need to run notepad as an adminstrator - Click the Windows Start button, type
notepad, right click on the found result and choose Run As Administrator.
Make sure you can access the machine via samba by opening Windows Explorer, clicking in the address bar and entering
\\myapp.mono. You'll be prompted for your username and password - use your login details for the Linux machine, not your Windows login. If you're on a domain, you might need to prefix your username with a backslash to get it to work. Make sure you select to save your creditials.
Startup Visual Studio again (or switch back to it)
In the Solution Explorer, open the References node, right click on System.Web.Mvc, choose Properties and set Copy Local to true (this causes Visual Studio to copy the MVC assembly to the Linux server).
From the Build menu, select Publish MyApp. Enter
\\myapp.mono\www\myappas the target location and click Publish.
Open a web browser and navigate to
http://myapp.monoand you should see the same MVC app template running under Mono on Linux!
Once you've got this up and running, you might like to use VBoxHeadlessTray to get the VM off your desktop and out of the way.
Now that we have a basic web framework up and running the final piece is a database.
I use the same MySQL server running on the virtual machine regardless of whether it's running the Microsoft development server or under Mono. This is not like running a production and development server off the same DB (which I wouldn't recommend), rather it's about having the same site runnable and testable in the two hosting environments. I tend to use the Microsoft development server for general testing and debugging, and publish periodically to the Mono server to test everything works under Mono.
First we need to make MySQL accessible from outside the virtual machine. Edit the MySQL configuration file, scroll down to
bind-address and change it to the IP address of the virtual machine (the same IP address noted down above), or set it to
0.0.0.0 to allow access from any remote destrination. Then restart MySQL.
sudo vi /etc/mysql/my.conf sudo /etc/init.d/mysql restart
Next we need to create a database for the site:
mysql -u root -p mysql> CREATE DATABASE myapp; mysql> GRANT ALL ON myapp.* TO 'myapp'@'%' IDENTIFIED BY 'myapp_password'; mysql> exit
If you like, you can log back in as the myapp user and create some tables, run sql scripts, etc...
mysql -u myapp -p mysql> CREATE TABLE example(name text); mysql> SOURCE MySqlScript.sql mysql> exit
Now, back to the Windows machine:
- Download and install the Windows version of the MySQL .NET Connector. (See links above)
- Open your Visual Studio project and add a reference to MySQL.Data assembly.
- Right click on the reference, select Properties and set the Copy Local property to true. (again, so this assembly is deployed to the Mono machine).
To access the DB from C# code:
System.Data.IDbConnection dbcon; dbcon = new MySql.Data.MySqlClient.MySqlConnection( "Server=myapp.mono;" + "Database=myapp;" + "User ID=myapp;" + "Password=myapp_password;" + "Pooling=false"); dbcon.Open();
You can read more about this on the Mono site.
And that's it! You now have a functioning development environment for running ASP.NET MVC apps under Windows and/or Linux-Mono.
Leave a comment