Hosting multiple WordPress websites on a single Nginx server requires careful configuration to ensure proper isolation and performance. In this guide, we’ll walk through the process of configuring Nginx to host multiple WordPress sites on a single server using server blocks.
Step 1: Prepare WordPress Directories
Create separate directories for each WordPress site you want to host.
sudo mkdir -p /var/www/wordpress_site1
sudo mkdir -p /var/www/wordpress_site2
Step 2: Download and Extract WordPress
Download the WordPress files for each site and extract them into their respective directories.
cd /var/www/wordpress_site1
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
Repeat the above steps for the second WordPress site,
cd /var/www/wordpress_site2
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
Step 3: Configure Database for Each WordPress Site
To install MariaDB on Ubuntu, you can use the following command:
sudo apt-get install mariadb-server
This command installs the MariaDB server on your Ubuntu system. If you prefer to install MySQL instead, you can use the following command:
sudo apt-get install mysql-server
After the installation is complete, you can start, stop, and check the status of the MariaDB or MySQL service using the following commands:
- Start MariaDB/MySQL service:
sudo systemctl start mariadb
- Start MySQL service:
sudo systemctl start mysql
Create the databases
Create a separate MySQL or MariaDB database and user for each WordPress site.
You can achieve this by logging into MYSQL/MariaDB:
mysql -u root -p
Now, let’s create the Database with the associated user for the site1 by pasting the following commands one by one:
CREATE DATABASE wordpress_site1_db;
CREATE USER 'wordpress_site1_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress_site1_db.* TO 'wordpress_site1_user'@'localhost';
FLUSH PRIVILEGES;
Repeat the above steps to create the database and its associated user for site2
CREATE DATABASE wordpress_site2_db;
CREATE USER 'wordpress_site2_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress_site1_db.* TO 'wordpress_site1_user'@'localhost';
FLUSH PRIVILEGES;
Step 4: Configure Nginx Server Blocks
To install Nginx on Ubuntu, you can use the following command:
sudo apt-get install nginx
This command will install the Nginx web server on your Ubuntu system. After the installation is complete, you can start, stop, and check the status of the Nginx service using the following commands:
- Start Nginx service:
sudo systemctl start nginx
- Check the status of Nginx service:
sudo systemctl status nginx
Install and configure PHP for Nginx
WordPress is built on PHP and so we need to also install the required php modules using the following command:
sudo apt-get install php php-fpm php-mysql
The above command has installed PHP, the PHP FastCGI Process Manager, and the MySQL extension for PHP.
Let’s configure PHP to work with the Nginx server by editing the php-fpm configuration file, which you can access using this command:
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Update the settings in the file as seen below:
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
Such settings mean that PHP has been configured to run under the same user and group as Nginx while listening on the correct socket.
We can now save the changes to the file and then restart PHP-FPM to apply the changes:
sudo systemctl restart php8.1-fpm
Create the Nginx Server configuration files
You will need to create server block configuration files for each WordPress site. Let’s create for the first site, wordpress_site1
sudo nano /etc/nginx/sites-available/wordpress_site1
Paste the following code snippet
server {
listen 80;
server_name wordpress_site1.com www.wordpress_site1.com;
root /var/www/wordpress_site1;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
error_page 404 /index.php;
location ~ /\.ht {
deny all;
}
}
Create a similar configuration file for the second WordPress site (wordpress_site2
).
sudo nano /etc/nginx/sites-available/wordpress_site2
Also, paste the following code snippet
server {
listen 80;
server_name wordpress_site2.com www.wordpress_site2.com;
root /var/www/wordpress_site2;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
error_page 404 /index.php;
location ~ /\.ht {
deny all;
}
}
Step 5: Enable Server Blocks
Create symbolic links to enable the server blocks.
sudo ln -s /etc/nginx/sites-available/wordpress_site1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/wordpress_site2 /etc/nginx/sites-enabled/
Step 6: Test Configuration and Reload Nginx
Test the Nginx configuration for syntax errors.
sudo nginx -t
If the test is successful, reload Nginx to apply the changes.
sudo systemctl reload nginx
Step 7: Complete WordPress Installation
Access each WordPress site in a web browser to complete the installation. Follow the on-screen instructions, providing the database details created earlier.
Conclusion
Configuring Nginx to host multiple WordPress websites involves setting up individual server blocks for each site. By following these steps, you can efficiently manage and host multiple WordPress sites on a single Nginx server, ensuring proper isolation and optimal performance for each site.