← cd ~/blog

less ispconfig-how-to-migrate-from-apache-to-nginx.md

Ispconfig : how to migrate from Apache to nginx

#Bash#Debian & Sysadmin#Ispconfig archive

# historical archive — written years ago, kept as it was. Some info is dated.

The time has come! Since switching to WordPress, my one imperative is: even the most complex pages must be served quickly!
I read (not true), studied (but also not), in short, without any documentation I stumbled into the crazy adventure of replacing (on the fly) Apache with nginx in an Ispconfig3 environment!
First things first: this howto is provided as is.
Make a backup of everything before every change, because I am not responsible for any mistakes you make! With a backup you’ll be able to go back; without a backup, don’t even start following the next steps!
Enough now, I’ve been far too long with the warnings!

What I had:

  1. 20 working websites under Apache with php5 etc etc..
  2. Varnish as a Reverse Proxy;
  3. Ispconfig managing everything.

What I didn’t like:

  1. having to always use .htaccess files for every little thing. I was minifying CSS and JS and ended up with huge .htaccess files!!!
  2. the pages were served slowly relative to the content.

What I would have liked to have:

  1. an almost single configuration that would let me have fast pages and ease of use of the web server!
  2. … I think that’s enough, right?!.

Question: are you sure nginx is right for you? No, I don’t know, but I saw, read, a post that convinced me to make the switch.

Let’s begin!

APACHE

The switch itself is very simple. The complex part will come later and I will NOT cover it today; I reserve the right to write my own personal post on how to best configure nginx, fastcgi_cache, memcache and w3 total cache for WordPress!

The wonderful thing is that while we work, you’ll be able to keep your websites up and running!!!

Stop any firewalls; it’s the simplest thing so you don’t have to open and close ports, risking doing something silly.

the files that need to be modified for Apache are: /etc/apache2/ports.conf and all the files in the directory /etc/apache2/sites-enabled/

the first is simple:

vi /etc/apache2/ports.conf

edit it so it looks something like this:

Listen 8000

Listen 8043

Listen 8043

In short, you need to change the listening ports for the http and https protocols.

This file was the most complex part! Now

cd /etc/apache2/sites-enabled/
sed -i 's/<80>/8000/g' *
sed -i 's/<443>/8043/g' *

this way you’ve changed apache’s listening ports, but the change won’t take effect until you restart apache. For it to keep serving our sites while we work, all you have to do is redirect all incoming traffic on ports :80 and :443 to the new ones. The kernel’s pre-routing table comes to our aid.

Now, thanks to iptables (if it’s not installed, take care of it!) we’ll work the magic:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8000
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8043
service apache2 restart

verify that the command

iptables -n -L -t nat

returns something like

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8000
REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443 redir ports 8043

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

NGINX

Now we’re free to proceed with installing nginx. We’ll use the version with support for fastcgi-cache and so we’ll add a repository to our list:

apt-get install python-software-properties
add-apt-repository ppa:rtcamp/nginx
apt-get update
apt-get install nginx-custom

If, after running apt-get update, you should receive an error like

...
W: Impossibile recuperare http://ppa.launchpad.net/rtcamp/nginx/ubuntu/dists/wheezy/main/source/Sources  404  Not Found                                                                                                                                                                                 

W: Impossibile recuperare http://ppa.launchpad.net/rtcamp/nginx/ubuntu/dists/wheezy/main/binary-i386/Packages  404  Not Found

E: Impossibile scaricare alcuni file di indice: saranno ignorati o verranno usati quelli vecchi.

check the file /etc/apt/sources.list.d/rtcamp-nginx-wheezy.list and replace wheezy with the name of the corresponding Ubuntu version. I used quantal.

ISPCONFIG

Now we have to deal with Ispconfig so that it forgets about Apache and starts using nginx correctly. Reach for phpMyadmin!

We need to get to the Ispconfig DB for a manual change; the table is ‘server’.

Let’s edit the only row that should be present

Now we’re interested in modifying two parameters in the config column

The [global] section

and the [web] one

replace ‘apache’ with ‘nginx’, save and close! You’re almost done!

cd /usr/local/ispconfig/server/plugins-enabled/
rm apache2_plugin.inc.php
ln -sv /local/ispconfig/server/plugins-available/nginx_plugin.inc.php nginx_plugin.inc.php

NGINX + ISPCONFIG

Let’s show Ispconfig to nginx

You’ll need a few files (I’ll provide them) that you’ll have to put in the right place!

Download the file and be careful where you unpack it

cd /tmp
tar xfjv etc.tar.bz2
cd etc/ngnix
cat nginx.conf > /etc/nginx/nginx.conf
cp sites-available/* /etc/nginx/sites-available/

Now let’s stop Apache, remove the redirection from the pre-routing table, and restart nginx.

service apache2 stop
iptables -t nat  -D PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8000
iptables -t nat  -D PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8043
service nginx restart

Cross your fingers and verify that nginx sees Ispconfig correctly by visiting the link http://yourdomain:8080/

The simplest thing you need to do to activate your sites under nginx will be to open and re-save each site from the Ispconfig control panel. This will generate a new standard configuration file for nginx and start the corresponding process.

Some things should be checked, such as whether or not the php-fpm socket feature is enabled for each site. If it isn’t, some PHP sites might report the 502 Bad Gateway error.

We’re done, I hope everything went well!

See you soon!!!