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

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

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

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 updates all packages to 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 extras, install Nginx 1.20 on 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 installed 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 start 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;
    rewrite ^ $scheme://$request_uri?;

server {
    listen 80;
    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;
        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


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

Set Up SSL Certificate in Amazon Linux 2

Setup SSL Certificate in Ubuntu

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

This Post Has 9 Comments

  1. Raj

    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?


  2. Farid

    Thanks! Worked like a charm!

  3. dafoot

    thought I’d give nginx a go on my latest server, thanks for the guide nice intro

    now to go find a couple of your ads to click on….

  4. Troubadix

    How can I change the user php-fpm is running with?
    I want it to run as the user the nginx vhost belongs to – so different user for every vhost

  5. Joseph


    I purchased a 3rd party SSL cert and I’m running NodeJS on AWS EC2 Linux. Can you show how to integrate the SSL with the NodeJS APP? I’m using nginx free SSL and it’s only valid for 3 months. Please help

Leave a Reply