Add a second site to Scaleway Wordpress image

Scenario: You have a web site set up with the default Scaleway Wordpress image. You don't care about the performance and speed that much but you want to save money and decide to add a second maybe even third WordPress site on the same server instance.

This tutorial will quickly explain how would you go about setting this up.

I fully tested this with a completely fresh Wordpress image on Scaleway. But you can set this up with any other cloud provider (AWS, Bluehost, Google Cloud, ...). If you are hacking this on a server with a production website make sure you create backups and be extra careful.

We will need to do three things:

  • Change the folder structure of our /var/www folder
  • Add another database and user
  • Update Nginx to route the traffic to the correct site.

To start, set up your ssh key and connect to your server with

ssh root@IP.OF.YOUR.SERVER  

Change the folder structure

Current Wordpress php, HTML, js, css files reside in /var/www along with index.php

What you want to do is create two separate folders inside /var/www the new folder structure will look like this.

   |-www
   |---wordpress
   |---newsite

wordpress and newsite will each contain a separate Wordpress installation. Let's make a copy of current Wordpress site from /www to www-copy

root@wordpress:~# rsync -r /var/www/ /var/www-copy  

And then let's clear the www directory and rsync back to individual site directories.

root@wordpress:~# rm -r /var/www/*  
root@wordpress:~# rsync -r /var/www-copy/ /var/www/wordpress  
root@wordpress:~# rsync -r /var/www-copy/ /var/www/newsite  

We must also give user www-data proper permissions on the upload folder in order for image upload to work.

root@wordpress:~# mkdir /var/www/wordpress/wp-content/upload  
root@wordpress:~# sudo chown -R www-data:www-data /var/www/wordpress/wp-content/upload

root@wordpress:~# mkdir /var/www/newsite/wp-content/upload  
root@wordpress:~# sudo chown -R www-data:www-data /var/www/newsite/wp-content/upload

Add new database and user

You can connect to local MySQL database with the mysql command.

root@wordpress:~# mysql  

Then we need to do five things.

  • Create a new user: CREATE USER newsiteuser@localhost;
  • Set password for user: SET PASSWORD FOR newsiteuser@localhost= PASSWORD("newsitepassword");
  • Create a new database: CREATE DATABASE newsitedatabase;
  • Grant privileges: GRANT ALL PRIVILEGES ON newsitedatabase.* TO newsiteuser@localhost IDENTIFIED BY 'newsitepassword';
  • Refresh mysql priviliges: FLUSH PRIVILEGES;

After all this MySQL hacking we can exit mysql console and update our new site config with new data.

root@wordpress:~# vim /var/www/newsite/wp-config.php  

Update DB_NAME , DB_USER and DB_PASSWORD with new data.

/** The name of the database for WordPress */
define('DB_NAME', 'newsitedatabase');  
/** MySQL database username */
define('DB_USER', 'newsiteuser');  
/** MySQL database password */
define('DB_PASSWORD', 'newsitepassword');  

Update nginx config

First, we will want to update the existing config file root to point to /var/www/wordpress - our new path. While we are editing the file we should also add the server_name (http://nginx.org/en/docs/http/server_names.html) property. For my set up I'm using two subdomains wordpress.example.com and newsite.example.com. In my DNS settings I set up both subdomains with A record pointing the same public instance IP. Nginx will then based on server_name property route the traffic to appropriate WordPress installation.

root@wordpress:~# vim /etc/nginx/sites-available/000-default.conf  

Change root /var/www; to root /var/www/wordpress; and add new line server_name wordpress.example.com; Also make sure to remove default_server from listen property.

Your config should look similar to this. You should adapt to your own needs.

Let's also create a new config for our new site, by copying the existing one and make all the necessary updates.

root@wordpress:~# cp /etc/nginx/sites-available/000-default.conf /etc/nginx/sites-available/newsite.conf  

One thing we must not forget is to make a symbolic link for our new config to sites-enabled folder. With nginx you can have multiple configs in the /sites-available folder, but if you want to activate them you need to create a symbolic to /sites-active folder, we can do this with.

root@wordpress:~# ln -s /etc/nginx/sites-available/newsite.conf /etc/nginx/sites-enabled/newsite.conf  

Edit the file in a similar fassing as 000-default.conf but root pointing to /var/www/newsite

To restart nginx execute

root@wordpress:~# service nginx restart