đź“‹
Install Nginx, MariaDB, PHP & phpMyAdmin
  • Get Started
  • Install Nginx
  • *Install MariaDB
  • Install MySQL
  • Install PHP
  • Configure Nginx for PHP
  • Test PHP
  • Install phpMyAdmin
  • Update root Password
  • Reference
Powered by GitBook
On this page
  • Prioritize index.php
  • Server Name
  • PHP Socket
  • Save and Test

Was this helpful?

Configure Nginx for PHP

We now need to make some changes to our Nginx server block.

The location of the server block may vary depending on your setup. By default, it is located in /etc/nginx/sites-available/default.

Edit the file in nano.

sudo nano /etc/nginx/sites-available/default

Prioritize index.php

Press CTRL + W and search for index.html

Now add index.php before index.html

In file: /etc/nginx/sites-available/default
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

Server Name

Press CTRL + W and search for the line server_name

Enter your server’s IP here or domain name if you have one.

In file: /etc/nginx/sites-available/default
server_name YOUR_DOMAIN_OR_IP_HERE;

PHP Socket

Press CTRL + W and search for the line location ~ \.php.

You will need to uncomment some lines here by removing the # signs before the four lines marked in red below.

Also ensure value for fastcgi_pass socket path is correct. For example, if you installed PHP version 7.4, the socket should be: /var/run/php/php7.4-fpm.sock

If you are unsure which socket to use here, exit out of nano and run ls /var/run/php/

In file: /etc/nginx/sites-available/default
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
}

Make sure to remove the # sign before the closing bracket } in above.

Save and Test

Once you’ve made the necessary changes, save and close (Press CTRL + X, then press Y and ENTER to confirm save)

Now check the config file to make sure there are no syntax errors.

sudo nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

If no errors, you can reload the Nginx config.

sudo service nginx reload

You've completed this section, the Nginx webserver now integrates with PHP.

PreviousInstall PHPNextTest PHP

Last updated 1 year ago

Was this helpful?