In this guide we will install and configure PHP to work with Nginx on Ubuntu 20.04 (Focal Fossa) Show 1. Install PHPUnlike 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.
Now update the package lists and install PHP-FPM on Ubuntu 20.04. We will also install
If you intend on using MySQL, you should also install the
Once installed, check the PHP version. If PHP was installed correctly, you should see something similar to below.
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 You should see a few entries here.
Above we can see the socket is called 2. Configure Nginx for PHPWe 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 Edit the file in nano.
2.1. Prioritize index.phpPress Now add /etc/nginx/sites-available/default
2.2. Server NamePress Enter your server’s IP here or domain name if you have one. /etc/nginx/sites-available/default
2.3. PHP SocketPress You will need to uncomment some lines here by removing the <code>#</code>&nbsp;signs before the four lines marked in red below.</p><p>Also ensure value for&nbsp;<code>fastcgi_pass</code>&nbsp;socket&nbsp;path is correct. For example, if you installed&nbsp;PHP version 7.4, the socket should be:&nbsp;<code>/var/run/php/php7.4-fpm.sock</code></p><p>If you are unsure which socket to use here, exit out of nano and run <code>ls /var/run/php/</code></p><div class="code-wrap"><div class="pre-label">/etc/nginx/sites-available/default</div><pre id="supercoder-block_5ea7801709f3d" class="code pre-only"><code class="pre-only"> <span class="red">location ~ \.php$ { include snippets/fastcgi-php.conf;</span> # # # With php-fpm (or other unix sockets): <span class="red">fastcgi_pass unix:/var/run/php/<strong>php7.4-fpm.sock</strong>;</span> # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; <span class="red">} </span></code></pre><div class="btn-copy-wrapper"><button class="btn-copy" title="Copy"></button></div></div><p>Make sure to remove the <code>#</code> sign before the closing bracket <code>}</code> in red above.</p><a class="anchor" id="id-2-4-save-and-test"></a><h3>2.4. Save and Test</h3><p>Once you’ve made the necessary changes, save and close (Press&nbsp;<code>CTRL</code>&nbsp;+&nbsp;<code>X</code>, then press&nbsp;<code>y</code>&nbsp;and&nbsp;<code>ENTER</code>&nbsp;to confirm save)</p><p>Now check the config file to make sure there are no syntax errors.&nbsp;</p><div class="code-wrap"><pre id="supercoder-block_5ea7826909f3e" class="code language-bash"><code class="language-bash">sudo nginx -t</code></pre><div class="btn-copy-wrapper"><button class="btn-copy" title="Copy"></button></div></div><p>Output:</p><div class="code-wrap"><pre id="supercoder-block_5ea7827d09f3f" class="code pre-only"><code class="pre-only">nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful</code></pre><div class="btn-copy-wrapper"><button class="btn-copy" title="Copy"></button></div></div><p><span id="ezoic-pub-ad-placeholder-131" class="ezoic-adpicker-ad"></span><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"><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"><script data-ezscrex="false" data-cfasync="false" style="display:none">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 PHPTo see if PHP is working correctly on Ubuntu 20.04, create a new PHP file called Once you have the correct document root directory, use the
Type or paste the following code into the new file. /var/www/html/info.php
Save and exit (press 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:
You have now successfully installed PHP-FPM for Nginx on Ubuntu 20.04. Make sure to delete
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 PHPPHP-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.
|