In a pursuit to optimise a customers server, we looked at the initial stack of software which runs the application. As it runs on a VPS and has limited resources, we decided on using Nginx – an extremely lightweight web server, instead of Apache which can be a resource hog. Below you will find my guide for installing Nginx 0.7.76, PHP 5.3, MySQL 5.1 and APC caching. Enjoy!
Let’s install Nginx
[sourcecode language="bash"] sudo apt-get install nginx
[/sourcecode]
Start your server and test:
[sourcecode language="bash"]/etc/init.d/nginx start[/sourcecode]
Update your nginx configuration (see later section!) /etc/nginx/sites-available/default (make a backup of the old one):
[sourcecode language="bash"]
server {
listen 80 default;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
## Default location
location / {
root /var/www;
index index.php index.htm index.html;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /var/www;
}
## Parse all .php file in the /var/www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
upstream backend {
server 127.0.0.1:9000;
}
[/sourcecode]
Visit the IP number in your web browser to make sure it’s installed ok “Welcome to nginx!“. Next, add to /etc/apt/sources.list:
[sourcecode language="bash"] deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
[/sourcecode]
then run
[sourcecode language="bash"]
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 8D0DC64F
sudo apt-get update
[/sourcecode]
Now let’s install PHP 5.3, and the things you need. Here’s my stack:
[sourcecode language="bash"]
sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json php5-suhosin php5-common php-apc php5-dev
[/sourcecode]
A few of these are replaced by php5-common – this is ok and I should update the stack at some point but it’s automatic!
Now we grab the PHP FPM + CGI Module:
[sourcecode language="bash"]sudo apt-get install php5-fpm php5-cgi[/sourcecode]
Restart Nginx + Fast CGI
[sourcecode language="bash"]sudo /etc/init.d/nginx restart[/sourcecode]
and you should get something like this:
[sourcecode language="bash"]
ryan@localhost:~# sudo /etc/init.d/nginx restart
Restarting nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
nginx.
[/sourcecode]
Get CGI up:
[sourcecode language="bash"]/etc/init.d/php5-fpm start[/sourcecode]
Create an index.php in /var/www with and test your configuration. This was all working for me on Ubuntu Server amd64. Now it’s time to try and deploy an application to see how it can work :)
My nginx.conf file:
[sourcecode language="bash"]
user www-data 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;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml
application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
[/sourcecode]
I installed memcache and APC (beta works with 10.04 lucid but you need libpcre installed ). Select the [no] defaults:
[sourcecode language="bash"]
sudo pecl install memcache
sudo apt-get install libpcre3-dev
sudo pecl install apc-beta
[/sourcecode]
Didn’t need to modify any php.ini so far, but it is in 3 locations so I’ll see how this goes :-)
Update: Don’t forget to install postfix and configure it as an Internet Server if it was not part of your default installation. Then you can send emails…. Also, just to make sure… “ufw” the default firewall needs your attention ;)
[bash]sudo apt-get install postfix[/bash]