Your Essential Queries
How To Install Nginx PHP
We'll See How To Install & Configure Nginx And PHP In Linux Easily:
1) Open Terminal
2) Update packages:
- sudo apt update && sudo apt upgrade
2) Install Nginx:
- sudo apt install nginx
3) Enable Firewall:
- sudo ufw enable
4) Allow Nginx To Firewall
- sudo ufw allow 'Nginx HTTP'
5) Restart Firewall:
- 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)
- 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
- sudo nano /etc/nginx/sites-available/localhost
8) Paste the code
- server {
- listen 80;
- server_name _;
- root /var/www/html;
- index index.php index.html index.htm;
- location / {
- #try_files $uri $uri/ =404;
- autoindex on;
- }
- location ~ \.php$ {
- include snippets/fastcgi-php.conf;
- fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
- }
- location ~ /\.ht {
- deny all;
- }
- }
To Save :
press CTRL+x
press y
press Enter
9) Create a test php file inside /var/www/html
- cd /var/www/html && nano index.php
10) Put php code inside index.php
- <?php
- phpinfo();
- ?>
To Save :
press CTRL+x
press y
press Enter
11) Remove Nginx default configuration file
- sudo rm -rf /etc/nginx/sites-enabled/default
12) Link new configuration file
- sudo ln -s /etc/nginx/sites-available/localhost /etc/nginx/sites-enabled/
13) Restart Nginx Server
- sudo systemctl restart nginx
Now reload http://localhost again. If everything was successfull you'll see a php information page
Congratulations!!