Server-side processing using php-fpm

I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.
Search for a command to run...

I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.
No comments yet. Be the first to comment.
In this basic level series, I will post best practices to configure the NGINX web server using Linux based instances.
Worker processes When we start the NGINX service, we get a master process which we can view using systemctl. This master process then spawns worker processes that are used to serve requests. The default is one worker process. We can change the no. of...
Hi readers, There are times when we make huge decisions in our lives. These may not be easy to make due to several reasons. Like the decision I made to quit DevOps completely. You may feel that I quit suddenly, but I took this decision years ago. It ...

Objectives Target the code at: https://github.com/melvincv/java-basic-practice-app Switch to main-v2 branch. Build the code using GitHub Actions. Deploy the code on two AWS EC2 servers. (independently) Implement approval process for deployment o...

with Sonarqube, Ansible and Blue / Green deployment

Jenkins Install Controller and Agent The controller is the main Jenkins instance that will co-ordinate the connections and tooling for the agents. Agents are the workhorse instances used to run jobs and pipelines. It can have various tools and unique...

Docker, Kubernetes and GitOps

PHP is a popular scripting language that is often used to create dynamic web pages. However, PHP can be resource-intensive, especially on high-traffic websites. This is where NGINX and php-fpm come in.
NGINX is a high-performance web server that can handle a lot of traffic. php-fpm is a PHP FastCGI implementation that can offload the processing of PHP scripts from NGINX. This can improve performance and reduce the load on the web server.
To use NGINX and php-fpm to process PHP scripts, you need to configure both servers.
Here is the command to install both nginx and php-fpm on Ubuntu or Debian:
sudo apt install nginx php-fpm
sudo systemctl list-units | grep php
To configure NGINX to use php-fpm, you need to add the following lines to your nginx.conf file:
location / {
fastcgi_pass unix:/var/run/php-fpm.sock;
}
This tells NGINX to pass all requests for PHP scripts to the php-fpm socket at /var/run/php-fpm.sock.
To configure php-fpm to listen on the Unix socket, you need to add the following lines to your php-fpm.conf file:
listen = /var/run/php-fpm.sock
This tells php-fpm to listen on the Unix socket at /var/run/php-fpm.sock.
Once you have configured NGINX and php-fpm, you need to restart both servers.
To restart NGINX, you can run the following command:
sudo service nginx restart
To restart php-fpm, you can run the following command:
sudo service php-fpm restart
Once both servers are restarted, you can test your configuration by creating a simple PHP script and accessing it through your web browser.
The following is a simple PHP script that you can use to test your configuration:
<?php
echo "Hello, world!";
?>
Save this file as index.php in the root directory of your web server. Then, access it through your web browser by going to the following URL:
" http://localhost/index.php "
If you see the message "Hello, world!", then your configuration is working correctly.
Using NGINX and php-fpm together can improve the performance of your PHP applications by offloading the processing of PHP scripts from the web server. This can be especially beneficial for high-traffic websites.
Look for these config:
index directive, index.php
location block
fastcgi is a protocol for transferring binary data
/etc/nginx/fastcgi.confpass to a UNIX socket, a file that listens for binary data.
server {
listen 80;
server_name 167.99.93.26;
root /sites/demo;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~\.php$ {
# Pass php requests to the php-fpm service (fastcgi)
include fastcgi.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
You may test the PHP processing by loading a PHP info file:
echo '<?php phpinfo(); ?>' > info.php
Open the file in the browser using http://URL/info.php
First, check the error logs:
tail -n 1 /var/log/nginx/error.log
Error: Permission denied while connecting to php-fpm socket.
Check the user with which the NGINX process and the php-fpm process runs:
ps aux | grep nginx
ps aux | grep php
See that the nginx and php-fpm processes run as the same user.
Add the user directive to the NGINX config to match the user of the php-fpm process:
user www-data;
Reload NGINX and check again.
How to Configure PHP-FPM with NGINX: https://www.digitalocean.com/community/tutorials/php-fpm-nginx
How to Setup PHP on Nginx with FastCGI (PHP-FPM) example: https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Nginx-PHP-FPM-config-example)