Lets start with install nginx.
apt update && apt -y upgrade apt install nginx
Restart nginx with command
service nginx restart
Now you will be able to visit server with IP address, will see default page like
Nginx config for default web site is at /etc/nginx/sites-enabled/default, if you remove all comments, it look like
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }
Install PHP
We install PHP and common modules with command
apt -y install php php-cli php-curl php-gd php-mysql php-imagick php-imap php-mcrypt php-json php-xml php-mbstring php-zip php-xmlrpc php-soap php-intl
Install PHP-FPM
Install php-fpm
apt -y install php-fpm
On Ubuntu 16.04, start php-fpm with
service php7.0-fpm start
This command may be different depending on the version of PHP installed.
We need to find socket or port used by php-fpm, this can be found with command
cat /etc/php/7.0/fpm/pool.d/www.conf | grep listen
Path may wary depending on PHP version. In this cause, we use unix socket for php-fpm, the socket path is /run/php/php7.0-fpm.sock
Configure Nginx with PHP-FPM
Edit Nginx default config
vi /etc/nginx/sites-enabled/default
Find
index index.html index.htm index.nginx-debian.html;
Replace with
index index.php index.html index.htm index.nginx-debian.html;
Find
location / { try_files $uri $uri/ =404; }
Add below
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; }
Here is config after edit.
Restart nginx
service nginx restart
Testing if PHP working
To test if PHP work for default nginx web site, we need to create a PHP file under /var/www/html folder
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
Now access the web site with IP address of the server.
http://Your-Server-Ip/phpinfo.php
You will see phpinfo() page.
Related posts