Let’s do a Quick and Dirty Nginx Installation on Ubuntu Linux for personal Development Purpose with minimum effort.
It’s not always necessary to install the full blown Webserver + PHP + SQL Environment – Sometimes you only need a Webserver running PHP for doing some Development Tasks.
And here we are – This Tutorial doesn’t care about Security or anything special – it’s just about a fast Installation of Nginx and PHP.
Requirements
- Ubuntu Linux minimal installation
- System Permission to install Software
- Shell Access
Preparations
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nano
Nginx and PHP Installation
Install Nginx and php by typing:
sudo apt-get install nginx
sudo apt-get install php-fpm
Check the running PHP Version from the Socket File “phpX.X-fpm.sock” by typing:
sudo ls -l /var/run/php/*.sock
# Result:
# /var/run/php/php8.1-fpm.sock
You may have a different Version of PHP running. – Please note this for the upcoming Configuration.
Nginx Configuration
Edit the default Configuration to enable PHP Support:
sudo nano -w /etc/nginx/sites-available/default
Add “index.php” to the Index Block:
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
Remove the Dash (#) from the following Lines inside the PHP Block as shown in the example – Important: Change the Filenaming if necessary to your Version of “phpX.X-fpm.sock”
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
Press [CTRL]+[X] and [Y] to save the Changes.
Reload Nginx to enable PHP:
sudo systemctl reload nginx
Finish Line
At this Point you are already done – Your Webserver should be up and running – You can reach it by entering your Servers IP from any Browser in your local network.
http://server-ip
Optional: PHP Testing
Create a phpinfo.php File to check if everything is working as expected:
sudo nano -w /var/www/html/phpinfo.php
Add the following Content into the Editor:
<?php
phpinfo();
?>
Finally open your Browser pointing to the new created PHP File – You should see the PHP Info Page now:
http://server-ip/phpinfo.php
Hint – If you get an Error “502 Bad Gateway” – Check the File Naming / Version of “phpX.X-fpm.sock” and related entries in “/etc/nginx/sites-available/default”
Optional: Running PHP Code inside HTML Files
By default – PHP will not run in HTML Files on Nginx and PHP-FPM. To enable it – edit the nginx Config File:
sudo nano -w /etc/nginx/sites-available/default
Change the PHP Location Entry from: location ~ \.php$ {
to:
location ~ \.(php|html)$ {
Edit the php-fpm “www.conf” – Please change the Version Number depending on your Installation:
sudo nano -w /etc/php/8.1/fpm/pool.d/www.conf
Enable the block (remove the ” ; “and add the .html Extension (You’ll find it at the end of the Config):
security.limit_extensions = .php .php3 .php4 .php5 .php7 .htm .html
Save your changes and reload nginx and php-fpm Services:
sudo systemctl reload nginx
sudo systemctl reload php8.1-fpm
Again: Change the PHP-Version Number depending on your Installation at the php reload command.
You should now be able to serve php inside .html Files.