How to install Nginx and PHP on Ubuntu

For this tutorial I am using the Ubuntu 8.10 linux version. We given very easy steps for installing php and ngnix and configuration details.

How to install Nginx and PHP on Ubuntu

Install Nginx
# sudo apt-get install nginx

If apache server is installed on your machine then please stop the apache server first.

#sudo /etc/init.d/nginx start

#sudo update-rc.d nginx defaults

Install PHP5

#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-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Open the PHP.ini file

#sudo vim /etc/php5/cgi/php.ini

Insert following line at the end of php.ini file
cgi.fix_pathinfo = 1

Install the lighttpd server

#sudo apt-get install lighttpd

You will see an error message saying that lighttpd couldn’t start because port 80 is already in use.

For removing the error message use following command

#sudo update-rc.d -f lighttpd remove

Start the FastCGI daemon service

#sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

If you want run following command whenever you boot the system. Open following file.

#sudo vim /etc/rc.local

Copy paste following following lines in file.

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

Configuration Nginx

#sudo vim /etc/nginx/nginx.conf
Just copy paste the following code in end of httpd block. Dont paste outside of http block.

server {
listen 80;
server_name _;

access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}

# 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 /var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

How to install Nginx and PHP on Ubuntu
How to install Nginx and PHP on Ubuntu

Just restart the Nginx server.

#sudo /etc/init.d/nginx restart

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “How to install Nginx and PHP on Ubuntu”

Leave a Reply

Your email address will not be published.