Ubuntu 14.04 Web Server

If you are developing websites or web applications one of the first things you are going to want to do is set up a development environment. This post will serve as a howto for setting up an Apache web server on Ubuntu 14.04.

We will be setting up the web server to run several websites so that you can easily separate your projects, and run multiple sites on the same system. This is known as a virtual host

Step 1 – Installing Apache

Installing Ubuntu 14.04 server on a vm is fairly uncomplicated so I won’t be covering that here. If you haven’t already installed Ubuntu for your vm, you can download the iso from the Ubuntu website.

Once you have your server installed either on old hardware or on a VM run the following commands to install Apache.

sudo apt-get update
sudo apt-get install apache2

Step 2 – Document root

Your web host most likely provides you with a “public_html” directory to store your website files in for public viewing. This is known as the document root folder. We are going to place our document root directory at /var/www/mysite/public_html

If you have just installed Apache run the following command to see what is already contained in /var/www

ls -la /var/www

The output of that command should be something similar to this:

drwxr-xr-x  2 root root 4096 Jul 19 22:11 html

We are going to create another directory in this location to place our website

sudo mkdir -p /var/www/mysite/public_html

In the example -p tells the terminal to create any needed parent directories, which in this case mysite is the parent directory of public_html.

Using your favorite ftp file transfer program upload your website to the webserver we have been working on. I like to use filezilla.

Make sure you upload the site to your /var/www/mysite/public_html directory.

Create as many directories as you need starting with /var/www as the root for all the websites you plan to work on.

For example you may have a /var/www/example/public_html and a /var/www/superawesomesite/public_html

All of these websites can be hosted from the same server.

Step 3 – Permissions

The first thing we need to do after the directories have been created is to change ownership of the public directory.

sudo chown -R $USER:$USER /var/www/mysite/public_html

In the above example the $USER variable represents the currently logged in user. This means that you can operate as a standard user to edit all the files in the public_html folder.

Secondly we need to modify the permissions of the /var/www directory to allow read, write, and execute permissions for the owner, read and execute for the file group, and execute only for everyone else.

sudo chmod -R 755 /var/www

I’ll cover Linux file permissions in another blog post. However, in this case as stated above running this command means that only the file owner will be able to change the files and directories that reside within /var/www.

Step 4 – Configure the host file

Apache allows us to host multiple websites on the same server using virtual hosts. In this way we do not need a single server for each website we want to host. We will achieve this by creating a host file for the website use.

sudo nano /etc/apache2/sites-available/mysite.conf

Within the text editor enter the following:

<VirtualHost *:80>
      ServerAdmin      webmaster@mysite.local
      ServerName       mysite.local
      ServerAlias         www.mysite.local
      DocumentRoot  /var/www/mysite/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

This is a basic configuration file for your virtual host. It tells the Apache webserver what site it is hosting, some general error log information and the email address of the domain administrator.

Repeat this process for each website you are hosting or you can simply copy the above file name it according to the site you are creating and update the information in the file to match your new site.

Step 5 – Enable the website

Now we need to enable our website. Apache provides a number of tools to work with websites that are hosted on an Apache server.

a2ensite will allow us to enable a website on an Apache server. While, a2dissite will disable that same website.

In our case we need to enable the website.

sudo a2ensite mysite.conf
sudo service apache2 restart

Conclusion

At this point you are done with the server configuration. If you were working from a local machine with a domain that you do not own you will have to update your hosts file. On a Windows system this file is located at C:\Windows\System32\drivers\etc. You will need to open the file as an administrator. On a Mac this file is located at /etc/hosts.

Copy the following to the end of the file, replacing the ipaddress with the ipaddress of your web server. (If you don’t know the ip address of your server first run (ifconfig) from the command line.

192.168.0.2 mysite.local

You should now be able to visit “www.mysite.local” and see the website that you uploaded in step 2. If you created multiple sites don’t forget to add a hosts entry for each on your local computer.

One thought on “Ubuntu 14.04 Web Server

Leave a Reply