Slackware style init script for nginx web server
Slackware style init script for start/stop/restart nginx web server:
#!/bin/sh
#
# /etc/rc.d/rc.nginx
#
# Start/stop/restart nginx web server.
test_config() {
set +e
RES=`nginx -t 2>&1`
if [ $? -ne 0 ] ; then
echo "Error in nginx configuration found, restart aborted!"
echo $RES
exit 10
fi
set -e
}
nginx_start() {
ulimit -n 65535
/usr/sbin/nginx -c /etc/nginx/nginx.conf
}
nginx_stop() {
killall nginx
rm -f /var/run/nginx/nginx.pid
}
nginx_restart() {
test_config
nginx_stop
nginx_start
}
case "$1" in
'start')
nginx_start
;;
'stop')
nginx_stop
;;
'restart')
nginx_restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
;;
esac
Using Nginx as a reverse proxy for Apache
We all know that Apache is a great software and it cannot be replaced every time. Also, Apache shown great performance serving dynamic php content whether it’s running mod_php or fastcgi. BoÅ¡tjan Å kufca made an excellent comparison between Apache + mod_php and Nginx + php-fpm.
So, Apache is very good serving dynamic content while nginx is the best on serving static content. But is it possible to have the best from both worlds? Is it possible to use all the apache features (like native .htaccess support) while serving the static content of the website with nginx? Luckily yes.
First of all let’s see the general structure of Apache web server and why it would be a good idea to use a reverse proxy:
1. Client initiates request to the Apache server.
2. His browser connects to the Apache server.
3. The Apache server creates new thread/process to handle the request.
4. If client requested dynamic content, web server spawns CGI process or executes dynamic content handling module (i.e. mod_php) and waits while request will be processed. When it receives resulted web-page, it sends it back to the client.
5. If client asked for some static file, web server sends this file to client
6. Client’s browser receives answer, closes connection to web server and displays content.
Linux Slackware 13.0 + Nginx + PHP FastCGI using PHP-FPM
While Apache is a great web server, it has a high memory footprint. If you don’t need all the Apache features or if you have a slow/old/low memory machine Nginx (Engine X) might be perfect.
The typical nginx configuration involves using spawn-fcgi from the LightTPD project to get nginx serving up PHP. There are a few issues with spawn-fcgi which I’m not going to cover here.
In order to avoid these issues I’m going to use PHP-FPM. PHP-FPM stands for PHP FastCGI Process Manager. It is actually a set of patches for the PHP source that bake in FastCGI process management into the PHP binaries.
PHP-FPM requires libevent to be installed. Since Slackware doesn’t include a libevent package it has to be build from sources. In this tutorial I’m using libevent-1.4.12. An attached slackware package of libevent-1.4.12 is available at the end of this post.
cd /usr/local/src
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar -xzvf libevent-1.4.12-stable.tar.gz
./configure --prefix=/usr
make
make install