WordPress is the most used content management system (CMS) on the Internet today. The open-source platform offers rich tools and themes that make website development an easy process for all categories of users. But with a key focus on performance, WordPress websites require a fast-performing web server like Nginx to rank well in search engines and improve user experience.
This article explains how to install WordPress with a LEMP stack on either Ubuntu 20.04 or Debian 11. LEMP is an acronym for Linux, Nginx, MySQL, and PHP.
Prerequisites
- Deploy a new Ubuntu 20.04 or Debian 11 cloud server
- Create a standard user account with sudo privileges
- Install LEMP on the server
1. Create the WordPress Database
WordPress requires a database to store website files and data. Create a new user and password for WordPress to connect.
Login to MySQL.
$ mysql -u root -p
Create a new database.
CREATE DATABASE wordpressdb;
Also, create a new user with a hard-to-guess password.
CREATE USER ‘dbuser’@’localhost’ IDENTIFIED BY ‘Password’;
Now, give the user full read and write privileges to the database.
GRANT ALL PRIVILEGES ON wordpressdb.* TO ‘dbuser’@’localhost’;
Refresh MySQL.
FLUSH PRIVILEGES
Exit the MySQL Shell.
Exit
2. Configure Nginx
Using your favorite editor, create a new Nginx Virtual host configuration file.
$ sudo nano /etc/nginx/sites-available/wordpress
Paste the following code:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# Pass PHP requests to PHP_FPM
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico {
access_log off;
log_not_found off;
expires max;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Deny Access to Hidden Files such as .htaccess
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Disallow PHP files In the WordPress uploads directory
location /wp-content/uploads/ {
location ~ \.php$ {
deny all;
}
}
}
The above configuration file is tuned to serve WordPress files and forward all PHP requests to PHP-FPM
. First, make sure all necessary PHP modules are installed.
Activate the configuration file by creating a link to sites-enabled
.
$ sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
Also, delete the default configuration file to avoid any conflicts.
$ rm /etc/nginx/sites-enabled/default
Next, fine-tune the main Nginx configuration.
$ sudo nano /etc/nginx/nginx.conf
Depending on your server instance, make sure the number of worker processes matches equals the number of cores for your server.
user www-data;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
Turn on Gzip caching for best WordPress performance by uncommenting all options and make sure the block looks similar to the one below:
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Save and close the file.
Validate Nginx configurations.
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
For changes to take effect, restart the server.
$ sudo service nginx restart
3. Install and Configure WordPress
Download the latest WordPress version.
$ wget http://wordpress.org/latest.tar.gz
Extract files from the tar archive.
$ tar -xzvf latest.tar.gz
Now, move the downloaded files to the webroot directory, in this case /var/www/html
.
$ sudo mv wordpress/* /var/www/html
Grant Nginx (running as www-data) full ownership rights to the directory.
$ sudo chown -R www-data:www-data /var/www/html/
In your web browser, navigate to your server's name or IP address to launch the WordPress web installer and complete the installation. Enter the database information created on Step 1 of this article to start the setup process. Finally, run the installation to set up an administrator account for login to the main WordPress backend dashboard.
Congratulations, you have installed WordPress on a LEMP configuration, then secured the server by limiting access to hidden files and uploading PHP scripts to the WordPress uploads directory. Next, you can install free themes and plugins to design your first website on the open-source platform.