Nginx provides a significant performance boost compared to Apache II and other servers, which is why many VPS-based websites are transitioning to Nginx for web hosting. If you’re curious about how Nginx compares to Apache, you can check out our detailed NGINX vs. Apache comparison.
In this guide, we’ll walk you through setting up an Nginx server for a WordPress website on a Debian operating system. While the steps provided are specific to Debian, they can also be adapted for other Unix-based systems with slight adjustments, such as using different package managers or configuration file paths. The overall configuration process remains similar across these systems.
Installation
The first step is to install the necessary packages. On Debian-based systems like Ubuntu, you can run the following command:
apt install nginx php-fpm openssh-server mariadb-server mariadb-client wget nano sudo php-mysql php-curl php-zip php-gd php-intl php-xml
This command will install the Nginx server, PHP FastCGI Process Manager (PHP-FPM) module, OpenSSH server for SSH access, MariaDB for database management, essential PHP modules for WordPress, and the wget
command to download WordPress.
Configuring the PHP-FPM Module
Next, we’ll configure the PHP-FPM module. By default, PHP-FPM listens on port 127.0.0.1:9000
. However, for enhanced security, it’s recommended to use a Unix socket instead of a port.
To make this change, edit the www.conf
file located in the /etc/php/8.2/fpm/pool.d
directory. The file’s location may vary depending on your operating system. Use the following configuration to set up PHP-FPM to listen on a Unix socket:
; Comment out the default listening value (port 127.0.0.1:9000)
; listen = 127.0.0.1:9000
; Set the listening value to use a Unix socket instead
listen = /run/php/php-fpm.sock
; Ensure the 'listen.owner' and 'listen.group' match the Nginx user
listen.owner = www-data
listen.group = www-data
; Set the read/write permissions for the Unix socket file
listen.mode = 0660
Configuring Nginx to Work with PHP-FPM
Now, we’ll configure Nginx to work with PHP-FPM. Create a new file in the /etc/nginx/conf.d
directory.
# This file will be use to configure nginx server
nano /etc/nginx/conf.d/example.conf
Use the following configuration to integrate Nginx with the PHP-FPM module:
# It is recommended to use an upstream server to handle PHP requests
upstream php_fpm {
# Specify the path to the PHP-FPM Unix socket
server unix:/run/php/php-fpm.sock;
}
server {
# Listen on port 80 for IPv4 connections
listen 80;
# Listen on port 80 for IPv6 connections
listen [::]:80;
# Define your domain name
server_name diversifyindia.in;
# Set the root directory for your WordPress site
root /var/www/html/wordpress;
# Specify the index files to be used
index index.php index.html;
# Process all PHP files using PHP-FPM
location ~ \.php$ {
fastcgi_pass php_fpm;
include fastcgi.conf;
# Set the timeout for FastCGI requests (should match 'max_execution_time' in php.ini)
fastcgi_read_timeout 30;
}
# Use try_files to handle requests and execute PHP files if needed
location / {
try_files $uri $uri/ /index.php?$args;
}
}
For more details on configuring caching for WordPress on Nginx, check out our comprehensive guide on setting up WordPress cache plugins with the Nginx server.
Creating a Database for WordPress
The next step is to set up a database for your WordPress website. You can use either MySQL or MariaDB for this purpose.
# Start the MariaDB server and run the secure installation script
# Answer 'yes' or 'no' to the prompts during setup
systemctl start mariadb && mysql_secure_installation
# Log in to the MySQL shell as the root user
mysql -u root -p
Now, you’ll need to create a dedicated database for your WordPress installation. It’s also crucial to create a new database user with the appropriate privileges to manage this database. For security reasons, avoid using the root user for managing the WordPress database.
-- Create a database for the WordPress installation
CREATE DATABASE wordpress;
-- Create a new database user with limited privileges
CREATE USER 'wp'@'localhost' IDENTIFIED BY 'myPassword';
-- Grant all privileges on the WordPress database to the new user
GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wp'@'localhost';
-- Reload the privilege tables to apply changes
FLUSH PRIVILEGES;
-- Exit the MySQL shell
exit;
Downloading and Installing WordPress
Now, download the latest version of WordPress from WordPress.org using the wget
command.
wget https://wordpress.org/latest.tar.gz
Once the download is complete, navigate to the server’s root directory and extract the latest.tar.gz
file. On Debian systems, change the directory to /var/www/html
and extract the file there.
# Change to the directory where WordPress will be installed
cd /var/www/html
# Extract the WordPress archive file
tar -xzf ~/latest.tar.gz
# Change the owner and group of the WordPress directory to 'www-data'
# This allows the Nginx user to write data to this directory
chown -R www-data:www-data wordpress
Starting the Nginx Server and Setting Up WordPress
Finally, start the Nginx server and proceed with the WordPress setup to complete the Nginx WordPress configuration.
# Start the PHP-FPM server
systemctl start php8.2-fpm
# Start the Nginx server
systemctl start nginx
Visit your domain (e.g., your-domain.com
) in a web browser to continue with the WordPress installation. For guidance, read our article on recommended WordPress settings to set up your website properly. The WordPress NGINX config PHP-FPM setup is now complete, and your site is ready to go live.
Next, you can enable HTTPS for your website. For more information on serving your website over HTTPS, refer to our tutorial on installing an SSL certificate for your website.
Frequently Asked Questions
Which server is better, Nginx or Lighttpd?
Nginx is preferred over Lighttpd because it benefits from active development, with new versions released every couple of weeks that include new features and bug fixes. Additionally, Nginx offers a simple configuration process, and there are numerous tutorials available online to help with server setup.
Why is ‘if’ evil in Nginx?
The if
directive is considered problematic when used in a location
context. The only completely safe use of the if
directive in this context is for return
or rewrite
statements. Outside of these uses, the if
directive may not behave as expected and can lead to unintended results. This is why if
is often deemed “evil” in the location
context.
What is the difference between Nginx and Nginx Plus?
NGINX Plus provides superior support compared to NGINX Open Source. Additionally, NGINX Plus offers exclusive features not available in the open-source version, such as active health checks, session persistence, JWT authentication, and more.
What is the difference between worker connections and worker processes in Nginx?
In Nginx, worker processes handle the actual processing of requests, while the worker_connections
directive determines the maximum number of simultaneous connections that each worker process can handle.
For instance, if you are running a one-worker process with 512 worker connections, you can serve up to 512 clients simultaneously. If you have two worker processes, each with 512 connections, you can handle a total of 2 x 512 = 1024 clients simultaneously.