Difference between revisions of "Nginx"

From ArchWiki
Jump to: navigation, search
Line 7: Line 7:
  
 
==Installation ==
 
==Installation ==
The nginx package is now in the [community] repository, the latest stable version is nginx-0.6.34:
+
The nginx package is now in the [community] repository, the latest stable version is nginx-0.7.62:
 
   # pacman -S nginx
 
   # pacman -S nginx
 
or you can compile and install the latest development version with ABS.
 
or you can compile and install the latest development version with ABS.
Line 14: Line 14:
 
Run
 
Run
 
   # /etc/rc.d/nginx start
 
   # /etc/rc.d/nginx start
to start working. The default main page is /srv/http/nginx/index.html. Now you can use this URL: <nowiki>http://127.0.0.1</nowiki> to test if nginx is working.
+
The nginx server should now be running.  
 +
The default main page is /srv/http/nginx/index.html(the nginx package does not install a default index.html, you must create this now).
 +
After creating a /srv/http/nginx/index.html webpage, you can use this URL: <nowiki>http://127.0.0.1</nowiki> to test if nginx is working.
  
 
To enable service by default at startup just add "nginx" to the DAEMONS in the /etc/rc.conf.
 
To enable service by default at startup just add "nginx" to the DAEMONS in the /etc/rc.conf.
Line 38: Line 40:
 
This command make the local machine as a FastCGI server using port 9000.
 
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/
+
'''Note:''' Since nginx 0.7.62 the default internal $document_root is /etc/nginx/html which is moved outside the default nginx directory and symlinked to /srv/http/nginx in the arch package. 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
+
Below 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
 
   #!/bin/bash
 
   #!/bin/bash
 
    
 
    

Revision as of 12:14, 26 September 2009


Summary

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.

Installation

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

 # pacman -S nginx

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

Starting service

Run

 # /etc/rc.d/nginx start

The nginx server should now be running. The default main page is /srv/http/nginx/index.html(the nginx package does not install a default index.html, you must create this now). After creating a /srv/http/nginx/index.html webpage, you can use this URL: http://127.0.0.1 to test if nginx is working.

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

Configuring

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 127.0.0.1:9000 &

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

Note: Since nginx 0.7.62 the default internal $document_root is /etc/nginx/html which is moved outside the default nginx directory and symlinked to /srv/http/nginx in the arch package. 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/

Below 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

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

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 127.0.0.1 -p 9000 -C 9 -f /usr/bin/php-cgi
 # php --fpm -b 127.0.0.1:9000

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_pass   127.0.0.1:9000;
      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,

 <?php
   phpinfo();
 ?> 

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

Security

  • allow/deny based on IP

ngx_http_access_module

  • HTTP authentication

ngx_http_auth_basic_module

  • HTTPS support

ngx_http_ssl_module

Tips

  • 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 127.0.0.1 -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 127.0.0.1 -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.

References