Host Multiple Sites on LEMP Server in Linode


Linode provides unmanaged VPS hosting. Linode 1GB (1024) 2GB (2048) costs 10USD per month. You may host unlimited websites in a Linode. Here we will describe the steps involved to host many websites in a single Linode 2048.

1. Create an Account with Linode

Goto Linode.com and create an account. you need to provide your credit card details.

2. Add a Linode

Select a plan and add a linode as follows:

add-linode

From your Dashboard copy the IP of your Linode and note down some place else.

3. Install OS e.g. Debian 8.1

The process is called Rebuild. Rebuild you Linode with Debian 8.1.

debian-rebuild

Choose a Password for Root.

4. Boot Linode

Click the Boot button and your linode will start.

linode-boot

you will see your linode booting

linode-booting

5. Run Putty in Windows

Run Putty with the IP address you copied in step 2.

run-putty

A security alert will pop up. Click Yes.

Login with your root password, you already set in step 3.
You will see a command prompt in black screen.

6. Set a Hostname

Set a hostname for your linode. hostname is anything you like to name your VPS server. e.g. “supercom”.
Execute the following command:

hostnamectl set-hostname supercom

Check whether the hostname has been set properly. Run:

hostname

You will get supercom as output.

Again Run the following to set hostname in everywhere:

nano /etc/hosts

Now change the text debian to supercomp.

etc-hosts

Save and Exit by Pressing Ctrl+X and y.

7. Set the Timezone

Run the following command to setup your local time.

dpkg-reconfigure tzdata

To check it shows proper date and time run:

date

It will show you current date and time.

8. Install software Updates

Run the following to update your system softwares:

apt-get update
apt-get upgrade

During upgrade you need to press y to continue.

9. Create User

adduser admin

add-admin

usermod -a -G sudo admin

Now logout of root account. Run putty again and login as admin.

10. Secure Linode with SSH Key Pair

Open puTTYgen in your own Windows PC. Generate a Public/Private key pair.

puttygen
Save Private key in your PC.
Copy Public key.

Now run the following commands with admin user in your linode terminal.

mkdir .ssh
sudo nano .ssh/authorized_keys

Paste the copied public key into this file.
Save and Exit.

sudo chown -R admin:admin .ssh
sudo chmod 700 .ssh
sudo chmod 600 .ssh/authorized_keys
sudo nano /etc/ssh/sshd_config

Now disable root login by changing the following value:

PasswordAuthentication no
PermitRootLogin no

Restart SSH

sudo service ssh restart

Now you can not login with root account. Also you can not login with password. admin user can only login with the private key. Open the private key you saved and you can login with admin user.

11. Create a Firewall

Run the following:

sudo nano /etc/iptables.firewall.rules

Paste the following into this file.

*filter

-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A OUTPUT -j ACCEPT

-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

-A INPUT -p icmp --icmp-type echo-request -j ACCEPT

-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT

Activate the rules:-

sudo iptables-restore < /etc/iptables.firewall.rules

Make sure the rules always activate while you restrat linode

sudo nano /etc/network/if-pre-up.d/firewall

Paste the following script.

#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules

Run this:

sudo iptables -L

If everything is OK, you will find the following output.

iptables

12. DDOS Protection

Run the following commands:

sudo apt-get install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local

Set “enabled” to “true” in the [ssh-ddos] section. Then restart Fail2ban.

ssh-ddos

sudo service fail2ban restart

13. Install Nginx Web server

Run the following command:

sudo apt-get update
sudo apt-get install nginx

After successful instal open your browser and type the IP address. You will see the following:

nginx

To configure Nginx according to your hardware specification, run:

sudo nano /etc/nginx/nginx.conf

Change worker_processes value to 1. To further optimize nginx configuration is out of the scope of this guide. It will be posted some other day.

To prepare Nginx to run PHP, you need to edit default values. Run

sudo nano /etc/nginx/sites-available/default

And uncomment by removing # from the following segment

location ~ \.php$ {
include snippets/fastcgi-php.conf
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

Restart Nginx Server by typing the following:

sudo service nginx restart

14. Install MySQL

Run the following commands one by one.

sudo apt-get install mysql-server
sudo mysql_install_db
sudo mysql_secure_installation

You will be prompted a series of questions after you enter root password. Just type y or n as required.

Change the root password? [y/n]: n
Remove anonymous users? [y/n]: y
Disallow root login remotely? [y/n]: y
Remove test database and access to it? [y/n]: y
Reload privilege tables now? [y/n]: y

Refer: MySQL Commands for Hosting Websites in Linux VPS

15. Install PHP

Run the following command:

sudo apt-get install php5-fpm php5-mysql

Change Configuration

sudo nano /etc/php5/fpm/php.ini

Find cgi.fix_pathinfo inside the configuration file and make it like

cgi.fix_pathinfo=0

You need to remove initial ; from the above line.

Save and Exit. Restart PHP5

sudo service php5-fpm restart

To check whether php successfully installed you need to run:

sudo nano /var/www/html/info.php

Type the following code:

phpinfo

 

Now type the following in your browser:

http://your_server_IP/info.php

Replace your_server_IP with your actual IP.
You will see the following output.
phpinfo
At this point, your server is ready to host as many websites as you like. All you need is to create different nginx virtual hosts for different websites.

16.Host Multiple Websites

In this example we will show how to host 2 websites. For multiple websites you just need to repeat the process as described.
Suppose we want to host example1.com and example2.com

At first create content directories for websites. Run:

sudo mkdir /var/www/html/example1
sudo mkdir /var/www/html/example2

Now create virtual host for example1.com

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example1
sudo nano /etc/nginx/sites-available/example1

This file looks like the following after removing unnecessary comments.

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	root /var/www/html;
	index index.html index.htm index.nginx-debian.html;
	server_name _;
	location / {
		try_files $uri $uri/ =404;
	}
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
	}
}

Now change the above file to host example1.com

server {
	listen 80;
	listen [::]:80;
	root /var/www/html/example1;
	index index.php index.html index.htm;
	server_name example1.com;
	location / {
		try_files $uri $uri/ /index.php?$args;
	}
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
	}
}

Now enable example1.com and restart nginx.

sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled
sudo service nginx restart

If you have already changed your DNS settings for your domain name example1.com as

ns1.linode.com
ns2.linode.com
ns3.linode.com
ns4.linode.com
ns5.linode.com

You should see example1.com in your browser in action. Hurray, you just successfully hosted your first website example1.com.
Again to host example2.com, first copy the vhost file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example2
sudo nano /etc/nginx/sites-available/example2

Now change the above file to host example2.com like this:

server {
	listen 80;
	listen [::]:80;
	root /var/www/html/example2;
	index index.php index.html index.htm;
	server_name example2.com;
	location / {
		try_files $uri $uri/ /index.php?$args;
	}
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
	}
}

Now enable example2.com and restart nginx.

sudo ln -s /etc/nginx/sites-available/example2.com /etc/nginx/sites-enabled
sudo service nginx restart

Similarly, you should see example2.com in your browser in action. So go ahead and repeat the process to host unlimited websites in Linode.

, ,

One response to “Host Multiple Sites on LEMP Server in Linode”

  1. An easier way to host PHP website on Linode is by using Cloudways platform. You can instantly launch Linode server right from their platform. The server will already have OS, stack and PHP installed and configured. This saves a lot of time and efforts.

Leave a Reply

Your email address will not be published.