Running Multiple WordPress Sites on Ubuntu 16.04 LTS Server with Nginx, MariaDB and PHP 7.1 Support

Sometimes, running one website on a single server might not be a great way to utilize your system resources… Nowadays, a Linux host with 1 CPU and 4GB RAM can power multiple WordPress websites and do it efficiently with Nginx…

When deciding to run multiple WordPress websites on Ubuntu 16.04 LTS server, the steps below can get you started… With Nginx HTTP server, you can run as many WordPress websites as possible as long as the host system has enough resources with a feature called Server Block

Nginx server block is a feature similar to Apache2 virtualhost. These features allow users and webmasters to host multiple independent websites or blogs on a single host computer with single IP address and separate domain names.

Instead or running individual website on individual server with individual IPs, Nginx server block can be configure to run all your different websites from a single host computer and one IP address…

When you’re ready, follow the steps below to get this working.

Step 1: Prepare Ubuntu 16.04 LTS Server

Ubuntu Servers are easier to use and manage, especially for new users and students. Other Linux server will let you do the same, but I find Ubuntu to be much more user friendly then other Linux distributions.

So, prepare Ubuntu server and run the commands below to update it.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

Step 2: Install Nginx HTTP Web Server

Next, run the commands below to install Nginx HTTP server…

sudo apt-get install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always startup when the server boots..

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Step 3: Install MariaDB Database Server

You’ll need a database server for WordPress… and MariaDB database server is a great place to start. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

After that, run the commands below to secure MariaDB server.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • 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

Restart MariaDB server

sudo systemctl restart mysql.service

Step 4: Install PHP 7.1-FPM and other PHP modules

PHP 7.1 isn’t available on Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Run the commands below to install PHP 7.1 and related modules.

sudo apt install php7.1-fpm php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-ldap php7.1-zip php7.1-curl

After install PHP, run the commands below to open Apache2 default PHP file.

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

Then edit the below lines to look like this and save the file.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100
max_execution_time = 360
cgi.fix_pathinfo = 0
date.timezone = America/Chicago

Step 5: Create WordPress Databases and Dsers

At this point in our setup, all required servers and packages have been installed… it’s now time to create our individual website database and config files…

We’re going to be creating configurations for three WordPress websites.. They are:

http://example.com
http://example.net
http://example.org

We’re going to create three databases for these three websites.. These databases will be called:

examplecomdb
examplenetdb
exampleorgdb

To create the databases, run the commands below to logon to the server.

sudo mysql -u root -p

Then run the commands below to create all three databases… one line at a time…

CREATE DATABASE examplecomdb;
CREATE DATABASE examplenetdb;
CREATE DATABASE exampleorgdb;

Our next task we’ll create three database users, one for each site.. they will be called:

examplecomuser
examplenetuser
exampleorguser

Run the commands below to create the three users and set their passwords

GRANT ALL ON examplecomdb.* TO 'examplecomuser'@'localhost' IDENTIFIED BY 'type_new_password_here';
GRANT ALL ON examplenetdb.* TO 'examplenetuser'@'localhost' IDENTIFIED BY 'typw_new_passwored_here';
GRANT ALL ON exampleorgdb.* TO 'exampleorguser'@'localhost' IDENTIFIED BY 'type_new_password_here';

When you’re done, run the commands below to save your changes and exit.

FLUSH PRIVILEGES;
exit;

Step 6: Create Nginx Server Blocks

Now we’re going to create three server blocks for our three websites.. run the commands below to create each… for each, add th configure code below, replace the domain name and root to their appropriate folders.

sudo nano /etc/nginx/sites-available/example.com
sudo nano /etc/nginx/sites-available/example.net
sudo nano /etc/nginx/sites-available/example.org

For each file, copy and paste the code below… replacing the domain name reference and root folder highlighted.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/example.com;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;        
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Example.net server block looks like this:

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/example.net;
    index  index.php index.html index.htm;
    server_name  example.net www.example.net;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;        
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Do this as many time for each website…

When you’re done, run the commands below to enable the server blocks…

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/

Step 7: Create Document Root Folder for Each Site

In each of the server block or site configuration file, there’s a root location. This is where each website content should be stored.. and should be different for all websites. Now that we’ve defined the root locations for all our site, go and create them below.

sudo mkdir -p /var/www/html/example.com
sudo mkdir -p /var/www/html/example.net
sudo mkdir -p /var/www/html/example.org

Step 8: Download WordPress content and copy it to each of the root location

Now download WordPress content and extract into the root directory for each site. To do that run the commands below

cd /tmp/ && wget http://wordpress.org/latest.tar.gz

Then extract the downloaded file.

tar -xzvf latest.tar.gz

And copy to each root folder for each site.

sudo cp -R wordpress/* /var/www/html/example.com
sudo cp -R wordpress/* /var/www/html/example.net
sudo cp -R wordpress/* /var/www/html/example.org

Step 9: Configure WordPress Database Settings

After that, run the commands below to create WordPress wp-config.php settings for each of the domains…

sudo cp /var/www/html/example.com/wp-config-sample.php /var/www/html/example.com/wp-config.php
sudo cp /var/www/html/example.net/wp-config-sample.php /var/www/html/example.net/wp-config.php
sudo cp /var/www/html/example.org/wp-config-sample.php /var/www/html/example.org/wp-config.php

Then run the commands below to open each of the config file and enter the database connection info.. then save the file.

sudo nano /var/www/html/example.com/wp-config.php
sudo nano /var/www/html/example.net/wp-config.php
sudo nano /var/www/html/example.org/wp-config.php

When each file opens type the database name and password for each site you created above.. and save the file.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'examplecomdb');

/** MySQL database username */
define('DB_USER', 'examplecomuser');

/** MySQL database password */
define('DB_PASSWORD', 'type_password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Do the above for each of the sites you create, making sure the database connection info is correct for each site.

When you’re done, run the commands below to configure the directories permissions for all the sites…

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

After that, restart Nginx web server

sudo nginx -t
sudo systemctl restart nginx

After restarting Nginx with no errors, you should be able to access your sites the domain names.. and see WordPress default setup page.

ex… http://example.com

WordPress default setup page

Follow WordPress wizard until setup is complete for each site.. That’s it!

This is how to set up multiple WordPress websites on Ubuntu 16.04 LTS

Enjoy!

Setup OpenSSH Key Authentication (Passwordless) on Ubuntu 16.04 LTS Server

7 Replies to “Running Multiple WordPress Sites on Ubuntu 16.04 LTS Server with Nginx, MariaDB and PHP 7.1 Support

  1. Great tut loved it!! I want to install django sites along wordpress site. It would be great if you write a tutorial for that!

  2. I followed your tutorial to add another site to my existing Digitalocean Droplet already containing a wordpress site but I got 502
    Bad Gateway error.How can I fix this?

    Nginx Error Log-
    2018/08/13 18:36:03 [crit] 1525#1525: *25 connect() to unix:/var/run/php/php7.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 34.222.44.40, server: hackingtutorials.club, request: “GET / HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.1-fpm.sock:”, host: “hackingtutorials.club”

  3. can someone say how to setup ssl for multiple sites with certbot after following this tutorial ?

  4. There is a minor mistake:

    server {
    listen 80;
    listen [::]:80;
    root /var/www/html/exmaple.net; <– typo instead of exMAple.net should be example.net

    Thanks for the guide

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: