How to Setup PHP-FPM (PHP 8) Nginx in Amazon Linux 2 (updated June 2021 )

This article will tell you the exact steps to set up PHP with Nginx in Amazon Linux 2, one of the most secured servers operating systems available in 2021.

What is Nginx

According to Wiki, Nginx, stylized as NGINX or NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license. In my opinion, Nginx is much faster than its competitor Apache and runs on different varieties of platforms like Windows / Linux / Mac. PHP / Nginx is a great combination that provides super-fast performance for WordPress / Laravel or any custom PHP frameworks. Those who once get used to Nginx, never come back to Apache again.

How to Finish the Installation

First of all set up an EC2 instance in AWS by following my article. Then connect to the instance using Putty or Terminal based on your operating system. After finishing these steps then run this command to complete the setup.

At first, we need to update the YUM package. “sudo yum update -y“. Using this command we are updating YUM. YUM (Yellowdog Updater Modified) is an open-source command-line and a graphical-based package management tool for RPM (RedHat Package Manager) based Linux systems. It allows users and system administrators to easily install, update, remove, or search software packages on a system. So sudo yum update -y basically updates all packages to its the latest version from a repository list which is managed by AWS. This is always a good practice to run “yum update” first before you install any software.

sudo yum update -y

After that, using amazon-linux extas, install Nginx 1.20 into your server.

sudo amazon-linux-extras install nginx1

Once you have installed the Nginx server, then you need to install PHP using amazon-linux-extras. In this tutorial, I have installed php7.2. But if you want to install a different version then please change this to “php7.3” or “php7.4” or “php8.0”, which will install PHP 7.3 / PHP 7.4 / PHP 8.0 respectively.

sudo amazon-linux-extras enable php8.0

After we install Nginx and PHP, now we need to install additional PHP extensions. Here I am going to install mbstring and XML extension. But if you need additional extensions then you can add those at the end by concatenating with space.

yum clean metadata

sudo yum install php php-cli php-mysqlnd php-pdo php-common php-fpm -y

sudo yum install php-gd php-mbstring php-xml php-dom php-intl php-simplexml -y

Last but not least, start the Nginx server by running the following command.

sudo systemctl start nginx 

This is optional, which is setting Nginx to start when the system reboots.

sudo systemctl enable nginx

After that, start PHP-FPM

sudo systemctl start php-fpm

Once you started the PHP-FPM, then enable it so that it auto restarts after a system reboot

sudo systemctl enable php-fpm

Now set up a server block in Nginx for PHP FPM. I have a detailed article written on the Nginx Server block here.

sudo vi /etc/nginx/conf.d/gcptips.conf
server {
    listen 80;
    server_name www.gcptips.com;
    rewrite ^ $scheme://gcptips.com$request_uri?;
}

server {
    listen 80;
    server_name gcptips.com;
    
    root /var/www/wordpress;
    index index.php;
    charset UTF-8;
    
    

    location ~ \.php$ {
        try_files $uri =404;

        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        # fastcgi_intercept_errors on;
        # fastcgi_keep_conn on;
        # fastcgi_read_timeout 300;

        # fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass  unix:/var/run/php-fpm/www.sock;
        #for ubuntu unix:/var/run/php/php8.0-fpm.sock;

        ##
        # FastCGI cache config
        ##

        # fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:10m max_size=1000m inactive=60m;
        # fastcgi_cache_key $scheme$host$request_uri$request_method;
        # fastcgi_cache_use_stale updating error timeout invalid_header http_500;        
        
        fastcgi_cache_valid any 30m;
    }
}

Restart the Nginx for the change to take effect

sudo systemctl restart nginx 

Need to set up SSL? Please check out my other article on setting up SSL on Amazon Linux 2 here.

If you have reached so far then you have successfully installed PHP with Nginx in Amazon Linux 2.

This will help you to install Nginx 1.12 with PHP 8.0

Here are two videos that I created to show you the full process

Conclusion

I hope this will help you to get started with AWS and you can continue further with your dream project. Feel free to check out my other articles related to DevOps / Docker or Linux.

Related Articles

Nginx server block

My Affiliated products

3 thoughts on “How to Setup PHP-FPM (PHP 8) Nginx in Amazon Linux 2 (updated June 2021 )”

  1. Hi there,

    I followed the above steps and still cant load nginx webpage for some reason. I been trying for last 3 days. I dont why webpage is not working! My security group has been changed too. I done everything but still not working! Can you please help me?

    Thanks

  2. Pingback: Setup WordPress with Nginx and PHP-FPM | SEO One Page

Leave a Comment

Your email address will not be published. Required fields are marked *