Vishal

Posted on November 15th

How to install PHP-FPM with Nginx on Ubuntu

"How to install PHP-FPM with Nginx on Ubuntu"

What is Ubuntu?

Ubuntu is one of the GNU/Linux distributions based on Debian distribution which is mainly developed to supply OS for as many individuals as possible. This is one of the most favored and commonly used Linux distributions, as it can satisfy both new and experienced Linux users and professionals due to its simplicity, a great number of contributions from the users and high level of security.

Overview

  • Ubuntu operating system was introduced in October 2004 by a company established by the South African, Mark Shuttleworth known as the Canonical Ltd. Due to the fact that it relies on community and costs a reasonable amount of money, the name Ubuntu is derived from the languages of Africa and it means ‘humanity toward others’. Ubuntu is free to download, use, and share, and it comes in several flavors, each tailored to different use cases: Ubuntu is an open-source operating system and is available for download, use, and distribution for free, and the Ubuntu operating system has several distros, each being developed for a particular purpose.
  • Ubuntu Desktop: At least that was the original version designed for individual personal computers and laptops. It is a based OS and contains pre-installed application software including Firefox internet browser, Libre Office suite, and some multimedia applications.
  • Ubuntu Server: Development: Web server version for features developers need and work for web hosting, cloud computing or data center. It does not have a graphical user interface by default because it is intended for server tasks and optimized for performance.
  • Ubuntu Studio: A creative edition targeted towards artists and designers, allowing them to edit audio, video, stencils, and graphics in a single application.
  • Ubuntu Kylin: A version designed with the Chinese government’s endorsement and adjusted to Chinese conditions and preferences.

Features of Ubuntu

  1. User-Friendly Interface: The underlying GUI environment of Ubuntu has always been based on GNOME, although Ubuntu switched from its in-house developed Unity in 2017. One of the best things about Ubuntu is that it provides a bright and clear interface, which provides an opportunity for an average user who doesn’t want to deal deeply with Linux.
  2. Software and Package Management: It utilizes the APT (Advanced Package Tool) as the package manager, which makes it very convenient for the installation, updating, and removal of software. It also supports a special kind of package called “snap” packages, which are applications that contain all necessary dependencies.
  3. Security: One of Ubuntu’s strengths is the security of this operating system. It emphasizes security with features such as updates, LTS versions, and security features like firewalls and encryptors.
  4. Community and Support: Ubuntu has a powerful and dynamic community base that participates in the development of its new versions and also helps in case of problems a user may face. It has professional support services, thus making it suitable for businesses and enterprises to obtain their products from Canonical.
  5. Customization: Ubuntu is extremely configurable. When implemented, it provides an exceptionally high degree of customization. Users can select various graphical interfaces, adjust the configuration, and change the look of the operating system.
  6. Performance: Ubuntu is very friendly to all types of systems, ranging from older to current systems. It is especially known for its stability and, more importantly, operational performance, which qualifies it for both desktop and server use.

What is Nginx?

One of the basic units of the internet is the web server. These web servers are computers that deliver requested web pages. To make your computer a web server, make sure you download anyone, e.g.:- Nginx, Apache, Tornado, Caddy, or Microsoft Internet Information Services (IIS). In this tutorial, we will learn about Nginx (What is Nginx, how to install it, and how it works)

Nginx is an open-source web server software, that is used for reverse proxy, it provides HTTPS server which is mainly made for handling maximum performance and stability

The NGINX Architecture

NGINX uses an advanced event-based mechanism in many operating systems. Its process highly efficiently runs loops in a single-thread process called workers

Workers accept requests from a shared listen socket and execute highly efficient run loops inside each worker to process thousands of request

  • Workers receive requests from a commonly shared listening socket and use highly efficient run loops to handle thousands of requests internally.
  • Masters authenticate configurations through the establishment, linking, and interconnection of sockets. They are also responsible for initiating, terminating, and regulating the quantity of designated worker processes. Additionally, the master node possesses the capability to adjust worker processes without causing any disruption to services.
  • Proxy caches represent unique entities equipped with both a cache loader and a manager. The cache loader examines items within the disk cache and updates the in-memory database of the engine with metadata retrieved from the cache. It readies NGINX instances to interact with files already housed on the disk in a meticulously organized format. Meanwhile, the cache manager oversees the processes of cache expiry and invalidation.

Benefits of NGINX

High Performance NGINX provide us high performance to handle thousand of request with minimal resource consumption \* \*\*Scaling:\*\* NGINX add more server to handle increased traffic. it act like a load balancer to distribute

Prerequisites

Before we start make sure you have a computer and server with good internet access, basic knowledge of the terminal and command how it work, and one last and important thing you have access to install software

Install Nginx

\### Step 1: Update package list

In many linix distributer, they provide nginx by default, so you can download from there package manager and in the case of Ubuntu it provide also by defaultdnableisable

For downloading nginix in your machine first of all open the terminal and run this command to update your machine

sudo apt update

Step 2: Install Nginx

After updating the machine we are ready to install Nginx, to install nginx run this command in your terminal

sudo apt install nginx

Step 3: Install PHP and PHP-FPM

Install PHP and PHP-FPM along with common PHP extensions:

sudo apt install php-fpm php-mysql -y

This command installs PHP-FPM (php-fpm) and the PHP MySQL extension (php-mysql). You can install other extensions as needed.

Step 4: Configure Nginx to Use PHP-FPM

You need to configure Nginx to use PHP-FPM. Open the default server block configuration file in a text editor:

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

Find the location ~ \.php$ { block and make sure it looks like the following:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Make sure to change fastcgi_pass to the correct path for the PHP-FPM socket. The path usually is:

fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;

Replace php8.2 with your PHP version if it's different.

Step 5: Test Nginx Configuration

After editing the configuration, test it to make sure there are no syntax errors:

sudo nginx -t

If the output says the syntax is OK, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Create a PHP Test File

To verify that PHP is working with Nginx, create a PHP file in the web root:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, open your web browser and visit http://your-server-ip/info.php. You should see the PHP information page, confirming that PHP-FPM works correctly with Nginx.

Step 7: Secure Your Installation

After confirming that PHP is working, remove the info.php file to prevent unauthorized access:

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

Step 8: Enable and Start PHP-FPM

Ensure PHP-FPM is enabled and running:

sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm

That's it!

You have successfully installed PHP-FPM with Nginx on Ubuntu! You can now configure your server to host your PHP-based applications.

Comments

Leave a comment.

Share your thoughts or ask a question to be added in the loop.