Detailed guide to deploying two separate WordPress sites on the same Ubuntu server using Apache2, MySQL, and PHP 8.3.


How to Deploy Two Separate WordPress Sites on the Same Ubuntu Server with Apache2, MySQL, and PHP 8.3

This guide will show you how to host two distinct WordPress websites on a single server using Apache2 virtual hosts. Each site will have its own MySQL database, and PHP 8.3 will handle the scripting.

Prerequisites

Before starting, make sure you have:

  • An Ubuntu server with a non-root user with sudo privileges.
  • Domain names or subdomains for each site (e.g., site1.com and site2.com).
  • A basic understanding of Linux commands.

Step 1: Update Your Server

First, ensure your system is up-to-date:

sudo apt update
sudo apt upgrade

Step 2: Install Apache2

Apache2 will serve as the web server for both sites. If Apache2 is not already installed, run:

sudo apt install apache2

Start and enable Apache2:

sudo systemctl start apache2
sudo systemctl enable apache2

Verify that Apache is running:

sudo systemctl status apache2

Step 3: Install MySQL

Install MySQL to manage databases for both WordPress installations:

sudo apt install mysql-server

Secure the MySQL installation:

sudo mysql_secure_installation

Log in to the MySQL shell:

sudo mysql

Create separate databases and users for each WordPress site. For example:

CREATE DATABASE wp_site1;
CREATE USER 'wp_user1'@'localhost' IDENTIFIED BY 'password1';
GRANT ALL PRIVILEGES ON wp_site1.* TO 'wp_user1'@'localhost';
FLUSH PRIVILEGES;

CREATE DATABASE wp_site2;
CREATE USER 'wp_user2'@'localhost' IDENTIFIED BY 'password2';
GRANT ALL PRIVILEGES ON wp_site2.* TO 'wp_user2'@'localhost';
FLUSH PRIVILEGES;

EXIT;

Now you have two databases (wp_site1 and wp_site2) and two users (wp_user1 and wp_user2) for your WordPress installations.

Step 4: Install PHP 8.3

Add the PHP 8.3 repository and install it with the required modules:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.3 libapache2-mod-php8.3 php8.3-mysql php8.3-xml php8.3-gd php8.3-curl php8.3-mbstring php8.3-zip php8.3-intl

Verify the installation:

php -v

You should see PHP 8.3.x.

Step 5: Configure Apache for Multiple Sites (Virtual Hosts)

Each WordPress site will have its own Apache virtual host configuration.

Create Directories for Each Site

First, create separate directories for each WordPress site in /var/www/:

sudo mkdir -p /var/www/site1.com
sudo mkdir -p /var/www/site2.com

Assign ownership of these directories to the Apache user:

sudo chown -R www-data:www-data /var/www/site1.com
sudo chown -R www-data:www-data /var/www/site2.com
sudo chmod -R 755 /var/www/site1.com
sudo chmod -R 755 /var/www/site2.com

Create Virtual Host Files

Now, create separate virtual host configuration files for each site.

For site1.com:

sudo nano /etc/apache2/sites-available/site1.com.conf

Add the following configuration (replace site1.com with your actual domain):

<VirtualHost *:80>
    ServerAdmin admin@site1.com
    ServerName site1.com
    ServerAlias www.site1.com
    DocumentRoot /var/www/site1.com

    <Directory /var/www/site1.com>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site1.com_error.log
    CustomLog ${APACHE_LOG_DIR}/site1.com_access.log combined
</VirtualHost>

For site2.com:

sudo nano /etc/apache2/sites-available/site2.com.conf

Add the following configuration (replace site2.com with your actual domain):

<VirtualHost *:80>
    ServerAdmin admin@site2.com
    ServerName site2.com
    ServerAlias www.site2.com
    DocumentRoot /var/www/site2.com

    <Directory /var/www/site2.com>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site2.com_error.log
    CustomLog ${APACHE_LOG_DIR}/site2.com_access.log combined
</VirtualHost>

Enable Virtual Hosts

Enable both virtual hosts:

sudo a2ensite site1.com.conf
sudo a2ensite site2.com.conf

Disable the default Apache site configuration:

sudo a2dissite 000-default.conf

Reload Apache to apply the changes:

sudo systemctl reload apache2

Step 6: Download and Configure WordPress

Now, download WordPress and configure it for both sites.

Download WordPress

Download the WordPress package and extract it to each site’s root directory:

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz

Copy WordPress to site1:

sudo cp -r wordpress/* /var/www/site1.com/

Copy WordPress to site2:

sudo cp -r wordpress/* /var/www/site2.com/

Assign proper permissions to both sites:

sudo chown -R www-data:www-data /var/www/site1.com
sudo chown -R www-data:www-data /var/www/site2.com
sudo chmod -R 755 /var/www/site1.com
sudo chmod -R 755 /var/www/site2.com

Configure WordPress Databases

For site1.com, create the configuration file:

cd /var/www/site1.com
sudo cp wp-config-sample.php wp-config.php

Edit the configuration file to match the wp_site1 database settings:

sudo nano wp-config.php

Modify these lines:

define( 'DB_NAME', 'wp_site1' );
define( 'DB_USER', 'wp_user1' );
define( 'DB_PASSWORD', 'password1' );
define( 'DB_HOST', 'localhost' );

Repeat the process for site2.com:

cd /var/www/site2.com
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

Modify it to match the wp_site2 database settings:

define( 'DB_NAME', 'wp_site2' );
define( 'DB_USER', 'wp_user2' );
define( 'DB_PASSWORD', 'password2' );
define( 'DB_HOST', 'localhost' );

Step 7: Enable Apache2 Rewrite Module

To enable pretty permalinks for both sites, activate the rewrite module:

sudo a2enmod rewrite
sudo systemctl restart apache2

Ensure the AllowOverride All directive is set in both virtual host files (already done in Step 5).

Step 8: Complete WordPress Setup in the Browser

Now, navigate to each site in your web browser to complete the WordPress installation.

  • For site1.com, go to http://site1.com.
  • For site2.com, go to http://site2.com.

Fill out the setup forms with the necessary information (site title, admin user, password, etc.) for each WordPress instance.

Step 9: Optional – Use HTTPS (SSL)

It’s a good idea to secure your sites with HTTPS. You can use Let’s Encrypt to get free SSL certificates for both domains. First, install Certbot:

sudo apt install certbot python3-certbot-apache

Then, run Certbot for each site:

sudo certbot --apache -d site1.com -d www.site1.com
sudo certbot --apache -d site2.com -d www.site2.com

Certbot will automatically configure your Apache files to use SSL. After that, reload Apache:

sudo systemctl reload apache2

Conclusion

You have now successfully deployed two separate WordPress websites on a single Ubuntu server using Apache2, MySQL, and PHP 8.3. Each site is fully isolated with its own virtual host, database, and WordPress installation.

Edvaldo Guimrães Filho Avatar

Published by

Categories: ,

Leave a comment