Multiple WordPress Website on LEMP

LEMP

1. Install LEMP

2. Create a New Nginx Server Block:

Create a new Nginx server block for each WordPress website you want to host. You can do this by creating a new configuration file in the /etc/nginx/sites-available/ directory. For example, to create a new server block for a website called example.com, you can create a file called example.com.conf in the /etc/nginx/sites-available/ directory.

nano /etc/nginx/sites-available/example.com.conf

3. Configure Nginx Server Block:

In the server block configuration file, you need to specify the root directory for your WordPress installation and the domain name of your website. Here is an example configuration file:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;

index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}

# Block PHP files in uploads directory
location ~* /wp-content/uploads/.*\.php$ {
deny all;
}

# Block access to hidden files and directories
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}

Note: if you are using a different version of PHP then replace the 7.4 with the version you are using. eg. 8.0 and do not forget to replace example.com with your domain.

4. Create Database

Create a New MySQL Database: Create a new MySQL database for each WordPress website you want to host. You can do this using the MySQL command line interface

mysql -u root

or

mysql -u root -p
CREATE DATABASE db_databasename;
GRANT ALL PRIVILEGES ON db_databasename.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
exit;

Note: Replace the databasename and wordpressuser as per your choice. we will use the database name and wordpressuser later in the section of wordpress configuration.

5. Download WordPress & Install

Download and Install WordPress: Download and install WordPress in the root directory of your website. You can do this using the WordPress command line interface.

wget https://wordpress.org/latest.zip

Create WordPress folder for your website.

sudo mkdir -p /var/www/example.com

copy latest.zip to /var/www/example.com

cp latest.zip /var/www/example.com/latest.zip

unzip with

cd /var/www/example.com
unzip latest.zip

change Directory to your newly created folder </var/www/example.com>

cd /var/www/example.com

here you will see a folder named wordpress

change directory again to wordpress

cd wordpress

copy wp-config-sample.php to wp-config.php

sudo cp wp-config-sample.php wp-config.php

Now edit the new config file with a command-line text editor like Nano.

sudo nano wp-config.php

Find the following lines and replace the red texts with the database name, username and password you created in the previous step.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

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

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

Then scroll down to find the following line.

$table_prefix = 'wp_';

By default, every WordPress database table name begins with wp_ as the prefix. It’s highly recommended to change it to something else to improve security. Use random characters like below.

$table_prefix = 'DBexAg_';

Save and close the file.

We also need to set the Nginx user (www-data) as the owner of the WordPress site directory by using the following command.

sudo chown www-data:www-data /var/www/example.com/ -R

Enable Nginx Server Block: Once you have created the Nginx server block configuration file and installed WordPress, you need to enable the server block by creating a symbolic link to the file in the /etc/nginx/sites-enabled/ directory. You can do this using the ln command:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

To Test nginx configuration

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

you probably need to install some PHP7.4 extensions. if you are using php7.4.

sudo apt install php-imagick php7.4-fpm php7.4-mbstring php7.4-bcmath php7.4-xml php7.4-mysql php7.4-common php7.4-gd php7.4-json php7.4-cli php7.4-curl php7.4-zip

Then reload PHP-FPM and Nginx. The wizard should now be displayed.

sudo systemctl reload php7.4-fpm nginx

6. Enabling HTTPS

To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 20.04 server.

sudo apt install certbot python3-certbot-nginx

And run this command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d example.com,www.example.com

The certificate should now be obtained and automatically installed.

7. Finish the Installation with the Setup Wizard

Enter your domain name in the browser address bar.

example.com

or

example.com/wp-admin/install.php

You shall see the WordPress installation wizard. Select a language.

Choose Language

Create an admin account and click the Install WordPress button.

And now your new WordPress site is installed.

Repeat steps 2-7 for each WordPress website you want to host on your server.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Liked this post? Share with others!

Do you want to boost your business today?

This is your chance to invite visitors to contact you. Tell them you’ll be happy to answer all their questions as soon as possible.