Introduction
WordPress is a popular Content Management System (CMS) which is used to publish website over internet. It has grown into a powerhouse that runs over 40% of all websites on the internet. WordPress covers you whether you’re setting up a personal blog, creating an online store, or launching a corporate site.
- Nginx (pronounced “engine-x”) is a powerful, open-source web server and reverse proxy server. Created by Igor Sysoev and first publicly released in 2004, Nginx has quickly become one of the most popular web servers on the internet, powering millions of websites.
- PHP: Developers use PHP, an open-source programming language, for web improvement and to integrate within HTML. It executes on the server side, meaning it runs on the web server, not in the end-user’s web browser.
- MySQL: MySQL is an open-source (RDBMS) that uses the (SQL). Developers use it for web applications because of its quality, speed, and convenience.
This guide will walk you through installing WordPress on Ubuntu 22.04 using the LEMP stack (Linux, Nginx, MySQL, PHP). WordPress is a popular content management system (CMS) that allows you to create and manage websites easily.
Ensure you meet a few prerequisites before installing WordPress with Nginx on Ubuntu 22.04. These prerequisites ensure a smooth and successful installation process. Here’s what you’ll need:
- Ensure you have a server running Ubuntu 22.04 with a non-root user with sudo privileges.
- You’ll need a registered domain name that points to your server. This can be set up through your domain registrar.
- Ensure your server’s firewall allows HTTP and HTTPS traffic. You can manage this using UFW (Uncomplicated Firewall).
Steps to Check and Set Up Prerequisites
1. Update Your System
sudo apt update && sudo apt upgrade -y
2. Create a Non-Root User
sudo adduser yourusername
sudo usermod -aG sudo yourusername
3. Set Up Your Domain Name
Ensure your domain name points to your server’s IP address. This can typically be managed through your domain registrar’s DNS settings.
Step-by-Step Process to install WordPress, PhP, MySQL and Nginx
Step 1: Update Your System
First, update your package list and upgrade existing packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Nginx
Nginx packages are available in the default Ubuntu repository. You can use the below command to install them:
sudo apt install nginx
Once the installation is complete, the Nginx service will start automatically. To know the status of the service, use the below command:
sudo systemctl status nginx
Configure UFW (Optional)
VPS firewall allows us to open ports 80 and 443 for HTTP and HTTPS respectively. You can enable the Nginx full profile which contains rules for both ports. This can be done using:
sudo ufw allow 'Nginx Full'
sudo ufw enable
Install and Configure MySQL Database
To store data we will be using MySQL. In case you do not have MySQL installed, then you can get it by using:
sudo apt install -y mysql-server
After installing MySQL, let’s make sure it’s running. Enter the below in the command line.
systemctl status mysql
Creating the database and a database user
We first need to start MySQL:
$ mysql -u root -p
Here you can create a database and a database user with the names WordPress and WordPressUser respectively.
mysql> create database wordpress_db;
And a database user:
mysql> CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'P@ssw0rd445';
Make sure that you replace the password with something a bit more secure! We then need to grant privileges to the user on the database:
mysql> GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
mysql>FLUSH PRIVILEGES;
mysql>exit;
Before we install PHP, let’s run a test on MySQL. Typemysql -u wordpress -p Password445(-u is for calling user and -p is for calling password)
Step 4: Install PHP
WordPress is written in PHP, so we need to install PHP. We will also install many packages at once this time. Specifically, we’re going to install PHP FPM with all possible packages or extensions.
sudo apt update && sudo apt install -y software-properties-common
sudo apt install php-fpm php-mysql mysql-server nginx unzip
root@vps2964888:~# php -v
PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
Apart from installing php8.3.6, WordPress requires several PHP modules to extensions to communicate with MySQL correctly. WordPress and many of its plugins leverage additional PHP extensions. They are:
- MySQL for connecting to the MySQL database.
- cURL for making remote requests.
- Mbstring to handle multibyte strings.
- ImageMagick to perform actions such as image resizing.
- XML to provide XML support.
- Zip to unzip plugins, themes, and WordPress update packages.
- Finally, type following command in the terminal to check the status. PHP is active and running.
sudo systemctl status php8.3-fpm.service
when you finished installing the extensions, restart PHP-FPM process so that the running PHP processor can refresh newly installed features.
sudo systemctl restart php8.3-fpm
Step 5: Configure Nginx for WordPress
We need to tell NGINX to send all PHP requests to PHP FPM for processing. To do this, update the default NGINX configuration.
Type the command cd /etc/nginx/sites-availablein the terminal.
Before making any changes, let’s make a backup. So it’s easy to return to the initial file if there is a mistake. Type in the terminal.
cd /etc/nginx/sites-available/
sudo cp default.baksudo rm default
sudo nano a2zeducate
We can then paste in the following configuration to the newly created empty file:
server {
listen 8080;
server_name a2zeducate.com www.a2zeducate.com;
index index.php index.html index.htm;
root /var/www/html/a2zeducate.com/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# php7.4-fpm.pid php7.4-fpm.sock php-fpm.sock
}
location ~ /\.ht {
deny all;
}
}
Now we will make a symlink in Linux is made simple using the ln command with the -s option to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/a2zeducate /etc/nginx/sites-enabled/
Restarting Nginx
We need to restart Nginx for the new configuration to take effect
sudo systemctl restart nginx.service
Removing the default index.html page
cd /var/www/html
sudo rm index.nginx-debian.html
Download and Install WordPress with Nginx
While still in the /var/www/html folder, let’s download WordPress from wordpress.org:
cd /var/www/html/Â
wget https://wordpress.org/latest.tar.gz
Next, you can extract this archive to the directory created earlier. This can be done using:
tar xf latest.tar.gz
Now, let’s copy the entire contents of the directory into our document root. We’re using the -a
flag to make sure our permissions are maintained,
sudo cp -a /var/www/html/wordpress/* /var/www/html/a2zeducate.com/
let’s copy over the sample configuration file to the filename that WordPress actually reads:
cd  /var/www/html/a2zeducate.com/
sudo cp wp-config-sample.php wp-config.php
Now, We ’ll assign ownership to the www-data user and group. This is the user and group that Nginx runs as, and Nginx will need to be able to read and write WordPress files in order to serve the website and perform automatic updates:
sudo chown -R www-data:www-data /var/www/html/a2zeducate.com
Files are now in the server’s document root and have the correct ownership, but you still need to complete some additional configuration.
6- Setting up WordPress Configuration file
Let’s Now open the WordPress configuration file:
sudo nano /var/www/html/a2zeducate.com/wp-config.php
we will modify some of the database connection settings at the beginning of the file. You’ll have to adjust the database name, the database user, and the associated password that was configured within MySQL.
...
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpressuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
...
define('FS_METHOD', 'direct');
7- Completing the installation through web interface
Now that the server configuration is complete, you can finish up the installation through WordPress’ web interface.
In your web browser, navigate to your server’s domain name or public IP address:
http://server_domain_or_IP/wordpress
After installation of the wordpress sites, I noticed in System Health section in wordpress, some modules were missing. wordpress admin panel Tool > Site Health
perform the following tasks in terminal then to remove the critical error related to website health.
apt-get install php8.3-imagick
apt-get install php8.3-dom
apt-get install php8.3-curl
apt-get install php8.3-mbstring
apt-get install php8.3-zip
apt-get install php8.3-gd
apt-get install php8.3-intl
Additionally, we have to do some extra configurations in php.ini to set the parameters values which are mostly required by themes and plugins . for that got nano /etc/php/8.3/fpm/php.ini
memory_limit = 512M
max_execution_time = 40000
max_input_time = 6000
upload_max_filesize = 40M
post_max_size = 128M
Configure Automatically Log Off Idle RDP user Sessions in Windows