How configure php nginx ubuntu?

In this guide we will install and configure PHP to work with Nginx on Ubuntu 20.04 (Focal Fossa)

1. Install PHP

Unlike Apache, Nginx does not contain native PHP processing. For that we have to install PHP-FPM (FastCGI Process Manager). FPM is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

You should add Ubuntu’s universe repository before installing php-fpm to ensure you get the correct package.

sudo add-apt-repository universe

Now update the package lists and install PHP-FPM on Ubuntu 20.04. We will also install php-mysql to allow PHP to communicate with the MySQL database. Press y and ENTER when asked to continue.

sudo apt update && sudo apt install php-fpm

If you intend on using MySQL, you should also install the php-mysql package.

sudo apt install php-mysql

Once installed, check the PHP version.

If PHP was installed correctly, you should see something similar to below.

PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Above we are using PHP version 7.4, though this may be a later version for you depending on which package was in the repository.

Depending on what version of Nginx and PHP you install, you may need to manually configure the location of the PHP socket that Nginx will connect to.

List the contents for the directory /var/run/php/

You should see a few entries here.

total 4
lrwxrwxrwx 1 root     root     30 Apr 27 23:28 php-fpm.sock -> /etc/alternatives/php-fpm.sock
-rw-r--r-- 1 root     root      5 Apr 27 23:28 php7.4-fpm.pid
srw-rw---- 1 www-data www-data  0 Apr 27 23:28 php7.4-fpm.sock

Above we can see the socket is called php7.4-fpm.sock. Remember this as you may need it for the next step.

2. 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. If you followed one of our previous guides on setting up Nginx with multiple domains, your config file may be located somewhere like  /etc/nginx/sites-available/example.com.

Edit the file in nano.

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

2.1. Prioritize index.php

Press CTRL + W and search for index.html.

Now add index.php before index.html

/etc/nginx/sites-available/default

    index index.php index.html index.htm index.nginx-debian.html;

2.2. 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.

/etc/nginx/sites-available/default

        server_name YOUR_DOMAIN_OR_IP_HERE;

2.3. PHP Socket

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

You will need to uncomment some lines here by removing the &lt;code&gt;#&lt;/code&gt;&amp;nbsp;signs before the four lines marked in red below.&lt;/p&gt;&lt;p&gt;Also ensure value for&amp;nbsp;&lt;code&gt;fastcgi_pass&lt;/code&gt;&amp;nbsp;socket&amp;nbsp;path is correct. For example, if you installed&amp;nbsp;PHP version 7.4, the socket should be:&amp;nbsp;&lt;code&gt;/var/run/php/php7.4-fpm.sock&lt;/code&gt;&lt;/p&gt;&lt;p&gt;If you are unsure which socket to use here, exit out of nano and run &lt;code&gt;ls /var/run/php/&lt;/code&gt;&lt;/p&gt;&lt;div class="code-wrap"&gt;&lt;div class="pre-label"&gt;/etc/nginx/sites-available/default&lt;/div&gt;&lt;pre id="supercoder-block_5ea7801709f3d" class="code pre-only"&gt;&lt;code class="pre-only"&gt; &lt;span class="red"&gt;location ~ \.php$ { include snippets/fastcgi-php.conf;&lt;/span&gt; # # # With php-fpm (or other unix sockets): &lt;span class="red"&gt;fastcgi_pass unix:/var/run/php/&lt;strong&gt;php7.4-fpm.sock&lt;/strong&gt;;&lt;/span&gt; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; &lt;span class="red"&gt;} &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="btn-copy-wrapper"&gt;&lt;button class="btn-copy" title="Copy"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Make sure to remove the &lt;code&gt;#&lt;/code&gt; sign before the closing bracket &lt;code&gt;}&lt;/code&gt; in red above.&lt;/p&gt;&lt;a class="anchor" id="id-2-4-save-and-test"&gt;&lt;/a&gt;&lt;h3&gt;2.4. Save and Test&lt;/h3&gt;&lt;p&gt;Once you’ve made the necessary changes, save and close (Press&amp;nbsp;&lt;code&gt;CTRL&lt;/code&gt;&amp;nbsp;+&amp;nbsp;&lt;code&gt;X&lt;/code&gt;, then press&amp;nbsp;&lt;code&gt;y&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code&gt;ENTER&lt;/code&gt;&amp;nbsp;to confirm save)&lt;/p&gt;&lt;p&gt;Now check the config file to make sure there are no syntax errors.&amp;nbsp;&lt;/p&gt;&lt;div class="code-wrap"&gt;&lt;pre id="supercoder-block_5ea7826909f3e" class="code language-bash"&gt;&lt;code class="language-bash"&gt;sudo nginx -t&lt;/code&gt;&lt;/pre&gt;&lt;div class="btn-copy-wrapper"&gt;&lt;button class="btn-copy" title="Copy"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Output:&lt;/p&gt;&lt;div class="code-wrap"&gt;&lt;pre id="supercoder-block_5ea7827d09f3f" class="code pre-only"&gt;&lt;code class="pre-only"&gt;nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful&lt;/code&gt;&lt;/pre&gt;&lt;div class="btn-copy-wrapper"&gt;&lt;button class="btn-copy" title="Copy"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;span id="ezoic-pub-ad-placeholder-131" class="ezoic-adpicker-ad"&gt;&lt;/span&gt;&lt;span class="ezoic-ad ezoic-at-0 large-mobile-banner-1 large-mobile-banner-1131 adtester-container adtester-container-131" data-ez-name="devanswers_co-large-mobile-banner-1"&gt;&lt;span id="div-gpt-ad-devanswers_co-large-mobile-banner-1-0" ezaw="300" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:300px" class="ezoic-ad"&gt;&lt;script data-ezscrex="false" data-cfasync="false" style="display:none"&gt;if(typeof ez_ad_units!='undefined'){ez_ad_units.push([[300,250],'devanswers_co-large-mobile-banner-1','ezslot_16',131,'0','0'])};__ez_fad_position('div-gpt-ad-devanswers_co-large-mobile-banner-1-0');If no errors, you can reload the Nginx config.</p><div class="code-wrap"><pre id="supercoder-block_5ea7828d09f40" class="code language-bash"><code class="language-bash">sudo service nginx reload</code></pre><div class="btn-copy-wrapper"><button class="btn-copy" title="Copy"></button></div></div><span class="ezoic-autoinsert-ad ezoic-mid_content"></span><span style="clear:both;display:block" id="ez-clearholder-box-4"></span><span class="ezoic-ad ezoic-at-0 box-4 box-4260 adtester-container adtester-container-260" data-ez-name="devanswers_co-box-4"><span id="div-gpt-ad-devanswers_co-box-4-0" ezaw="250" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;width:100%;max-width:1200px;margin-left:auto !important;margin-right:auto!important;min-height:90px;min-width:728px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" style="display:none">if(typeof ez_ad_units!='undefined'){ez_ad_units.push([[250,250],'devanswers_co-box-4','ezslot_17',260,'0','0'])};__ez_fad_position('div-gpt-ad-devanswers_co-box-4-0');

3. Test PHP

To see if PHP is working correctly on Ubuntu 20.04, create a new PHP file called info.php in the document root directory. By default, this is located in /var/www/html/, or if you set up multiple domains in a previous guide, it may be located in somewhere like /var/www/mytest1.com/public_html

Once you have the correct document root directory, use the nano text editor to create a new file info.php

sudo nano /var/www/html/info.php

Type or paste the following code into the new file.

/var/www/html/info.php

<?php
phpinfo();

Save and exit (press CTRL + X, press Y and then press ENTER)

You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php: http://your_domain_or_IP/info.php

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

phpinfo() outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version and server information.

How configure php nginx ubuntu?

You have now successfully installed PHP-FPM for Nginx on Ubuntu 20.04.

Make sure to delete info.php as it contains information about the web server that could be useful to attackers.

sudo rm /var/www/html/info.php

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 200% using AI 🤖. Read my Ezoic review to find out how.

What is Linux Nginx PHP?

This is an acronym that describes a Linux operating system, with an Nginx (pronounced like “Engine-X”) web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.

What is PHP

PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script.

Does Nginx include PHP?

Nginx does not contain native PHP processing. So you will need to install PHP-FPM for processing the PHP files. FPM is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

What is FastCGI Nginx?

Definition of Nginx FastCGI. Nginx fastcgi is used to translate requests of clients from an application server which was not handled the request of the client directly. Nginx FastCGI is the protocol that was based on the CGI which is earlier or it will contain the gateway of a common interface.