Setting Up an Nginx Reverse Proxy Server with Apache for WordPress on Ubuntu

Step-by-step tutorial on setting up an Nginx reverse proxy server with Apache for WordPress:

Prerequisites

  • Ubuntu Server 20.04 (or a similar Debian-based distribution)
  • Root or sudo access to the server
  • A domain name or a server IP address

Step 1: Install Nginx

  1. Update the package lists:
   sudo apt update
  1. Install Nginx:
   sudo apt install nginx

Step 2: Install Apache and PHP

  1. Install Apache and PHP:
   sudo apt install apache2 libapache2-mod-php
  1. Install PHP and required extensions:
   sudo apt install php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc

Step 3: Install and Configure MySQL

  1. Install MySQL:
   sudo apt install mysql-server
  1. Run the MySQL security script to improve the database security:
   sudo mysql_secure_installation

Step 4: Configure Apache for WordPress

  1. Create a virtual host configuration file for WordPress:
   sudo nano /etc/apache2/sites-available/wordpress.conf
  1. Add the following content to the file:
   <VirtualHost *:80>
       ServerName example.com
       DocumentRoot /var/www/wordpress

       <Directory /var/www/wordpress>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
   </VirtualHost>
  1. Enable the site and restart Apache:
   sudo a2ensite wordpress.conf
   sudo systemctl restart apache2

Step 5: Configure Nginx as a Reverse Proxy

  1. Modify the default Nginx configuration file:
   sudo nano /etc/nginx/sites-available/default
  1. Replace the existing content with the following configuration:
   server {
       listen 80;
       server_name example.com;

       location / {
           proxy_pass http://localhost:8080;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }
   }
  1. Restart Nginx:
   sudo systemctl restart nginx

Step 6: Install and Configure WordPress

  1. Create a directory for WordPress:
   sudo mkdir /var/www/wordpress
  1. Set appropriate permissions for the WordPress directory:
   sudo chown -R www-data:www-data /var/www/wordpress
   sudo chmod -R 755 /var/www/wordpress
  1. Download and extract the WordPress files:
   cd /tmp
   wget https://wordpress.org/latest.tar.gz
   tar -xzf latest.tar.gz
   sudo cp -R wordpress/* /var/www/wordpress/
  1. Create the WordPress configuration file:
   sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
   sudo nano /var/www/wordpress/wp-config.php
  1. Edit the database settings in the wp-config.php file with your MySQL credentials.
  2. Set appropriate permissions for the WordPress files:
   sudo chown -R www-data:www-data /var/www/wordpress
   sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
   sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;

Step 7: Access WordPress

Open your web browser and navigate to http://example.com, replacing example.com with your domain name or server IP address.

Congratulations! You have successfully set up an Nginx reverse proxy server with Apache for WordPress. Nginx will now forward requests to Apache where WordPress is installed. Ensure that you have configured DNS or the hosts file correctly to point the domain to your server’s IP address.

Please note that this tutorial covers the basic steps to get started with an Nginx reverse proxy setup for WordPress. Depending on your specific requirements and environment, additional configuration and security measures may be necessary.