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.
Ravindra Bagale's Tip – मराठी
Amazon Linux वर PHP install केला पण php-fpm start केला नाही – मग Nginx "502 Bad Gateway" देतो आणि students गोंधळतात. 502 म्हणजे Nginx ला मागचा (PHP-FPM) program सापडला नाही. sudo service php-fpm status आधी बघा.
Ravindra Bagale's Tip – हिंदी
Amazon Linux पर PHP install किया पर php-fpm start नहीं किया – फिर Nginx "502 Bad Gateway" देता है और students उलझ जाते हैं. 502 का मतलब है Nginx को पीछे वाला program (PHP-FPM) नहीं मिला. पहले sudo service php-fpm status देखो.
Practice task
Install PHP on both instances and run php -v and php -m | grep -i mysql to confirm the MySQL extension is loaded.