arrow-left arrow-right brightness-2 chevron-left chevron-right circle-half-full dots-horizontal facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Install nginx w/ php5 on Ubuntu 8.10
2 min read

Install nginx w/ php5 on Ubuntu 8.10

Below are the steps I took to configure nginx and php5 on my Ubuntu 8.10 server. I choose nginx because its lightweight and relatively easy to manage once you figure out the configuration. I’ve been using Apache for years and its second nature at this point, however, on my small VPS server I needed to maximize my resources.

Make sure your system is updated:

sudo aptitude update && sudo aptitude safe-upgrade

Install nginx and PHP5, we’ll be installing the cgi version of php.

sudo aptitude install nginx php5-cgi

Download the following php-fastcgi startup script and save it to /etc/init.d/php-fastcgi. Nginx will use php in cgi-mode which is why we are creating a init file for it. (note: I found this init script on a mailing list someplace)

Download: php-fastcgi init script

Next, run the following commands on the php-fastcgi script.

sudo chmod u+x /etc/init.d/php-fastcgi
sudo chown 0.0 /etc/init.d/php-fastcgi
sudo update-rc.d php-fastcgi defaults 21 23

Now, go ahead and create your directory that will store your website. For example you might do the following:

mkdir -p /home/username/domains/yourdomaincom/{public_html,log,cgi-bin}

Modify the /etc/nginx/nginx.conf file and set the following variable. I have mine set to 15MB but if you wish to allow larger files to be uploaded over http set this accordingly.

client_max_body_size 15m;

Finally, lets setup a virtual domain. Navigate to the /etc/nginx/sites-available directory and create a file called yourdomain.com (replace with your domain) and use the following as a template. Make sure to replace your paths etc.

[sourcecode language=’css’]server {
listen 80;
server_name yourdomain.com www.yourdomain.com;

access_log /home/username/domains/yourdomain.com/log/access.log;

location / {
root /home/username/domains/yourdomain.com/public_html;
index index.html index.htm index.php;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/username/domains/yourdomain.com/public_html$fastcgi_script_name;
include fastcgi_params;
}
}[/sourcecode]

Like apache, nginx has a sites-available and sites-enabled folder this allows for better control of active/disabled virtual domains. Navigate to the /etc/nginx/sites-enabled folder and create a symlink back to your virtual host’s configuration.

cd /etc/nginx/sites-enabled/ && sudo ln -s ../sites-available/yourdomain.com

Finally, lets start everything up.
/etc/init.d/php-fastcgi start
/etc/init.d/nginx start

At this point you should be able to browse to your site using your domain name. You can easily duplicate multiple domains by creating new virtual host files and creating the sym link as noted above, remember that ngix will need to be restarted.