Difference between revisions of "Nginx"

From ArchWiki
Jump to: navigation, search
Line 42: Line 42:
Here is a daemon script to be used in rc.conf on boot to start php-cgi. save it as /etc/rc.d/fastcgi and add "fastcgi" to your DAEMONS array in /etc/rc.conf
Here is a daemon script to be used in rc.conf on boot to start php-cgi. save it as /etc/rc.d/fastcgi and add "fastcgi" to your DAEMONS array in /etc/rc.conf
   . /etc/rc.conf
   . /etc/rc.conf
   . /etc/rc.d/functions
   . /etc/rc.d/functions
   case "$1" in
   case "$1" in

Revision as of 12:06, 26 September 2009


Nginx (pronounced "engine X") written by Igor Sysoev (Russia) in 2005, is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. According to the June 2009 Web Server Survey from Netcraft, nginx now hosts nearly 3.37% of all domains worldwide, while lighttpd hosts about 1.8%. Nginx is now well known for its stability, rich feature set, simple configuration, and low resource consumption.


The nginx package is now in the [community] repository, the latest stable version is nginx-0.6.34:

 # pacman -S nginx

or you can compile and install the latest development version with ABS.

Starting service


 # /etc/rc.d/nginx start

to start working. The default main page is /srv/http/nginx/index.html. Now you can use this URL: to test if nginx is working.

To enable service by default at startup just add "nginx" to the DAEMONS in the /etc/rc.conf.


You can modify the configurations by editing the files in /etc/nginx/conf. (/etc/nginx/conf/nginx.conf being the main config file) .

More details can be referred from Nginx Configuration Examples.

Use PHP/Python with nginx

FastCGI technology is introduced into nginx to work with many external tools and servers, ie: PHP and Python. So, you cannot use PHP or Python service unless FastCGI server has been started.

PHP and Python can be run as FastCGI application and can process FastCGI requests from nginx.

Step 1: Start the FastCGI server
There are two different ways to run FastCGI server:

1) Directly running PHP’s built-in FastCGI server - this method don’t require any third party tools.
Archlinux's php package in the [extra] repository has already enabled FastCGI support.
For example:

 $ sudo pacman -S php php-cgi
 $ php-cgi -b &

This command make the local machine as a FastCGI server using port 9000.

Note: Since nginx 0.7.62 the default $document_root is /etc/nginx/html which is moved outside the default nginx directory and symlinked to /srv/http/nginx. For php to work with nginx 0.7.62 the open_basedir in /etc/php/php.ini has to contain the directory /etc/nginx/html. IE. open_basedir = /etc/nginx/html/:/srv/http/:/home/:/tmp/:/usr/share/pear/

Here is a daemon script to be used in rc.conf on boot to start php-cgi. save it as /etc/rc.d/fastcgi and add "fastcgi" to your DAEMONS array in /etc/rc.conf

 . /etc/rc.conf
 . /etc/rc.d/functions
 case "$1" in
 	stat_busy 'Starting Fastcgi Server'
 	if /usr/bin/php-cgi -b &
 		add_daemon fastcgi
 		stat_fail	fi
 	stat_busy 'Stopping Fastcgi Server'
 	[ -e /var/run/daemons/fastcgi ] && kill $(pidof php-cgi) &> /dev/null;
 	if [ $? -gt 0 ]; then 
 		rm_daemon fastcgi
 	$0 stop
 	$0 start
 	echo "Usage: $0 {start|stop|restart}"

2) Running PHP inside some third-party wrapper - it can be more comfortable than first method because of more flexibility. You can install fcgi(which includes cgi-fcgi), lighttpd (which includes spawn-fcgi), and PHP-FPM (PHP FastCGI Process Manager, which is now in the AUR. Recommend.)
For example:

 # cgi-fcgi -start -connect localhost:9000 /usr/bin/php-cgi
 # spawn-fcgi -a -p 9000 -C 9 -f /usr/bin/php-cgi
 # php --fpm -b

Note: You may need to edit /etc/php-fpm.conf at first. More details about php-fpm can be referred from php-fpm website

Note: Sometimes, you may encounter some permission issue when running some third party software to start PHP as FastCGI-server. See Tips.

Step 2: Edit /etc/nginx/conf/nginx.conf
Un-comment these lines and edit in the file to be as follows:

 location ~ \.php$ {
      root           html;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params

Step 3: Restart nginx
If nginx has been working, run

 # /etc/rc.d/nginx restart

Edit /srv/http/nginx/index.php,


view the URL: http://localhost/index.php with your browser, and you'll find PHP has worked well with nginx.


  • allow/deny based on IP


  • HTTP authentication


  • HTTPS support



  • Tuning the kernel for better speed

Add the followings to /etc/sysctl.conf

 net.ipv4.tcp_fin_timeout = 30
 net.ipv4.tcp_keepalive_time = 300
 net.ipv4.tcp_syncookies = 1
 net.ipv4.tcp_tw_reuse = 1
 net.ipv4.tcp_tw_recycle = 1
 net.ipv4.ip_local_port_range = 5000 65000 

and make it active:

 # /sbin/sysctl -p
  • The browser results in: the page you are looking for is temporarily unavailable. Please try again later.

This is because the FastCGI server has not been started.

  • The browser results in: no input file specified

If the configuration of nginx (fastcgi_param SCRIPT_FILENAME) is all right, this kind of error means php fail to load the requestd script. Usually it's simply an permission issue, you can just run php-cgi as root

 # spawn-fcgi -a -p 9000 -f /usr/bin/php-cgi

or you should create some group and user to start the php-cgi. For instance :

 $ sudo groupadd www
 $ sudo useradd -g www www
 $ sudo chmod +w /srv/www/nginx/html
 $ sudo chown -R www:www /srv/www/nginx/html
 $ sudo spawn-fcgi -a -p 9000 -u www -g www -f /usr/bin/php-cgi

Another occasion is that, wrong "root" argument in the "location ~ \.php$" section in nginx.conf, make sure the "root" points to the same diretory as it in "location /" in the same server. Or you may just set root as globle, don't define it in any location section.

Also remine that your php script path was defaultly defined to /srv/www/nginx/html by argument "open_basedir" in /etc/php/php.ini, you can change them if you need.