Nginx (pronounced “engine x”) is a free, open-source, high-performance HTTP server known for its stability, rich feature set, simple configuration, and low resource consumption.
I do this install several times a month for development servers but someone asked me for the quick version the other day. So here it is in a nutshell, how to install Nginx (nginx version: nginx/1.9.x at time of posting), PHP5 (PHP5-FPM version 5.3 at posting), and MySQL support on Ubuntu 13.04 LTS using apt-get.
This PPA is maintained by the Nginx team.
You can get the latest stable version of Nginx from the Nginx PPA on Launchpad.
You will need to have root privileges to perform the following commands.
1 2 3 4 5 6 |
root@corp:/# sudo -s root@corp:/# apt-get install python-software-properties dialog root@corp:/# nginx=development # use nginx=stable for latest stable version root@corp:/# add-apt-repository ppa:nginx/$nginx root@corp:/# apt-get update root@corp:/# apt-get install nginx |
Nginx should already be started but if not start it
1 |
root@corp:/# service nginx start |
At this point you should be able to access your servers public IP address and see the default “Welcome to Nginx” (or in my case a 404 page) screen.
Nginx should start at boot time but if not, run
1 |
root@corp:/# update-rc.d nginx defaults |
Install MySQL
1 |
root@corp:/# apt-get install mysql-server mysql-client |
If like me, you use a separate MySQL server, you don’t need to install mysql-server. If you install mysql-server, you will be asked for a “root” password for MySQL. Make it secure and write it down for later usage.
Install PHP
1 2 3 4 |
root@corp:/# add-apt-repository ppa:brianmercer/php5 root@corp:/# apt-get update root@corp:/# apt-get upgrade root@corp:/# apt-get install php5-fpm |
Then open /etc/php5/fpm/php.ini and add the line cgi.fix_pathinfo = 1 right at the end of the file:
1 |
root@corp:/# nano -w /etc/php5/fpm/php.ini |
Sample nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
user www-data; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; # multi_accept on; } http { include /etc/nginx/mime.types; access_log /var/log/nginx/access.log; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; types_hash_max_size 2048; server_tokens off; index index.php index.html index.htm; gzip on; gzip_disable "MSIE [1-6].(?!.*SV1)"; upstream php { server 127.0.0.1:9000; } include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*; } |
Sample etc/nginx/conf.d/default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
server { fastcgi_buffer_size 4K; fastcgi_buffers 64 4k; server_name _; root /var/www/sites/$host; error_log /var/log/nginx/error.log notice; access_log /var/log/nginx/access.log combined; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Very rarely should these ever be accessed outside of your lan location ~* .(txt|log)$ { allow 127.0.0.1; deny all; } location ~ ..*/.*.php$ { return 403; } location / { try_files $uri rewrite; } location @rewrite { # Some modules enforce no slash (/) at the end of the URL # Else this rewrite block wouldn't be needed (GlobalRedirect) rewrite ^/(.*)$ /index.php?q=$1; } location ~ .php$ { # Zero-day exploit defense. # http://forum.nginx.org/read.php?2,88845,page=3 # Won't work properly (404 error) if the file is not stored on this server #which is entirely possible with php-fpm/php-fcgi. # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. #And then cross your fingers that you won't get hacked. try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass php; } location ~* .(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } } |
I would honestly RECOMMEND that you go to the Nginx site for configuration information. This setup is on the development server and temp production servers behind firewalls. Just because this works for me doesn’t mean that it will work for you!