DEV Community

Cover image for How to host multiple WordPress websites on a single DigitalOcean droplet using Apache
Zeeshan Haider Shaheen
Zeeshan Haider Shaheen

Posted on

How to host multiple WordPress websites on a single DigitalOcean droplet using Apache

In this tutorial, we will learn how to set up WordPress and how to host multiple WordPress websites on a single DigitalOcean droplet using Apache2.

In this guide, we will be using Ubuntu 20.04.

Before we start

Alt Text

Installing WordPress

Alt Text

Our first step would be to install WordPress, write the following commands in the terminal to install WordPress.

wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
Enter fullscreen mode Exit fullscreen mode

Creating WordPress Database and Users

The next step involves creating a database and users for WordPress by using MySQL for individual sites.

Log into MySQL using the administrator account you configured during the MySQL installation:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Enter the password and you will enter into MySQL prompt.

Creating Databases

Now we will create databases for our individual websites.

CREATE DATABASE  DatabaseOne;
CREATE DATABASE  DatabaseTwo;
Enter fullscreen mode Exit fullscreen mode

Creating Users

We then create Users for our MySQL database.

Note(There is a difference between the MySQL Users and Linux users).

We are creating users for our MySQL database and UserOne has no access to UserTwo details.

CREATE USER  UserOne@localhost;
CREATE USER  UserTwo@localhost;
Enter fullscreen mode Exit fullscreen mode

Setting up Passwords

SET PASSWORD FOR 'UserOne'@'localhost' = 'PasswordOne';

SET PASSWORD FOR 'UserTwo'@'localhost' = 'PasswordTwo';
Enter fullscreen mode Exit fullscreen mode

Giving Privileges

GRANT ALL ON DatabaseOne.* TO 'UserOne'@'localhost';

GRANT ALL ON DatabaseTwo.* TO 'UserTwo'@'localhost';
Enter fullscreen mode Exit fullscreen mode

Refresh MySQL's privilege information to implement the changes

FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

exit MySQL

exit
Enter fullscreen mode Exit fullscreen mode

Site Root Directories Configuration

Now, we will be creating separate directories for each site.

Change the directory to /var/www/

cd /var/www
Enter fullscreen mode Exit fullscreen mode

Create a directory for each site:

sudo mkdir SiteOne

sudo mkdir SiteTwo
Enter fullscreen mode Exit fullscreen mode

Copy the sample configuration before we move the web contents into our folders:

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php
Enter fullscreen mode Exit fullscreen mode

Now, we will copy the WordPress files to our both directories:

There are many ways you can put WordPress files in your Websites folder.

In our case, we will be using Rsync.

udo rsync -avP ~/wordpress/ /var/www/SiteOne/
sudo rsync -avP ~/wordpress/ /var/www/SiteTwo/
Enter fullscreen mode Exit fullscreen mode

It's time to give ownership of directories to Apache

sudo chown www-data:www-data * -R
Enter fullscreen mode Exit fullscreen mode

Now add Linux username to web group:

sudo usermod -a -G www-data your_linux_user_name
Enter fullscreen mode Exit fullscreen mode

(Make sure to use your Linux username at the end of the above command)

WordPress Configuration

We already have copied WordPress files to each of our website directories.
Now, we will configure WordPress.

Change your directories to /var/www

cd /var/www
Enter fullscreen mode Exit fullscreen mode

First website Configuration

Go to your first site:

cd SiteOne
Enter fullscreen mode Exit fullscreen mode

Open WordPress Configuration file:

sudo nano wp-config.php
Enter fullscreen mode Exit fullscreen mode

Find the section that contains the fields below and substitute the database, username, and password for your first site:

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

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

/** MySQL database password */
define('DB_PASSWORD', 'PasswordOne');
Enter fullscreen mode Exit fullscreen mode

Save this file and exit.

Now, we will configure the second file.

Second website Configuration

Go to second site folder:

cd SiteTwo
Enter fullscreen mode Exit fullscreen mode

Open WordPress Configuration file:

sudo nano wp-config.php
Enter fullscreen mode Exit fullscreen mode

Find the section that contains the fields below and substitute the database, username, and password for your first site:

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

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

/** MySQL database password */
define('DB_PASSWORD', 'PasswordTwo');
Enter fullscreen mode Exit fullscreen mode

Save this file and exit.

Apache Virtual Host Configuration

Alt Text

Go to etc/apache2/sites-available

cd /etc/apache2/sites-available
Enter fullscreen mode Exit fullscreen mode

Create a new virtual host configuration file for each website:

cat > SiteOne.conf
cat > SiteTwo.conf
Enter fullscreen mode Exit fullscreen mode

Open first configuration file:

sudo nano SiteOne.conf
Enter fullscreen mode Exit fullscreen mode

Change the info. as you need:

<VirtualHost *:80>
    ServerAdmin admin@SiteOne
    ServerName SiteOne.com
    ServerAlias www.SiteOne.com

    DocumentRoot /var/www/SiteOne
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/SiteOne>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Open second configuration file:

sudo nano SiteTwo.conf
Enter fullscreen mode Exit fullscreen mode

Change the info. as you need:

<VirtualHost *:80>
    ServerAdmin admin@SiteTwo
    ServerName SiteTwo.com
    ServerAlias www.SiteTwo.com

    DocumentRoot /var/www/SiteTwo
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/SiteTwo>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Enabling configuration file

sudo a2ensite SiteOne.conf
sudo a2ensite  SiteTwo.conf
Enter fullscreen mode Exit fullscreen mode

Reloading Apache to save configuration:

sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

Now, you have successfully installed two WordPress sites on a single DigitalOcean Droplet.

If you have not made any mistake, you will see WordPress Welcome pages on both of your domains.

You can access the WordPress admin page by writing yoursitename.com/wp-admin.

Alt Text

I hope this article was very helpful. If you have any questions feel free to ask in the comments.

If you have some suggestions, feel free to share.
Thank you!!

Top comments (0)