ID-10021811

Most if not all of us are familiar with WAMP or XAMP or some similar package for creating a server environment on your local machine.  I personally prefer WAMP and it is perfect for working with PHP without having to do it online.  This is very handy when working with WordPress or other CMS systems, as they require a server environment to work in.  However, WAMP has many shortcomings and in no way does it replace a real web server.  So what if you need more than what WAMP has to offer?

 Installing a LAMP stack.

1. Ubuntu

Firstly, get Ubuntu Server here. Once you have installed it and it is up and running, open terminal and type in the following command to change to the root user:

sudo -i

You will be asked to authenticate – do so.

2. Update the Repository Cache

Next we have to ensure that the repository information is up to date.

apt-get update

3. Install Apache, PHP, and MySQL

Install tasksel

tasksel is a tool for selecting tasks for installation on Ubuntu and it makes it easy to install the packages we will need for this specific server environment.  It is important to note that you should NEVER use tasksel to remove tasks.  Removing tasks with tasksel may (read: probably will) remove core packages and will cause issues on your server.

apt-get install tasksel

Install the LAMP stack.

Now that you have installed tasksel, you can install the LAMP stack in one command:

tasksel install lamp-server

During the installation, you will be required to set a password for the MySQL root user via this dialog box:

Ubuntu-mysql-server-configuration

Enter a strong and secure password for the MySQL ‘root’ user.

Once you have reached this point, you have everything you need to server content from your server.  Apache2 is installed and configured to serve web pages from the /var/www/ directory.  You can upload your content to this location and Apache will serve the pages.

But this only allows you to serve/host one website.  What if you want more?

3. Configuring Name-based Virtual Hosts

Name-based virtual hosts allow Apache to host multiple websites / domains from the same IP address – which is how any shared-hosting server works. (IP-based virtual hosts require each site to have it’s own IP address)  Name-based virtual hosts will allow Apache to serve the correct website / content based on the domain name requested.

Before we start, we first need to set up a new user and a directory on /home from where our content will be served.  In this example, we will use the following:

mywebsite as the username

mywebsite.domain.co.za as the domain name

Through the rest of this guide, replace the username/domain name with the appropriate values for your own setup.

Add new user

Add the new user:

useradd mywebsite

Create and chown the user’s directory

We will now make the /home and /public_html directories for this user.  We will also create a directory for the new site’s access log and error log.

mkdir /home/mywebsite
mkdir /home/mywebsite/public_html
mkdir /home/mywebsite/logs
chown mywebsite.mywebsite /home/mywebsite
chown mywebsite.www-data /home/mywebsite/public_html
chown mywebsite.mywebsite /home/mywebsite/logs
chmod 711 /home/mywebsite/
chmod 750 /home/mywebsite/public_html/
chmod 750 /home/mywebsite/logs

Virtual Hosts file

It is now time to create the virtual hosts file, which will tell Apache to server requests for mywebsite.domain.co.za from the folder we just created.  Apache works by storing a list of website virtual hosts in /etc/apache2/sites-available/. Each website gets its own file for its virtual host which makes it easy to configure each domain seperately.

We will create the virtual host file using Nano:

nano /etc/apache2/sites-available/mywebsite.domain.co.za

Now that the file is created, add these directives to the file:

<VirtualHost *:80>
ServerAdmin webmaster@domain.co.za
ServerName mywebsite.domain.co.za
ServerAlias www.mywebsite.domain.co.za
DocumentRoot /home/mywebsite/public_html/
ErrorLog /home/mywebsite/logs/error.log
CustomLog /home/mywebsite/logs/access.log combined

Enable VirtualHost

Apache can enable/disable websites as needed.   Once you have created your virtual host for your website/domain, you need to tell Apache to bring it online by enabling it:

a2ensite mywebsite.domain.co.za

With this command, you are creating a symlink (symbolic link) in /etc/apache2/sites-enabled/ to your virtual hosts file for mywebsite.domain.co.za in /etc/apache2/sites-available/ which effectively puts the site online.

Now you have to reload Apache for the changes to take effect:

service apache2 reload

If you have properly (via your hosting company) pointed the DNS for your domain to your own server, you should now be able to visit the site in a browser and have it served from your own server.