Can php run on nginx?

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.

  • Install NGINX and PHP
  • Check NGINX and PHP-FPM services
  • Setup NGINX with PHP
  • Testing NGINX with PHP
  • Conclusion

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.

Can php run on nginx?

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.

Can php run on nginx?

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

Can php run on nginx?

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.

What is NGINX PHP?

Nginx is pronounced as “Engine-X”, which is a web server and reverse proxy server. Nginx is well known for its speed and ability to handle large number of requests simultaneously with optimal use of resources. PHP-FPM stands for “PHP-FastCGI process manager”.

How does NGINX communicate with PHP?

NGINX web server (as reverse proxy) serves PHP applications through the FastCGI protocol (as a backend application server). NGINX employs PHP-FPM (FastCGI Process Manager), an alternative PHP FastCGI implementation that runs in the background as a daemon, listening for CGI requests.

Does NGINX require PHP

PHP-FPM is installed and active for NGINX. And that's it, you've got NGINX up and running with PHP-FPM support. Remember, when you build your virtualhost configuration files, you'll need to make sure to include PHP support in those. For that, you can use the /etc/nginx/sites-available/default file as an example.

How PHP

Now create an NGINX server block that will make use of the above FPM pool. To do that, edit your NGINX configuration file and pass the path of pool's socket file using the option fastcgi_pass inside location block for php. Make sure the above configuration setting is syntactically correct and restart NGINX.