KenoKivabe

Your Essential Queries

Author
Md Mojahedul Islam
27 Jul, 2022

How To Install Nginx PHP

We'll See How To Install & Configure Nginx And PHP In Linux Easily:

1) Open Terminal

2) Update packages:

  1. sudo apt update && sudo apt upgrade    

2) Install Nginx:

  1. sudo apt install nginx  

3) Enable Firewall:

  1. sudo ufw enable  

4) Allow Nginx To Firewall

  1. sudo ufw allow 'Nginx HTTP'  

5) Restart Firewall:

  1. sudo systemctl restart ufw  

Now visit http://localhost. If everything was successful then you'll  see a webpage running


6) Install PHP (You can specify your required version instead of 7.4)

  1. sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-json php7.4-curl  

7) Create nginx configuration file to run php

  1. sudo nano /etc/nginx/sites-available/localhost  

8) Paste the code

  1. server {  
  2.     listen 80;  
  3.     server_name _;  
  4.     root /var/www/html;  
  5.   
  6.     index index.php index.html index.htm;  
  7.   
  8.     location / {  
  9.         #try_files $uri $uri/ =404;  
  10.         autoindex on;  
  11.     }  
  12.   
  13.     location ~ \.php$ {  
  14.         include snippets/fastcgi-php.conf;  
  15.         fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  
  16.      }  
  17.   
  18.     location ~ /\.ht {  
  19.         deny all;  
  20.     }  
  21.   
  22. }  

To Save :

      press CTRL+x
      press y
      press Enter

9) Create a test php file inside /var/www/html

  1. cd /var/www/html && nano index.php  

10) Put php code inside index.php

  1. <?php    
  2.        phpinfo();  
  3. ?>  

To Save :

      press CTRL+x
      press y
      press Enter

11) Remove Nginx default configuration file

  1. sudo rm -rf /etc/nginx/sites-enabled/default  

12) Link new configuration file

  1. sudo ln -s /etc/nginx/sites-available/localhost /etc/nginx/sites-enabled/  

13) Restart Nginx Server

  1. sudo systemctl restart nginx  

Now reload http://localhost again. If everything was successfull you'll see a php information page


Congratulations!!

Share: