NGINX - 2015
Recently, somebody asked me why you're using Apache but not Nginx. I could not answer. Now, I'm writing about it.
It now runs about 15 percent of all websites, including everyone from startups such as CloudFlare and Parse (bought by Facebook earlier this year) to web giants such as Automatic and Netflix.
apt-get install mysql-server mysql-client
Nginx is available as a package for Ubuntu 13.1.
$ apt-get install nginx
By default, nginx will be installed in /usr/sbin/nginx.
After download the source, installation can be done by using these commands from a terminal:
./configure make sudo make install
By default, nginx will be installed in /usr/local/nginx.
Start the server as root:
$ sudo service nginx start $ ps -ef|grep nginx ps -ef|grep nginx root 4638 1221 0 18:43 ? 00:00:00 nginx: master process /usr/sbin/nginx www-data 4639 4638 0 18:43 ? 00:00:00 nginx: worker process www-data 4640 4638 0 18:43 ? 00:00:00 nginx: worker process www-data 4642 4638 0 18:43 ? 00:00:00 nginx: worker process www-data 4643 4638 0 18:43 ? 00:00:00 nginx: worker process root 4654 2182 0 18:44 pts/0 00:00:00 grep --color=auto nginx
Whether we have installed Nginx using the package manager or from source, we will need to look at the main configuration file and see what may need changing and optimising.
For this installation, the document root is /usr/share/nginx/html, and if we type in localhost into url:
We can make PHP5 work in nginx through php-fpm (FastCGI Process Manager) is an alternative php FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:
apt-get install php5-fpm
The php-fpm is a daemon process (with the init script /etc/init.d/php5-fpm) that runs a FastCGI server on port 9000.
After editing the configuration file at /usr/local/nginx/conf/nginx.conf to whatever we want, we can reload the configuration with:
service nginx reload
One of the important configuration is set in /etc/nginx/sites-available/default and we need to modify the file as below:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm index.php; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.html; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } ... # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # # With php5-cgi alone: fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
Save the file and reload ngix.
Then, load the info.php file on browser:
<?php phpinfo(); ?>
We get:
If we load the following test.php file:
<pre> <?php var_export($_SERVER) ?> </pre>
We get:
array ( 'USER' => 'www-data', 'HOME' => '/var/www', 'FCGI_ROLE' => 'RESPONDER', 'QUERY_STRING' => '', 'REQUEST_METHOD' => 'GET', 'CONTENT_TYPE' => '', 'CONTENT_LENGTH' => '', 'SCRIPT_FILENAME' => '/usr/share/nginx/html/test.php', 'SCRIPT_NAME' => '/test.php', 'REQUEST_URI' => '/test.php', 'DOCUMENT_URI' => '/test.php', 'DOCUMENT_ROOT' => '/usr/share/nginx/html', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_SOFTWARE' => 'nginx/1.4.1', 'REMOTE_ADDR' => '127.0.0.1', 'REMOTE_PORT' => '42436', 'SERVER_ADDR' => '127.0.0.1', 'SERVER_PORT' => '80', 'SERVER_NAME' => 'localhost', 'HTTPS' => '', 'REDIRECT_STATUS' => '200', 'HTTP_HOST' => 'localhost', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_CONNECTION' => 'keep-alive', 'PHP_SELF' => '/test.php', 'REQUEST_TIME_FLOAT' => 1383958416.7519, 'REQUEST_TIME' => 1383958416, )
NOTE: for 502 bad gateway message.
We need to go to /etc/php5/fpm/pool.d/www.conf and edit the line listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000.
$ nginx -h nginx version: nginx/1.4.3 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/Cellar/nginx/1.4.3/) -c filename : set configuration file (default: /usr/local/etc/nginx/nginx.conf) -g directives : set global directives out of configuration file
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization