Step-by-Step Guide
Connect to Your EC2 Instance
Connect to your EC2 instance using SSH:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-instance-public-dns
Remove Current PHP Version
First, you need to remove the currently installed PHP version. You can list the installed PHP packages and remove them:
sudo apt list --installed | grep php
Then remove the listed PHP packages (e.g., php8.1):
sudo apt-get purge 'php*'
sudo apt-get autoremove
Add PHP 7.3 Repository
You need to add the repository that contains PHP 7.3. The ondrej/php repository is commonly used for this purpose:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Install PHP 7.3
Now, install PHP 7.3 and the necessary extensions:
sudo apt-get install php7.3
sudo apt-get install php7.3-cli php7.3-fpm php7.3-mysql php7.3-xml php7.3-mbstring php7.3-curl php7.3-zip
Configure Apache or Nginx to Use PHP 7.3
If you are using Apache:
sudo a2enmod php7.3
sudo systemctl restart apache2
If you are using Nginx, you need to configure Nginx to use PHP 7.3 by editing the server block configuration file:
sudo nano /etc/nginx/sites-available/default
Update the fastcgi_pass directive to point to the PHP 7.3 FPM socket:
nginx
Copy code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Save the file and restart Nginx:
sudo systemctl restart nginx
sudo systemctl restart php7.3-fpm
Verify PHP Version
To verify that PHP 7.3 is installed and configured correctly, you can check the PHP version:
php -v
You can also create a phpinfo() file to check the PHP version through your web server:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Navigate to http://your-ec2-instance-public-dns/phpinfo.php in your web browser and verify that PHP 7.3 is being used.
0 More Answers