It’s important to keep your version of PHP updated on your web server in order to get the best performance, minimize memory use, and for the best security.
In this article, I’ll describe how I updated from PHP 5.3 to PHP5.4, but the process should work for more recent versions.
Note this will require command line access. There is no Amazon control panel to do it.
How to Do It
If you Google around on how to upgrade PHP on your Amazon AMI, you’ll find this page which describes the commands to update httpd and php.
But, the problem is that issuing these commands will blow away all of your HTTPD settings and PHP modules, so don’t do it just yet!! You will be left with a server that doesn’t work, as far as hosting your website is concerned.
I would categorize this type of server upgrade as “major surgery” (at least to people who are not Linux experts, myself included). Therefore, you are going to want to take some major precautions before starting.
First of all, take a snapshot or image of your live Amazon EC2 Instance and make sure you know how to restore it.
Second, make a backup of the websites hosted on this server, just in case. Store these offline.
Third, store a local copy of your Virtual Host settings, as your httpd.conf files will get blown away during the upgrade. These are usually located at /etc/httpd/conf and /etc/httpd/conf.d. Search for “<VirtualHost” and copy the settings inside of the tags. The will look something like this:
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/var/www/html/website1_folder" ServerName yourdomain.com ErrorLog "logs/yourdomain.com-error_log" CustomLog "logs/yourdomain.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/var/www/html/website2_folder" ServerName subdomain.yourdomain.com ErrorLog "logs/yourdomain.com-error_log" CustomLog "logs/yourdomain.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/var/www/html/website3_folder" ServerName anotherdomain.com ErrorLog "logs/yourdomain.com-error_log" CustomLog "logs/yourdomain.com-access_log" common </VirtualHost>
ssh into your instance and copy these settings to your local computer. (Note, you do not need to copy “NameVirtualHost *.80”. That setting is not used in the new version.)
Doing the Upgrade
Okay, we are finally ready to start. Any websites hosted on this server will experience some downtime starting now, so do this when your traffic is low, and when you have ample time to debug any issues that come up (i.e., don’t go out to lunch during this process).
Just like the article in the Amazon forum says, go ahead and issue the first command to blow away your current httpd and php:
] sudo yum remove httpd* php*
The output will look something like this:
Package Arch Version Repository Size
httpd x86_64 2.2.29-1.4.amzn1 @amzn-updates 3.0 M
httpd-tools x86_64 2.2.29-1.4.amzn1 @amzn-updates 135 k
php x86_64 5.3.29-1.7.amzn1 @amzn-updates 7.4 M
php-bcmath x86_64 5.3.29-1.7.amzn1 @amzn-main 62 k
php-cli x86_64 5.3.29-1.7.amzn1 @amzn-updates 6.4 M
php-common x86_64 5.3.29-1.7.amzn1 @amzn-updates 6.3 M
php-devel x86_64 5.3.29-1.7.amzn1 @amzn-updates 9.9 M
php-gd x86_64 5.3.29-1.7.amzn1 @amzn-main 669 k
php-mbstring x86_64 5.3.29-1.7.amzn1 @amzn-updates 4.1 M
php-mcrypt x86_64 5.3.29-1.7.amzn1 @amzn-main 92 k
php-mysql x86_64 5.3.29-1.7.amzn1 @amzn-updates 445 k
php-pdo x86_64 5.3.29-1.7.amzn1 @amzn-updates 381 k
php-php-gettext noarch 1.0.11-3.el6 @epel 57 k
php-tcpdf noarch 6.0.091-1.el6 @epel 11 M
php-tcpdf-dejavu-sans-fonts noarch 6.0.091-1.el6 @epel 1.5 M
php-tidy x86_64 5.3.29-1.7.amzn1 @amzn-main 100 k
php-xml x86_64 5.3.29-1.7.amzn1 @amzn-main 625 k
phpMyAdmin noarch 22.214.171.124-1.el6 @epel 19 M
Remove 18 Packages
Before you say “yes” to actually do it though, copy the output which says what modules you have installed. The reason you want to copy this is in case something doesn’t work after the update. You then have a list of a modules that you had installed before, and you can re-install any missing ones.
Next, issue the command to install the new httpd and php:
] sudo yum install httpd24 php54
(For PHP5.5, use php55 in this line and from now on). Next, issue this command to install MySQL:
] yum install php54-mysql
Note that when you install php modules from now on, you’ll have to use “php54” as the prefix instead of “php”. For example, if you want to install the php mbstring module, you’d have to do this:
] yum install php54-mbstring
] yum install php-mbstring
If there are other PHP modules that you know you need, install them in this way. I’ll explain how to install phpMyAdmin later, though.
Restoring Your Settings
Next, you’ll have to enter those httpd.conf settings that you saved previously. I chose to create a new file called “brian.conf” inside my /etc/httpd/conf.d directory and put my VirutalHost settings inside there. All of the “.conf” files in this directory are read, so you can name it anything you want with the “.conf” suffix.
Alternatively, you can add the settings inside /etc/httpd/conf/httpd.conf.
If you want, you can issue a “sudo service httpd restart” at this point, and you should see the front page of your website in the browser. However, mod_rewrite won’t be working yet, so if your site is a WordPress site, none of the other pages will work.
To get mod_rewrite working (i.e., your .htaccess) you have to tweak a setting in /etc/httpd/conf/httpd.conf. Search for this tag:
Inside it, change the “AllowOverride” Setting to “AllowOverride All”. In my file, it was on line 151, but that will vary.
Note, if your web files are in a different directory than /var/www/html, you’ll have to find the settings for that directory instead.
Save the file and issue a “sudo service httpd restart”. Voila! Your site should be working now!
If still not working, check out this Stack Overflow answer.
If you are curious to see what modules are installed at this point, issue this command:
] yum list installed | grep "php"
If you are using phpMyAdmin, then issue this command to install it:
] yum --enablerepo=epel install phpmyadmin
Depending on how your phpMyAdmin was set up, you might have to update some settings. In my case, it just worked after I did a “sudo service httpd restart”.
Enabling Auto Start
Okay, everything should be working now except for one thing. If you were to reboot your instance, the service would not start up automatically, which is not good. Issue these commands to make httpd and mysqld start up automatically upon bootup:
] sudo /sbin/chkconfig httpd on
] sudo /sbin/chkconfig mysqld on
If you haven’t already, start the services:
] sudo /sbin/service httpd start
] sudo /sbin/service mysqld start
It’s probably a good idea to reboot your instance at this point to make sure the services restart automatically.
Test your site thoroughly and add any PHP modules that might be needed.
You also might want to take a snapshot or machine image at this point, so you won’t have to go back to PHP5.3 ever!
Note, a side effect of this upgrade is that my ssh connection to my EC2 instance will time out after a period if left unattended. I don’t see why that happened, but I guess it is a good thing. I’ll investigate and add to this article if I figure out why that happened.
How did it work for you? Please share your experiences. I am not a Linux expert; I’m just passing on what worked for me, so your insight would be welcome! – Brian