How to create 2 or more Websites on the same Ubuntu server

NC

This article explains how to create two or more websites on one server. In this example I use the Ubuntu operating system.

Install Apache and PHP

First, I update and upgrade the ubuntu server operating system to avoid errors arising during the installation process.

sudo apt update && sudo apt upgrade -y

Next, I installed apache, a widely used web server software.

sudo apt install apache2 -y

ESince Wordpress is made by PHP, next I installed php.

sudo apt install php libapache2-mod-php php-mysql -y

To check if apache has been installed successfully, open any web browser and enter the ip address of the ubuntu server.

How to create 2 or more Websites on the same Ubuntu server

Database: Mysql Server

Next, you install mysql server to create databases for websites.

sudo apt install mysql-server -y

Before creating the databases for your website, you should set up the security for the database.

sudo mysql_secure_installation

Press "y", then press "0".

You will be asked to create an admin password for mysql, write down this password to be able to create a database for your website. Next, you choose Yes for the next options.

Next you use the password you just created to access mysql.

sudo mysql  -u root -p

After successful login, you create the database for the first website. For the first site I created a database named "wpdb1". 

CREATE DATABASE wpdb1 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Next I create a username "wpuser1" with password "password1".

CREATE USER 'wpuser1'@'localhost' IDENTIFIED BY 'password1';

Then I will grant wpuser1 account permission to wpdb1 database.

GRANT ALL ON wpdb1.* TO 'wpuser1'@'localhost';

So I have created the database for the first website, you need to write down this information for use in the next steps.

Next step we will create the directory to store the first website.

Wordpress installation

I will create a folder called site1 and host the first site there.

cd /var/www/
sudo mkdir site1
cd site1/

You use the wget command to download the latest version of wordpress. Then extract the downloaded file and copy the files to the current directory.

sudo wget http://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo mv wordpress/* .
sudo chown -R www-data: .

Don't forget to grant permission to upload photos or post to the first website. So I have created the first website hosting directory.

Next, you need to configure apache to know where your first site is.

Apache configuration

You copy the default apache configuration file and name it "site1.conf".

cd /etc/apache2/sites-available/
sudo cp 000-default.conf site1.conf

How to create 2 or more Websites on the same Ubuntu server

Here I have already created two domains for the two websites in this example. For the first site I use the domain "routerbestsite1.rb". We need to declare the domain name in site1.conf config file.

How to create 2 or more Websites on the same Ubuntu server

Also, you need to specify where to host the first website we created in the previous step. Don't forget to save your changes (Ctrl + X) when you close the configuration file.

Next you enable the newly created site and disable apache's default site.

sudo a2ensite site1.conf
sudo a2dissite 000-default.conf

Then you restart apache for the changes to take effect.

sudo systemctl reload apache2

Before using the website on the internet I will test it on the local network so I will edit the hosts file in the Windows operating system. You open notepad with admin rights and add a line that includes the ip address of the ubuntu server and the domain name of the first website.

10.11.32.78 routerbestsite1.rb

How to create 2 or more Websites on the same Ubuntu server

Wordpress Configuration

Let's try to configure the site first. Open a web browser and enter the domain name of the first website. 

Next, you use the information about the database created in the previous step to configure WordPress. These include the database name username and password.

How to create 2 or more Websites on the same Ubuntu server

You will be asked to create an admin account for the site first, then, fill in the required information to complete the WordPress installation process.

So the first website is done, the next steps will create the second website on the same server.

Create the Second Website

Basically the same steps as how to create the first website. With the second site you create another database. Here I will create a new database named wpdb2. And I will also create another username and password for this database. Username is wpuser2 and password is password2.

Just like the first site I will create a folder named site2 to host the second site. You can copy the downloaded Wordpress archive or download a new one. Here I will download new Wordpress and then unzip. Then copy the obtained files to the newly created site2 folder. After granting permission you are done creating a second website storage folder.

To let apache know you have a second site, copy the config file and name it site2.conf. Just like the first website you edit the domain name corresponding to your second website. 

Then you tell apache where you host the second site. Don't forget to save your changes when you close the configuration file. You then enable the site2.conf configuration file and restart apache for the changes to take effect.

Next, edit the hosts file on the Windows operating system so that you can access the second website for configuration. You open a web browser and enter the domain name of the second website and configure it.

Here you use the database information for the second site to configure. You note the database of the two sites is different. You need to write down information about the database to avoid confusion. Just like when configuring the first website you are also required to create an admin account.

As you can see, I have two sites on the same ubuntu server. The next steps will make these sites accessible over the internet.

Port Forwarding

On your router need to forward two ports 80 and 443. Port 80 for http protocol and port 443 for https protocol. In this example my ubuntu ip address is 10.11.32.78. You can forward ports on most routers.

How to create 2 or more Websites on the same Ubuntu server

How to create 2 or more Websites on the same Ubuntu server

After successful port forwarding, edit the hosts file on your computer so that the two websites can be accessed from outside the internet.

How to create 2 or more Websites on the same Ubuntu server

Here I use Freenom so I manually updated my public ip addresses. So you can access these two sites from anywhere.

Next, we will encrypt the communication between the user and the web server using let's encrypt.

HTTPS

Configure the domain names to enable https protocol.

sudo apt install python3-certbot-apache -y

You substitute the respective domain names that work for you. 

sudo certbot --apache -d routerbestsite1.rb -d www.routerbestsite1.rb

With the first configuration, you will be asked to enter your email address. Then press "A", press "N", press "2". If you receive the follow message, you have succeeded.

How to create 2 or more Websites on the same Ubuntu server

You can verify the results by refreshing your first web page. You will see a lock icon next to it. You do the same with the second site.

So I finished creating two websites on the same server. With this way you can add multiple websites on the same server.

Facebook: https://www.facebook.com/routerbest

Twitter: https://twitter.com/routerbestcom

Tags: Ubuntu website Apache WordPress