Home |
Last modified: 16-06-2020 |
Like Lighttpd, Nginx is a faster and leaner alternative to Apache. It can be used as either a regular web server or a reverse proxy. This is a short HOWTO on installing Nginx with PHP5 in FastCGI mode on Ubuntu (10.04).
Nginx.1.Web.Server.Implementation.Cookbook_Dipankar.Sarkar.2011.pdf
Nginx.HTTP.Server_Clement.Nedelcu.2nd.edition.2013.pdf
Mastering.Nginx_Dimitri.Aivaliotis.2013.pdf
http://wiki.nginx.org/Pitfalls
http://wiki.nginx.org/QuickStart
http://wiki.nginx.org/Configuration
NONE /usr/share/doc/nginx-doc/examples/
DONE http://till.klampaeckel.de/blog/archives/30-PHP-performance-III-Running-nginx.html
http://www.sitepoint.com/blogs/2010/05/04/php-with-nginx-is-about-to-become-a-lot-easier/
http://blog.hbis.fr/2009/03/18/nginx-fastcgi-php/
Install Nginx and check that it works OK before moving on to adding PHP5 to the mix:
To remove Nginx: apt-get --purge remove nginx ; rm -Rf /var/www
Note that if you perform multiple install/remove cycles, you might need to clean the database:
If you want to find where the binaries will be installed before running "make install", use "make -n install".
More information:
The main configuration file is /etc/nginx.conf, which can include yet more configuration files through the "include" command; In particular, additional files can be found in /etc/nginx/conf.d/ and /etc/nginx/sites-available/ (/etc/nginx/sites-enabled/default is a symlink to /etc/nginx/sites-available/default).
By default, the main file contains two contexts: events and http. The http context contains two includes pointing to ./conf.d/* and ./sites-available/*.
The first thing you probably want to do is save a copy and start experimenting with ./sites-available/default.
If you need to host multiple web sites on a single server, you need to configure Nginx to support so-called virtual hosts through one or more Server blocks. All you need to do is add server blocks to the sites-enables/default, and it will know which site to deliver based on the hostname used by the browser:
http://wiki.nginx.org/Pitfalls
http://wiki.nginx.org/QuickStart
http://wiki.nginx.org/Configuration
# Default catch-all virtual host when reached through IP
server {
root
/usr/share/nginx/www;
index
index.html index.htm;
server_name
_;
access_log /var/log/nginx/default.access.log;
error_log
/var/log/nginx/default.error.log;
}
server {
root /usr/share/nginx/www/site1;
index index.html index.htm;
#This is how Nginx knows how to map the hostname
to an actual site
server_name www.site1.com;
}
server {
root /usr/share/nginx/www/site2;
index index.html index.htm;
#so
the server can be reached with or without "www"
server_name www.site2.com
site2.com;
}
nginx -V
Before using a directive in nginx.conf, make sure the relevant module was compiled in the application.
nginx -t (checks the default configuration file)
nginx -t -c /home/joe/test.conf
Here's an example from Nginx 1 Web Server Implementation Cookbook:
Although putting sensitive information on a public server is never a good idea, you might need to. Here's how to protect a directory by requiring the user to type a login and password:
There are several ways to run PHP5 with Nginx:
We'll use PHP-FPM to run PHP5.
Tips:
By default, the followings files in /etc/nginx/ don't need to be modified: nginx.conf, fastcgi_params. The only file you need to edit for a standard install is /etc/nginx/sites-available/default. Here's an example:
Next, add the usual PHP script to check that it works:
Restart Nginx: /etc/init.d/nginx restart
Aim your browser to the server and check that it works as expected.
Check for errors in /var/log/nginx/error.log.
If you make any change to php.ini, remember to /etc/init.d/php5-fpm restart
Does FPM use both INI files below?
# ll /etc/php5/fpm/
conf.d -> ../conf.d/
php5-fpm.conf
php.ini
Should I include this file to use PDO in PHP5 scripts?
# ll /etc/php5/conf.d/
pdo.ini
suhosin.ini
http://192.168.0.3/punbb -> http://localhost/punbb/
http://192.168.0.3/punbb/ OK
Updated /etc/nginx/sites-available/default, restarted both PHP5-FPM and Nginx: redirected to 80 with 301 error