Migrating WordPress from Apache to Nginx

Contents

Migrating WordPress from Apache to Nginx

Transitioning your WordPress site from Apache to Nginx can yield significant performance, resource-efficiency and security benefits. This comprehensive guide walks you through planning, configuration, testing and optimization steps to ensure a smooth migration.

Table of Contents

1. Why Choose Nginx over Apache

While Apache remains popular, Nginx shines in high-concurrency environments:

Feature Apache Nginx
Architecture Process-based Event-driven
Memory Footprint Higher under load Low, predictable
Static Content Decent Excellent
Dynamic Content Handled via mod_php Handled via PHP-FPM

2. Prerequisites

  • Root or sudo access on the target Linux server (Ubuntu, Debian, CentOS).
  • Existing WordPress installation on Apache (files database).
  • Backup of files and database (mysqldump or phpMyAdmin).
  • DNS management to point to the new server or IP.
  • Basic familiarity with Linux shell, nginx, php-fpm.

3. Migration Planning

  1. Audit existing Apache configuration, .htaccess rules, SSL setup.
  2. Note PHP version and modules (GD, mbstring, etc.).
  3. Plan directory structure and user permissions.
  4. Prepare a staging environment or deploy during low-traffic periods.
  5. Document everything: virtual hosts, database credentials, SSL certs.

4. Installing and Configuring Nginx

On Ubuntu/Debian:

sudo apt update
sudo apt install nginx
  

Verify installation:

systemctl status nginx
  

By default, Nginx serves /var/www/html. You’ll create server blocks later for your WordPress site.

5. Setting Up PHP-FPM

Install PHP and FPM module:

sudo apt install php-fpm php-mysql php-gd php-xml php-mbstring
  

Check the pool configuration /etc/php/7.4/fpm/pool.d/www.conf (version may vary). Ensure:

  • listen = /run/php/php7.4-fpm.sock or TCP socket.
  • User/group matches Nginx worker user (www-data on Debian/Ubuntu).
  • Adjust pm.max_children, pm.start_servers based on RAM.

Restart PHP-FPM:

sudo systemctl restart php7.4-fpm
  

6. Converting .htaccess Rewrite Rules

Apache’s .htaccess contains WordPress permalinks:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
  

Equivalent in Nginx:

location / {
    try_files uri uri/ /index.phpargs
}
  

7. Creating Nginx Server Blocks

Example file /etc/nginx/sites-available/example.com:

server {
    listen 80
    server_name example.com www.example.com
    root /var/www/example.com
    index index.php index.html index.htm

    # Permalinks  PHP handling
    location / {
        try_files uri uri/ /index.phpargs
    }

    location ~ .php {
        include snippets/fastcgi-php.conf
        fastcgi_pass unix:/run/php/php7.4-fpm.sock
    }

    location ~ .(jpgjpegpnggifcssjssvg) {
        expires max
        log_not_found off
    }
}
  

Enable and test:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
  

8. SSL/TLS Configuration

Use Lets Encrypt with certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
  

Certbot will auto-configure your server block for SSL.

9. Testing and Troubleshooting

  • Check Nginx error logs: /var/log/nginx/error.log.
  • Verify PHP-FPM status: systemctl status php7.4-fpm.
  • Use curl -I https://example.com and browser DevTools.
  • Confirm file permissions: Nginx user (www-data) must own /var/www/example.com.

10. Performance Optimizations

  • Gzip compression:
    gzip on
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml rss text/javascript
          
  • Browser caching: Already implemented via expires in server block.
  • FastCGI cache: Very effective for anonymous visitors.
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:100m inactive=60m
    fastcgi_cache_key schemerequest_methodhostrequest_uri
    
    server {
        ...
        location ~ .php {
            fastcgi_cache WORDPRESS
            fastcgi_cache_valid 200 60m
            fastcgi_pass unix:/run/php/php7.4-fpm.sock
            ...
        }
    }
          

11. Security Enhancements

  • Limit PHP execution:
    location ~ /(:uploadsfiles)/..php {
        deny all
    }
          
  • Hide Nginx version: server_tokens off in nginx.conf.
  • HTTP security headers:
    add_header X-Content-Type-Options nosniff
    add_header X-Frame-Options SAMEORIGIN
    add_header X-XSS-Protection 1 mode=block
          

12. Additional Resources

By following these steps, you’ll harness Nginx’s efficiency, scale your WordPress site under high traffic and maintain a secure environment. Always test configurations in a staging area before production deployment.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *