So, if you're planning to setup NGINX to run a PHP based CMS or simply want to serve some dynamic web pages, you need to add PHP support first.
This could be done either manually or via some automated scripts.
We'll go the manually way this time, as it keeps the server neat and number of installed software packages minimal.
Also, doing it manually gives us a a great insight about what's actually going behind the scene.
Contents
Install NGINX and PHP
So, this is the very first step, you need to install the software packages.
Depending on the type of server you're using, this step could be different. But for example, I'll be using an Ubuntu server, running on a Vultr VPS instance.
So, after logging in to the server via SSH, update and upgrade the system.
sudo apt-get update && apt-get upgrade -y
Next you need to decide from where you want to install PHP, some PPA or the default packages from Ubuntu repository.
For the sake of simplisity, I'll be using the default packages available in Ubuntu repository.
sudo apt install nginx php7.4-fpm php7.4
You may also need to install some extra PHP modules, like php7.3-json or whatever that particular application requires to run properly.
Check NGINX and PHP-FPM services
After installing, the php-fpm and nginx service should be running.
To check that use the systemctl comand.
sudo systemctl status nginx.service # Check NGINX status sudo systemctl status php7.4-fpm.service # Check php-fpm status
Your installed PHP version could be different, edit the second command as appropriate.
If any of these serive is not running, use the systemctl start option to start them. But also keep in mind that these services won't start with any improper configuration.
At this point, NGINX should be serving the default index.html file, to be sure about that, open up any web browser, and go to your server's IP address, it should show the default Welcome page.
Setup NGINX with PHP
To execute PHP codes and serve dynamic content, you need to setup a proper nginx php configuration.
First you've to know how the PHP-FPM service is running, either it's binding a TCP port or to a UNIX socket.
If the php-fpm UNIX socket exists, then it's running and you're good to go. Use the command below to confirm.
ls /var/run/php/php*-fpm.sock
Also, as advised by experts, you should use PHP-FPM via a UNIX socket for better performance and security.
Next you need to edit the NGINX configuration. Here again, you have two different options. Ether edit the default configuration according to your need or create a new NGINX configuration.
Here I'll be editing the default configuration using the nano text editor.
It's a pretty long text file, with less some options commented out. To know exactly which options are enabled, use this command.
grep -v '#' /etc/nginx/sites-available/default
From here, you can get a rough overview of the configuration, which is somewhat confusing at first, to be honest.
Here's a sample configuration you can modify and use a a base template.
server { listen 80 default_server; listen [::]:80 default_server; server_name myservername.com; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } }
Now, don't just copy-paste this configuration, edit the configuration file as required.
After reaching to a point when you think the NGINX configuration is good enough for your desired purpose, save the file and restart the server with sudo systemctl restart nginx.service
.
Learning about each configuration parameters requires some studying, and testing through trial and error.
To test your NGINX with PHP configuration, use the sudo nginx -t command, if there's any error, note it and edit the configuration file again.
Actually you can host more than one site on a single server, we'll talk about that later. Also note that this server is not configured with SSL, i.e. https, we'll also talk about that later.
Testing NGINX with PHP
After setting up the server, you can create a PHP file inside the document root directory, which is /var/www/html
in this case.
Let's say the sample PHP file is hello-world.php.
<?php echo "Hello, world!"; ?>
To actually test it, open up a web browser and go to the URL or IP addresslike this. > myservername.com/hello-world.php
If the that PHP page is working, that means you've sucessfully installed NGINX with PHP support.
Next thing should be how to integrate the NGINX server with a MySQL database , running either locally or on another dedicated database server remotely.
Conclusion
It's a pretty comprehensive tutorial about NGINX, here's lot more to learn.
As example, configuring virtual hosts with NGINX, setting up SSL certificates to serve pages securely, setting up proxy server etc. etc.
If you have any question, leave comments below.
Leave a Reply