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
- Update the package lists:
sudo apt update
- Install Nginx:
sudo apt install nginx
Step 2: Install Apache and PHP
- Install Apache and PHP:
sudo apt install apache2 libapache2-mod-php
- 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
- Install MySQL:
sudo apt install mysql-server
- Run the MySQL security script to improve the database security:
sudo mysql_secure_installation
Step 4: Configure Apache for WordPress
- Create a virtual host configuration file for WordPress:
sudo nano /etc/apache2/sites-available/wordpress.conf
- 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>
- Enable the site and restart Apache:
sudo a2ensite wordpress.conf
sudo systemctl restart apache2
Step 5: Configure Nginx as a Reverse Proxy
- Modify the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
- 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;
}
}
- Restart Nginx:
sudo systemctl restart nginx
Step 6: Install and Configure WordPress
- Create a directory for WordPress:
sudo mkdir /var/www/wordpress
- Set appropriate permissions for the WordPress directory:
sudo chown -R www-data:www-data /var/www/wordpress
sudo chmod -R 755 /var/www/wordpress
- 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/
- 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
- Edit the database settings in the
wp-config.php
file with your MySQL credentials. - 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.