How To Setup WordPress Multisite on Ubuntu 22.04|20.04

Posted on 147 views

Are you a WordPress user who want to switch to WordPress Multisite Network setup? – To host more than one domain on the same WordPress instance?. In this guide, I’ll take you through the steps to setup WordPress Multisite Network using Nginx as a web server and secure the setup with Let’s Encrypt SSL certificates on Ubuntu 22.04|20.04 Linux systems.

In a nutshell, this is a LEMP Stack with WordPress installed and secured with Let’s Encrypt. So we will start our installation by setting up LEMP stack on Ubuntu 22.04|20.04 Linux. WordPress Multisite is a popular feature of WordPress which enables Web masters to run multiple websites using the same WordPress installation on the same server.

Step 1: Install MariaDB Database Server

This setup requires a database server. We will install and create a database to be used by WordPress. Run the commands below to install MariaDB database server:

sudo apt update
sudo apt -y install mariadb-server

After installation of the database server you need to secure it by running the command below:

$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 2: Create database and user for WordPress

After the MariaDB server has been installed, proceed to create a database and user for your WordPress website.

But first update authentication plugin:

$ sudo mysql -u root
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root';
FLUSH PRIVILEGES;
QUIT;

Login to your MariaDB database as root user:

$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 59
Server version: 10.3.31-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Then create a database for wordpress:

CREATE DATABASE wp_db;
GRANT ALL PRIVILEGES ON wp_db.* TO  wp_user IDENTIFIED BY "[email protected]";
FLUSH PRIVILEGES;
\q

Once the database is ready, proceed to install nginx web server:

Step 3: Install Nginx Web Server

Next we install Nginx web server on our Ubuntu server:

sudo apt install nginx

Starting nginx service on both methods is from systemd service manager.

sudo systemctl start nginx

Enable service to start on boot using:

sudo systemctl enable nginx

Step 4: Install PHP and PHP-FPM process handler

The next phase is the installation of php and all required modules. Unlike Apache web server, Nginx does not contain native PHP processing. For that, we have to install PHP-FPM (FastCGI Process Manager). On Ubuntu install php and php-fpm using the commands:

sudo apt update
sudo apt install php php-fpm,pear,cgi,common,mbstring,net-socket,gd,xml-util,mysql,bcmath

PHP socket is located on /var/run/php/ directory, i.e 

  • PHP 8.0 Sock – /run/php/php8.0-fpm.sock
  • For PHP 7.4 – /var/run/php/php7.4-fpm.sock
  • For PHP 7.3 – /var/run/php/php7.3-fpm.sock
  • For PHP 7.2 – /var/run/php/php7.2-fpm.sock
sudo vim /etc/php/*/fpm/pool.d/www.conf

Step 5: Download and Install WordPress

Download WordPress archive:

wget wordpress.org/latest.tar.gz

Extract the file:

tar xvf latest.tar.gz

Move resulting wordpress folder to website root directory:

sudo mv wordpress /var/www/mywebsite

Configure WordPress Database connection

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

Edit  wp-config.php:

$ sudo vim wp-config.php
define('DB_NAME', 'wp_db');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', '[email protected]');

Change ownership of /var/www/mywebsite to web user:

sudo chown -R www-data:www-data /var/www/mywebsite

Step 6: Configure Nginx for WordPress Multisite Network

Create VirtualHost configuration file for your WordPress:

sudo vim /etc/nginx/conf.d/mywebsite.conf

Copy the following configuration snippet and modify accordingly:

##################################
# WORDPRESS NGINX CONFIGURATIONS
##################################

server 
        listen 80;
        root /var/www/mywebsite;
        server_name mywebsite.com;
        return 301 https://$server_name$request_uri;


server mid

Remember to replace mywebsite.com with your valid domain name,/var/www/mywebsite with your wordpress installation root.

Step 7: Generate Let’s Encryt SSL certificate for the domain

Once you have modified nginx with correct settings, proceed to request for Letsencrypt certificate that we’ll use to secure the main site. Download and install certbot client to use:

sudo apt update
sudo apt install certbot

Open http and https ports on the firewall if ufw is active:

sudo ufw allow http
sudo ufw allow https

Stop nginx:

sudo systemctl stop nginx

Request for certificate

export DOMAIN="mywebsite.com"
export EMAIL="[email protected]"
sudo certbot certonly --standalone -d $DOMAIN --preferred-challenges http --agree-tos -n -m $EMAIL --keep-until-expiring

You should get output like this:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for wp.computingforgeeks.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/mywebsite.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/mywebsite.com/privkey.pem
   Your cert will expire on 2018-09-17. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Confirm that certs we indeed generated:

$ sudo ls -1 /etc/letsencrypt/live/mywebsite.com/
cert.pem
chain.pem
fullchain.pem
privkey.pem
README

Make sure you modify nginx configuration ssl section to point to correct path of Let’s Encrypt private key and certificate.

Validate configuration settings then restart nginx for the changes to be affected:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx

Step 8: Configure WordPress Website from browser

Open http://mywebsite.com to start wordpress installation. You should get initial installation page which looks similar to one below.

wordpress-multisite-install-01 (1)

Provide required information and click on “Install WordPress” button. If all goes well, you should get login page.

wordpress-multisite-install-02 (1)

Click Log in and provide username and password.

wordpress-multisite-install-03 (1)

On Login, you should get to wordpress Admin dashboard.

wordpress-multisite-install-04 (1)

Step 9: Configure WordPress Multisite

For WordPress multisite to work, you need to first enable it on wordpress configuration file:

sudo vim /var/www/mywebsite.com/wp-config.php

Add the following content before the line /* That's all, stop editing! Happy blogging. */

define( 'WP_ALLOW_MULTISITE', true );

Restart Nginx service:

sudo systemctl restart nginx

Relogin to WordPress Admin Page and go to:

Tools > Network Setup

wordpress-multisite-install-05

Choose whether to use Sub-domains or sub-directories to host other sites.

Enabling WordPress Network

Paste the given configuration snippet on /var/www/mywebsite/wp-config.php, just before /* That's all, stop editing! Happy blogging. */

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'mywebsite.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

See below screenshot as example:

wordpress-multisite-install-06

Restart nginx:

sudo systemct restart nginx

Relogin to start using WordPress Network Multisite feature. You should see new Network Admin Menu.

wordpress-multisite-install-07

You can start adding websites to your WordPress Network by navigating to Sites > Add New

wordpress-multisite-install-07

In our next guide, I’ll cover how to Add websites to wordpress Multisite Network Setup. Stay connected to receive our latest article updates.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.