How to Setup LAMP for Multiple Websites on Ubuntu 16.04

In this tutorial I am going to show you how you can easily setup a complete working server running Apache, MySQL and PHP. We will make this server in such a way that it can host multiple websites.

For this tutorial I am assuming the following configurations
Username: john
Server Public IP: 192.168.0.1
Domain 1: example.com
Domain 2: abc.com

Logging into SSH client

If you know how to log in with SSH you can skip this part.

In order to setup any server you need a way to access it. We are going to use OpenSSH utility that is used to make secure SSH connections. Almost all servers have this utility installed by default when you first install it.

Download and install PuTTY, a lightweight and a very popular SSH client.

putty - setup lamp with multiple websites

Enter your public IP address in the Host Name section and click Open

Enter root as username and enter the password your hosting provider may have given you or you setup while installing the server.

If this is the first time you are logging in with root you will be prompted to change the password. Enter old password followed by new password. Note down the new password in a safe place.

The root user is the administrative user in a Unix environment that has very broad privileges. It is normally discouraged to use root account on a regular basis as root user has super powers, it has abilities to actually destroy your system, even by accident. The next step will be to create an alternative user for our day to day activities.

To add a new user we will use the following command:

adduser john

Replace john with your desired username. You will be asked to enter password followed by a few questions. Fill in any information you would like or just hit Enter or Return in any field to skip.

Now we have a functional user account but sometimes we may need to do some administrative tasks. For this we will add this user to the sudo group giving them superuser rights. To do any super task you will be required to add sudo before each command.  As root, run the following command:

usermod -aG sudo john

We may now log out from user account and log back in with john’s account. Command to log out is exit. After exiting, run PuTTY program again and use the same procedure to login with john as you did above with root

LAMP Installation

LAMP is an acronym which represents a Linux Operating System, with the famous Apache web server storing site data on a MySQL database. The dynamic content of the website is processed by PHP. Our Ubuntu 16.04 setup will serve as a Linux Operating System. Now let’s go ahead and get the rest of the letters fired up.

Installation of Apache

sudo apt-get update

sudo apt-get install apache2

It will ask you to confirm. Hit Y and press Enter.

To check if Apache is installed and running. Open your browser and type http://192.168.0.1/ (replace the IP with your server’s IP) and you’ll see the Apache2 Ubuntu default page.

ubuntu default page

Installation of MySQL

sudo apt-get install mysql-server

You will be asked for confirmation. Enter Y and continue.

Next you will be asked to enter root password. And hit enter.

At this point MySQL installation is complete. But in order to secure our installation and protect it from some lock down access to the database system.

mysql_secure_installation

You will be asked to enter your root password for MySQL

Then you will be asked about some controls MySQL has to offer.

Validate Password: n For all others: Y

Now that MySQL has been installed and secured. We will install PHP with it’s apache and mysql modules

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

By default apache looks for index.html files for index files. For WordPress we need index.php file as our index. So we’ll neet to update our configuration.

Open /etc/apache2/modes-enabled/dir.conf file with sudo

sudo nano /etc/apache2/mods-enabled/dir.conf

In DirectoryIndex line: Move index.php to the very beginning.

So the new code block will look like this:

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

We have updated our Apache settings so we’ll need to restart it

sudo systemctl restart apache2

Our LAMP setup is now complete.

Setup Website No 1 example.com

We will setup our first website example.com. We will create MySQL database for this website. Type the following command and press enter

1. MySQL Setup

mysql -u root -p

Enter your MySQL root password.

Type the following commands in the mysql terminal:

CREATE DATABASE DATABASE_NAME;

We will create a new user for this website’s database. It is not a good practice to use MySQL root user for websites.

CREATE USER JOHN@LOCALHOST IDENTIFIED BY ‘very-complex-pass’;

Username: JOHN
Password: very-complex-pass

Now we will give access of the database to our newly created user

GRANT ALL PRIVILEGES ON DATABASE_NAME.* TO JOHN@LOCALHOST;

where DATABASE_NAME is our database name and JOHN is our database user.

The user is given the access. We need to flush the privileges so that MySQL’s current instance knows about this

FLUSH PRIVILEGES;
exit

Our database setup is also complete now.

Next step is to download and install WordPress

2. Installation of WordPress

Let’s go to our home directory

cd ~

To download latest version of WordPress run the following command

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

Extract the files with the following command

tar xzvf latest.tar.gz

This will create a WordPress directory in the home directory

Now we will configure WordPress

cd ~/wordpress

A sample copy of our wp-config.php file is provided by default. We’ll make a copy and do the edits.

cp wp-config-sample.php wp-config.php

nano wp-config.php

Now we will set our database details. Set database name, user, password provided above in the file.

To secure our WordPress installation we will add some secret keys.
Run this command

curl -s https://api.wordpress.org/secret-key/1.1/salt/

You will get a unique set of keys.

Copy the keys and replace them in the wp-config.php file

3. Setup Host File for example.com

Now we will set a domain name example.com in our Apache configuration

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the example.com.conf file and set the following variables

sudo nano /etc/apache2/sites-available/example.com.conf

ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html

To disable the default configuration and enable our new configuration. Run the following commands:

sudo a2dissite 000-default.conf
sudo a2ensite example.com.conf
sudo systemctl restart apache2

Copy WordPress files to our public_html directory

sudo rsync -av wordpress/ /var/www/example.com/public_html/

Now open your browser and enter example.com you will be presented with WordPress installation page. Enter your website details and you are ready to explore the world of WordPress.

Setup Website No 2 abc.com

Follow step 1 and 2 from above example to setup database and WordPress respectively. Just replace example.com with abc.com

The main step is to setup host file for abc.com. For every website you set up on your server you need to define a host file for it.

Host File for abc.com

Follow these steps.

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/abc.com.conf

Open the abc.com.conf file and set the following variables

sudo nano /etc/apache2/sites-available/abc.com.conf

ServerName abc.com
ServerAlias www.abc.com
DocumentRoot /var/www/abc.com/public_html

Enable our new configuration. Run the following commands:

sudo a2ensite abc.com.conf
sudo systemctl restart apache2

Copy website files to your public_html directory for website no 2.

sudo rsync -av ~/wordpress/ /var/www/abc.com/public_html/

Setup your wp-config.php file with database details.

This is how you run multiple websites on a single server. Feel free to comment below if you have any questions.

Where can I get a good quality server?

DigitalOcean is one of the best out there for private servers.

We will be happy to hear your thoughts

Leave a reply