Ravindra BagaleCourses & study guides

9. PHP, LAMP and LEMP Step by Step

9.2 Installing PHP on Amazon Linux 2023 and Ubuntu

OS + web server Install command PHP handler
AL2023 + Nginx or Apache sudo yum install -​y php php-​fpm php-​mysqlnd php-​cli PHP-FPM socket /​run/​php-​fpm/​www.​sock
CentOS Stream 9 + Nginx or Apache sudo yum install -​y php php-​fpm php-​mysqlnd php-​cli PHP-FPM socket /​run/​php-​fpm/​www.​sock
Ubuntu + Nginx sudo apt install -​y php-​fpm php-​mysql PHP-FPM socket /​run/​php/​php8.​x-​fpm.​sock
Ubuntu + Apache sudo apt install -​y php libapache2-​mod-​php php-​mysql Apache mod_php (no FPM needed)

On AL2023 / CentOS, start PHP-FPM and restart the web server:

sudo service php-fpm start
sudo systemctl enable php-fpm
sudo service nginx restart        # or: sudo service httpd restart
php -v

On Amazon Linux 2023 and CentOS, Apache is automatically wired to PHP-FPM by /etc/httpd/conf.d/php.conf, and the default Nginx server is wired by /etc/nginx/default.d/php.conf. For your own Nginx server block, add the PHP location shown below.

Ravindra Bagale's Tip

You installed PHP on Amazon Linux but didn't start php-fpm – then Nginx gives "502 Bad Gateway" and students get confused. 502 means Nginx couldn't find the program behind it (PHP-FPM). Check sudo service php-fpm status first.

Practice task

Install PHP on both instances and run php -v and php -m | grep -i mysql to confirm the MySQL extension is loaded.