Ubuntu 12.04 Web Server

by on May 8, 2012 at 11:28 pm
Ubuntu MySQL php Apache

The Ubuntu Web Stack

In this article, I’m going to be outlining the steps to install and configure a complete web server on a base install of Ubuntu 12.04 LTS server edition.

I personally don’t prefer to install package groups relating to “LAMP” or similar during initial installation because I’m simply too lazy to review each and every package that is included in those groups, and what the dependencies are.  By installing the required packages using apt-get, this allows you to install just the software you’re looking for, without worrying about dependencies.  This software stack is time tested; we’ll install on Ubuntu’s latest release, and test a few popular applications while we’re at it!


A newly installed Ubuntu 12.04 LTS Server Edition with no additional packages.  Need help with this step?  Check here.

Required packages:  apache2, php5, mysql-server, php5-mysql

Optional packages:  To configure this server remotely, you will need to have installed openssh-server.  This can be achieved by the following in terminal:

apt-get install openssh-server

Note:  This and many other guides on this site assuming you are performing operations as root (or other super user) unless otherwise stated.  By default, root login is prohibited on Ubuntu, even in the terminal.  During installation, the administrative account created has sudo permissions.  I suggest sudoing to root; otherwise, feel free to append ‘sudo’ and full path names as necessary.

Step 1:  Installing Apache2

Though often referred to as just ‘apache’ the name of the package we need to install is apache2.  To begin, log into your system’s terminal.  Run the following command:

apt-get install apache2

Type ‘Y’ as appropriate.  If you have a fast network connection, this should take a matter of seconds!

To test, on another machine that has connectivity to yours, type your server’s IP in a web browser, or you can type the following:

wget localhost index.html

Note the created index.html file in your current working directory.  If you don’t have this file, then apache2 might not be installed.

To test this further, try the following:

rm index.html
service apache2 stop
wget localhost index.html

This will have stopped the apache2 service.  Note the error in the output of wget, about the connection being refused.  Also note there is no index.html.  Restart apache2 using:

service apache2 start

That completes the default installation of apache2.

Step 2:  Configuring Apache2

While there are entire books written on configuring Apache, what I hope to outline here is basic default settings and typical production-ready setup.  I won’t be covering how to run Apache2 from a chroot jail or anything fancy like that (which is pointless, IMO) as those steps are available elsewhere are not specific to the task at hand.

Now that apache2 has been installed, you can see the requisite startup script has been placed in /etc/rc2.d/

apache2 startup script

contents of /etc/rc2.d

The number may vary, but what’s important is the S, which stands for start.  This script is a soft link to /etc/init.d/apache2.

The location of the apache2 config file is


Those of you familiar with Red Hat and similar distros will note the httpd.conf file, which has been created but is empty by default.  httpd is the name of the package/service of apache2 on Red Hat and similar distros.

Contained in the same directory as our config file, is a subdirectory named  conf.d.  Any files with the suffix .conf in this directory will be parsed by apache2 during startup.  It’s common practice to put config files in this directory if you’re running multiple sites (or subdomains) on the same server.  Create the following file:


and paste the following into it:

<VirtualHost *:80>
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/mysite.com
ServerName www.mysite.com
ErrorLog /var/log/apache2/error.log

We have now configured our first virtual host named www.mysite.com.  The first line of that file tells apache which IP and port to listen on for this virtual host.  In our case, *:80 means listen on any IP, port 80.  You can put in your server’s IP here if you like, but *:80 is fine.

The second line, ServerAdmin is optional, and so is the last line ErrorLog.  Whatever settings or options aren’t configured inside the <VirtualHost> tags will be inherited from apache2.conf or other config files.

The third line denotes the directory where you server’s web content will reside.  The default location for apache2 is /var/www.  I simply have chosen to create a subdirectory named mysite.com in that directory; another common location is a subdirectory in the /home directory.  This is up to you, and will vary upon your individual needs.

The fourth line is the host header apache2 will listen for, as sent by the end-user’s web browser.  As you may know, web resources can be accessed via IP address of the server, or a domain name as resolved by DNS.  On systems with more than one site, apache2 will read the domain name and present the appropriate content.

Now that you have created your virtual host’s config file, we need to create the directory we referenced, and assign it the appropriate permissions.

After you create the directory, create a simple index.html file inside of it, with whatever content you like.  Then, perform the following:

chown root:www-data /var/www/mysite.com -R
chmod 550 /var/www/mysite.com -R

The user/group www-data is the default user which will run the apache2 process.  I suggest setting permissions this way, only adding write permissions as absolutely necessary by your application.

Next, we want to restart apache, so it will read our new configuration file.  This should proceed without error.

Now, to test that our virtual host is working as intended, on a remote system on which you have administrative access, edit your hosts file, and add the following entry:

youriphere www.mysite.com

Substituting youriphere with…your web server’s IP!  Don’t know where the host file is on your system?  Look here:  wikipedia

Now, on the system with the edited hosts file, type in www.mysite.com.  This should display the contents of index.html that you created!  If not please check the steps outlined above carefully!

Another option to test is to create the entry in /etc/hosts on the webserver itself, and use wget just as we did above, except this time use www.mysite.com/index.html as the target file!

Step 3: Installing php5

If you don’t have apache2 up and running yet, please complete that step first.

To install php5 perform the following:

apt-get install php5

This should only take a few minutes to complete.  The default location for the config file is:


While most settings are appropriate by default, I suggest making the following change:

safe_mode = on

Next, let’s rename index.html to index.php, and replace the contents with the following:

echo \'hello\';

Restart apache2.  Now, visit www.mysite.com again.  The only text on the screen should be “hello”.  If this is the case, you have successfully installed php5!

Step 4: Installing MySQL

Install mysql-server:

apt-get install mysql-server
apt-get install php5-mysql

Set password for the root user.  Make this a strong password.  I recommend a 14+ character passphrase with mixed case, numbers, and special symbols.  Make it something you can remember, or store it in a safe place that’s NOT ON THE WEBSERVER.  Next, restart apache2.

Next, we’re going to setup a new database for our web application, create a new user, and assign that user permissions only access that db.

mysql -p #enter the root password when prompted on the next line


CREATE USER ‘webuser’@'localhost’ IDENTIFIED BY ‘password_here’; #Be sure to replace ‘password_here’ with a strong password.

GRANT ALL PRIVILEGES ON webdb.* to webuser@’localhost’;


This should complete the steps for MySQL.  Next, let’s install WordPress!

Step 5:  Install WordPress

This part is obviously optional, but I’m including it as it demonstrates that everything is working properly.

Download wordpress, or upload it via sftp to your server.

wget http://wordpress.org/latest.tar.gz

Next move to, unzip and untar the archive in our site’s document root.  We’ll need to modify the root of the WordPress installation as well.

tar zxfv latest.tar.gz
chmod 770 /var/www/mysite.com
chown www-data:www-data /var/www/mysite.com

Now, let’s open up our web browser on our desktop, and attempt the installation!

Everything should go smoothly!  I recommend changing permissions on our site’s folder back to 550 after installation is complete.

Further Reading:

Check back at a later date for more articles relating to iptables and other ways to secure your webserver!

in Distros, How-To, Ubuntu

, , , , , , , ,

You can skip to the end and leave a response. Pinging is currently not allowed.

  • Pingback: Installing Ubuntu 12.04 LTS Server Edition()

  • Pingback: CentOS 6 Web Server()

  • kris

    This was really helpful. I would also recommend including the enabling of mod_rewrite in the apache section, especially since it’s required with wordpress customizations and a common requirement in general.

    • http://www.zipref.com Mike

      Thank you for the suggestion! Next time I get around to editing this page, I’ll be sure to do that :)

  • http://www.level2designs.com/member/52705/ vancouver seo canadian web

    Thanks a lot for the blog article.Really looking forward to read more.

  • http://www.merlin-industrial.co.uk/ Bob White

    Thank you. Information I can understand.

  • matthew

    this is very helpful, and exactly what i was looking for. just going to throw the only question i have at the moment out there to see if anyone can answer.

    I started looking into this installation after stumbling upon a site for drupal, i’m wondering how drupal fits into all of this, if anyone is familiar with drupal. Thanks in advance.

    • http://www.zipref.com Mike

      Drupal would be installed in a similar manner as WordPress, in step 5.

  • http://techstat.net/get-the-latest-active-crucial-memory-coupons/ crucial

    Great article, I enjoyed reading it.

  • zachary

    i have a testing home ubuntu webserver that im running in virtualbox. i’ve got everything setup and i was wondering if i could forward a webpage from a free webhosting to my virtual server?

    • http://www.zipref.com Mike

      The short answer is no. Web pages need a static IP, which most ISPs don’t provide to home users. There are some work arounds for a personal site, but certainly not a good idea for a business site.

      • Steven

        You most certainly can use a home webserver to point to a domain. I have done this. Really easy. Register a domain name , lets say abc.com . Now go sign up for a dyndns account or no-ip account, either one works fine they do the same thing. so lets say you register abc.dyndns.info. you configure your external NIC card on your gateway as the public ip , then go into the gateways settings and find where you can set up dyndns. youll want to use the first public ip on the default route (which is your public ip address). dyndns keeps up with your changing public ip address.

        so now you have two domains.


        you want the end user to browse to abc.com

        so go into your dns manager for your domain, select domain forwarding and point it to abc.dyndns.info and select forward with masking. this way it covers up the ugly name and users get redirected to your dyndns address! then all you have to do is tell your gateway to forward the appropriate ports to the external interface using NAT rules and viola! you have a free hosting solution WITHOUT a static ip and hosting services. also you dont have to go in and change the public ip.


  • Clover Sg

    Very clear and informative.
    Should inform users that they should restart the web-server if they want to see any results if they change the configuration files

  • kr

    Very well written and concise tutorial. Thanks.

  • gabriel

    Well, got an error on the first instruction. Searched the internet. Solution: have “sudo” in front. Instead of this:
    apt-get install openssh-server
    you should have this:
    sudo apt-get install openssh-server

    That’s how it worked for me.

    • gabriel

      Sorry, apache that was, not openssh.

    • http://www.zipref.com Mike

      Thank you for the comment. When I am performing a lot of actions with root permissions, I usually switch to the root account. This eliminates the need to type sudo 100 times ;)

  • Chairman

    At installing Php5

    Next, let’s rename index.html to index.php, and replace the contents with the following:

    You said rename index.html to index.php . but mind telling where this index.html was you referering to ?

  • Chairman

    At installing Php5

    Next, let’s rename index.html to index.php, and replace the contents with the following:

    You said rename index.html to index.php . but mind telling where this index.html was you referering to ?

    • http://www.zipref.com Mike

      Sure thing! In my tutorial, you were instructed to create one. Perhaps you missed that part ;)

    • Brian

      Why would we be following this tutorial if we knew how to do that.

  • Omar Yesid Mariño

    You are amazing! Thank you very much!

  • Sunith

    Hi while installing Php5 how do I rename index.html to index.php and repalce the contents?
    Thank you

    • http://www.zipref.com Mike

      You can use the command ‘mv’
      mv oldname newname
      To replace the contents, use your favorite text editor. I recommend vi.

  • dfdfdd

    hi, thanks for the useful tutorial!
    However regarding the php

  • dfdfddsimon

    In your php code:

    ‘echo’ should be ‘Echo’ else will get no output

    • http://www.zipref.com Mike

      I don’t believe your statement is correct.

  • Brian

    You leave out all of the most important details.

  • danny

    Great stuff.

    One comment though, when I run the php5 index.php code, nothing shows up on my website. Have any idea what it could be?
    I am running on 12.04 Ubuntu and have downloaded the php5 package.


    • http://www.zipref.com Mike

      Did you restart apache after installing php? Also, make sure you are using your web browser to point to http://localhost/index.php

      • danny

        yes got it working thank you

  • James

    My query comes from after the server has been set up. If I want to access it from a different computer, say from work or elsewhere, how do I go about doing this? I have it set up on a virtual machine, and can connect to the server using the computer the virtual machine is set up on, but as soon as I jump to any other computer I am unable to open the webpage?

    • http://www.zipref.com Mike

      That all depends on the networking you have in place for your virtual machine and your local network.

  • Demetrics

    I have one issue i can not see my index.html file it just says index of / Name, last modified, size and description. The code does not work…. I also can not found safe mode.

    • http://www.zipref.com Mike

      I’m sorry this does not work for you. The walk through is pretty thorough, if you followed all of the steps, you should be seeing that file. Are there any steps you had trouble with before that step?