Install Nginx Web server With Php On Ubuntu
Last Updated Tue, Feb 19 2019 4:17 PM
1. Update packages
# apt-get update
2. Install nginx server
# sudo apt-get install nginx -y
3. Install other required packages
# sudo apt-get install python-software-properties
4. Add php repo and install php along with other modules
# sudo add-apt-repository ppa:ondrej/php
# sudo apt-get update
# sudo apt-get install php5.6 php5.6-fpm
# sudo apt-get install php5.6-mysql php5.6-gd php5.6-curl php5.6-mcrypt php5.6-imap php5.6-oauth
5. Edit php.ini file to add config
# sudo nano /etc/php5.6/fpm/php.ini
# cgi.fix_pathinfo=0
sudo nano /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5.6-fpm.sock
6. Edit the nginx vhost config file
# nano /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name server_domain_name_or_IP;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
7. Add a sample php page in web directory
# mkdir -p /usr/share/nginx/html (make folder is not existing)
# nano /usr/share/nginx/html/index.php
## add the following php code and save the file.
echo "Hi how are You";
8. Check webpage on browser