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.
Prioritize index.php
Press CTRL
+ W
and search for index.html
Now add index.php
before index.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.
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/
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.
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.
You've completed this section, the Nginx webserver now integrates with PHP.
Last updated
Was this helpful?