Steps to Migrating from Apache2 to NGINX

First, we will install NGINX and PHP-FPM:

sudo apt-get install nginx php5-fpm

Next, we will need to update the PHP-FPM config at /etc/php5/fpm/pool.d/www.conf

sudo nano /etc/php5/fpm/pool.d/www.conf

My server has 4 cores, so this is what my www.conf file looks like:

user              www-data;
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log         /var/log/nginx/error.log;
pid               /var/run/;

events {
    worker_connections  2048;

http {
    include     /etc/nginx/mime.types;
    access_log  /var/log/nginx/access.log;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   5;
    tcp_nodelay         on;
    server_tokens       off;

    gzip          on;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_disable  "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

After making the changes to PHP-FPM we will need to restart the service:

sudo /etc/init.d/php5-fpm restart

Now we can setup our NGINX config at /etc/nginx/nginx.conf

sudo nano /etc/nginx/nginx.conf

Configurations will vary based on your server and web site, but these setting worked for me, find and change these values in the nginx.conf file:

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 100
php_admin_value[memory_limit] = 128M

Note: Be sure and remove any leading semicolons (;)

After NGINX is configured, restart the service:

sudo /etc/init.d/nginx restart

Creating the Domain Configuration Files in NGINX

In Apache, any domains that are hosted on the server are configured in “/etc/apache2/sites-available/“, and NGINX has its equivalent configuration files in “/etc/nginx/sites-available/“, so the next step is to create those new files in the NGINX directory. I am running WordPress websites on my server, so this is what works best for me:

server {
    listen       80;
    server_name  [] *.[];
    root         /var/www/[]/public;
    access_log   /var/www/[]/log/access.log;
    error_log    /var/www/[]/log/error.log;
    location / {
        index index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    # Directives to send expires headers and turn off 404 error logging.
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
    # this prevents hidden files (beginning with a period) from being served
    location ~ /\.          { access_log off; log_not_found off; deny all; }
    location ~ \.php$ {
        client_max_body_size 25M;
        try_files      $uri =404;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        include        /etc/nginx/fastcgi_params;
    #Once the w3-total.conf file is ready uncomment out the line below
    #include w3-total.conf;

Once the config files in the sites-available directory are ready, we just need to “publish” the sites to the web by creating a symlink to the file from the “sites-enabled” directory:

sudo ln -s /etc/nginx/sites-available/[] /etc/nginx/sites-enabled/[]

After we have updated our site configurations, we will stop Apache and restart NGINX:

service apache2 stop
sudo /etc/init.d/nginx restart

Removing Apache

Once we are confidant that NGINX is running and apache is no longer needed, we will simply remove Apache from our web server.

sudo apt-get remove apache2*



Leave a Reply

On The Web