Install Apache, PHP and MySQL on Amazon EC2 and Other VPS

Till now, you have read and followed guides to setup linux EC2 server, Using Key pair to access your EC2 server and Install Webmin on it. This guide is also tested with 128-1024 MB other local and branded VPS. All works (Just neglect Amazon EC2 or VPC portions).

As we are moving forward, I would like to request you to read this Precautionary and money saving guide on EC2.

Before a domain could be hosted we need certain services on our server. Think of them like framework. Each have different use and importance. Not all may be necessary but few like Apache, PHP and MySQL is like basic for any server now.

In this guide,  you will learn how to install Apache, PHP and MySQL on Amazon EC2 and any other VPS in very less time without any errors. You can install these things via Webmin or via SSH using PuTTY. I prefer SSH for much better control over things. Fire-up your PuTTY and login to your web-server.

Update For AWS Users: My experiments shows that it is better to Assign Elastic IP at this step before going ahead to avoid server IP configurations. Please refer these small and easy steps to do it.

Sudo is used as prefix in front of commands assuming that you are using Amazon linux and logged in an ec2-user. For any other linux and VPS where you login as root this not necessary.

TIP: AWS user using ‘ec2-user’ can also avoid all ‘sudo’ by typing one command just once: “sudo su”

A. Install Apache:

1. CentOS and Other Linux like Debian,  etc comes pre-installed with Apache and even with MySQL. If in doubt first check it;

The return message will be self explaining. If it is not installed then copy-paste this;

Now, let’s start it;

This will restart the httpd (Apache) service. To test that it is working fine or not. You can type your VPS  or EC2 IP in browser and hit enter. You should get an Apache Test Page saying the server’s Operating system.

Remember: For Ec2 Users the IP will be this if not using EIP-

ec2-x1x-x2x-x3x-x4x-ap.northamerica.com –> “x1x.x2x.x3x.x4x” : The middle Four set of numbers; replaced with ‘dot’ instead of ‘dash’.

If using EIP: Well you can directly use that IP. Why wait for me? :-)

B. Install PHP:

PHP and PHP53 packages are different. If you want to run latest WordPress then you will need PHP 5.3.x. Problem is Operating system. Amazon Linux, CentOs 6.x and above or any latest Linux will automatically get PHP 5.3.x version (i.e. latest) using below regular method. However, people on older linux images, CentOs 5.x and lower will only get PHP 5.2 using this method. This will restrict you using WordPress etc.

If you have older OS and want latest PHP5.3 which works with everything,  Go here and Follow This PHP53 install with its own MySQL Package.

If you are on CentOS 6.x above or any other updated linux then just continue reading.

1. First check that PHP is installed or not;

Return message will say something if it is installed.

2. Install PHP;

3. Restart apache;

This is done. However,  we need to test this thing.

B1. Testing PHP

We have to create a PHP test file and run it from browser to do so. There are 2-3 ways to do it.

First: Using PuTTY. – Fast and easier.

1. Paste this

2. Then create file using;

3. If you don’t have ‘nano’ text editor installed then install it first using “sudo yum install nano”.

4. Copy-paste this content in that file;

5. Press Ctrl+X and then ‘Y’ and then Enter to save the file.

6. Now,  Go to your browser and type “http://YourIP/phpinfo.php” and hit enter. PHP info page shall appear.

Here IP should be standard IP (from that long DNS or EIP) and not Amazon long IP.

Second Method: Via Webmin

1. Go to your Webmin and Select File Manager from Others Option in Sidebar.

2. Create a file with notepad and copy same content as above. [Or save all efforts by downloading already created file Here.]

3. Save this file as ‘phpinfo.php’.

4. Upload this file to “/var/www/html/” using upload button of Webmin File manager.

5. Now go to browser and type “http://YourIP/phpinfo.php” and hit enter. PHP info page shall appear.

C. Install MySQL:

Note: If you installed PHP53 then please go to this page and install MySQL in that way as the packages are different. It is all same thing but due to latest version; MySQL is also needed latest. If you installed regular PHP then keep reading.

1. First check that it is already installed or not. Many OS comes with it pre-installed and this causes issues many times. Generally CentOS basic and Amazon Generic Linux don’t come installed with it. Lucky!

2. Returning message will be confirmatory one. Install it using this;

3. After pressing ‘y’ as usually we all do,  MySQL installation will complete.

4. Restart Apache,

I will suggest you to install some more MySQL packages to avoid any issues with PHPMyAdmin etc in future.

Let’s do this. Run this commands one by one;

Now,  type this;

Press Ctrl+W and find this line, “extension=msql.so”. Uncomment it. Ahh.. means.. just remove those # sign present in front.

Then copy-paste some more lines under it;

Now, restart Apache.

Done! Congratz.

Man! I am tired. Thank you for reading. Let me know of any issues and please do comment.

Next things you can read and do;

- Install PhpMyAdmin
-
Setup Domain Name Server in Webmin on VPS and EC2
-
Setup Apache Virtual Server on VPS and Ec2

Creative Commons License

Install Apache, PHP and MySQL on Amazon EC2 and Other VPS by Mrinal Buddekar is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Mrinal Buddekar

A Research Professional, Doctor, Technology Blogger
Mrinal Buddekar is a founder of I Just Utter. He is a technology enthusiast and author of many guides and how-to. Mrinal likes web development, linux, server administration and programming.