This tutorial will guide you through the simple steps of setting up a LAMP server on Linux, with the ability to add Multiple Virtual Hosts, as well as enabling mod_rewrite in case you want to play around with many popular Content Management Systems out there that either support or require RewriteEngine. I suspect the setup process to be similar on Non Ubuntu (or Debian for that matter) Linux flavors, however once you have LAMP installed on your Linux OS, then following the steps to add multiple virtual hosts should yield the same results as long as you are editing the correct files.
- Open a Terminal and enter the command
sudo tasksel - A command line setup will open and you probably already have * by Print Server (CUPS) and Samba File Server. So use your arrow keys and highlight the LAMP Server and press the Space Bar.
- Now with the * showing for Lamp Server, press Tab Once, and then Press Enter. Now it will show a progress bar with 0%, and may stay there for a while, this is okay because it is just downloading the necessary packages.
- Then it will prompt you for a MySQL root account password. Since we are running this service locally on your computer, hopefully just for development purposes, then you can choose a basic password. I made my password root. Press enter once, and it prompts for the password one more time.

- Now it should install everything it needs, and at the end it brings you back to the terminal.
- To verify that it installed either go to http://localhost OR go to System > Administration > Services, scroll down to the bottom and look for Web Server (apache2).
- Next we want to enable mod_rewrite, because this lovely module gives us pretty URLS (like wordpress.com/archives/145 instead of, wordpress.com/?page=archives&id=145). So enter this command into your terminal window
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load - Now we want to move the default location of the web root. Right now it is in /var/www, which you can keep if you want, but I prefer it to be in my user directory. So enter the command
mkdir /home/marf/public_html/then we want to edit the default site conf file and change “DocumentRoot /var/www” and “<Directory /var/www>” and add “ServerName localhost”. Enter this commandsudo gedit /etc/apache2/sites-available/defaultand add this text mentioned above to look like my picture.
- Now there won’t be anything to display there, so we want to make sure it is working, so in the console enter the command
cp /var/www/index.html /home/marf/public_html/. Finally we want to restart the web server so we can enter the commandsudo /etc/init.d/apache2 restartSome warning messages may flash by sayin “Could not reliably determine servers fully qualified domain name…” that is okay, just ignore it. Give it a few seconds and it should say [ OK ] and a terminal prompt should be back. Now go to http://localhost and hopefully you see the It Works! message.
Now if all you wanted was the simple LAMP stack, then stop there. However virtual hosts can be very useful in separating your projects in development as well as organizing them in a ~/Projects folder. So I recommend you to complete the next section even if you don’t fully appreciate it right now.
Multiple Virtual Hosts
- First make the directory where you want your Virtual Host to be located. I chose /home/marf/Projects/example.
mkdir /home/marf/Projects/example. Then enter the commandgedit /home/marf/Projects/example/index.htmland insert the textExample Page. - Now if you go to http://example it probably won’t work. This is because your request went to your ISP’s DNS servers ( or one you’ve specified in your Router if your an advanced user ). So we need to add the url to our hosts file allowing us to browse to that url and the request is given locally and the apache2 server handles the request. So enter the command
sudo gedit /etc/hostsand I add a line to the top, mine top 2 lines look like127.0.0.1 example
127.0.0.1 localhost
- Next we need to tell apache what to do with an incoming request for the url http://example, enter this command in the terminal
sudo gedit /etc/apache2/sites-available/defaultand insert the following code at the bottom of the file.
<VirtualHost *>
ServerAdmin webmaster@example
DocumentRoot "/home/marf/Projects/example/"
ServerName example
ErrorLog "/var/log/apache2/example-error.log"
CustomLog "/var/log/apache2/example-access.log" common
<directory "/home/marf/Projects/example/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>
It should look like this.
- Now again, enter the command
sudo /etc/init.d/apache2 restartand then browse to http://example and it should show Example Page.
There you have it, a Root public_html folder as well as the ability to add as many extra local domains as you wish.
Enjoy!

(1 votes, average: 4 out of 5)
Excellent walkthrough! Thanks a bunch.
T.
I am having an issue. I am new to ubuntu and just learning how to use it. When i use a gedit command it then asks for my password, but then it will not allow me to enter anything. Do you know what I can do to fix this? Since it wont accept my password it is not allowing me to edit files.
If it has the prompt for password, then you are entering it. However it won’t show a * for every letter you press, you just have to type the password, and press enter.
thank you so much, that worked!
I noticed on Marf’s instructions on how to install LAMP on Ubuntu, in the addition of the example virtual host … At least I needed to add :80 into it, like this … to make it work. Otherwise the instructions worked great. thanks.
Leave Comment