Installing a LAMP Web Server on Amazon Linux

The following procedures help you install the Apache web server with PHP and MySQL support on your Amazon Linux instance aws (sometimes called a LAMP web server or LAMP stack). You can use this server to host a static website or deploy a dynamic PHP application that reads and writes information to a database like WordPress, OpenCart…..

Prerequisites

This tutorial assumes that you have already launched an new instance with a public DNS name that is reachable from the Internet. For more information, see Step 1: Launch an Instance. You must also have configured your security group to allow SSH (port 22), HTTP(port 80), and HTTPS (port 443) connections. For more information about these prerequisites, see Setting Up with Amazon EC2.

Important

If you are trying to set up a LAMP web server on an Ubuntu instance, this tutorial will not work for you. These procedures are intended for use with Amazon Linux. For more information about other distributions, see their specific documentation. For information about LAMP web servers on Ubuntu, see the Ubuntu community documentation ApacheMySQLPHP topic.

To ensure that all of your software packages are up to date, perform a quick software update on your instance. This process may take a few minutes, but it is important to make sure you have the latest security updates and bug fixes.

Note

The -y option installs the updates without asking for confirmation. If you would like to examine the updates before installing, you can omit this option.

[ec2-user ~]$ sudo yum update -y

Now that your instance is current, you can install the Apache web server, MySQL, and PHP software packages. Use the yum install command to install multiple software packages and all related dependencies at the same time.

Here, httpd is on in runlevels 2, 3, 4, and 5 (which is what you want to see).

Test your web server. In a web browser, enter the public DNS address (or the public IP address) of your instance; you should see the Apache test page. You can get the public DNS for your instance using the Amazon EC2 console (check the Public DNS column; if this column is hidden, choose Show/Hide and select Public DNS).

Tip

If you are unable to see the Apache test page, check that the security group you are using contains a rule to allow HTTP (port 80) traffic. For information about adding an HTTP rule to your security group, see Adding Rules to a Security Group.

Important

If you are not using Amazon Linux, you may also need to configure the firewall on your instance to allow these connections. For more information about how to configure the firewall, see the documentation for your specific distribution.

Note

This test page appears only when there is no content in /var/www/html. When you add content to the document root, your content appears at the public DNS address of your instance instead of this test page.

Apache httpd serves files that are kept in a directory called the Apache document root. The Amazon Linux Apache document root is /var/www/html, which is owned by root by default.

To allow ec2-user to manipulate files in this directory, you need to modify the ownership and permissions of the directory. There are many ways to accomplish this task; in this tutorial, you add a www group to your instance, and you give that group ownership of the /var/www directory and add write permissions for the group. Any members of that group will then be able to add, delete, and modify files for the web server.

To set file permissions

Add the www group to your instance.

[ec2-user ~]$ sudo groupadd www

Add your user (in this case, ec2-user) to the www group.

[ec2-user ~]$ sudo usermod -a -G www ec2-user

Important

You need to log out and log back in to pick up the new group. You can use the exit command, or close the terminal window.

Log out and then log back in again, and verify your membership in the www group.

Log out.

[ec2-user ~]$ exit

Reconnect to your instance, and then run the following command to verify your membership in the www group.

[ec2-user ~]$ groups
ec2-user wheel www

Change the group ownership of /var/www and its contents to the www group.

[ec2-user ~]$ sudo chown -R root:www /var/www

Change the directory permissions of /var/www and its subdirectories to add group write permissions and to set the group ID on future subdirectories.

Recursively change the file permissions of /var/www and its subdirectories to add group write permissions.

[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;

Now ec2-user (and any future members of the www group) can add, delete, and edit files in the Apache document root. Now you are ready to add content, such as a static website or a PHP application.

(Optional) Secure your web server

A web server running the HTTP protocol provides no transport security for the data that it sends or receives. When you connect to an HTTP server using a web browser, the URLs that you enter, the content of web pages that you receive, and the contents (including passwords) of any HTML forms that you submit are all visible to eavesdroppers anywhere along the network pathway. The best practice for securing your web server is to install support for HTTPS (HTTP Secure), which protects your data with SSL/TLS encryption.

If your server is installed and running, and your file permissions are set correctly, your ec2-user account should be able to create a simple PHP file in the /var/www/html directory that will be available from the Internet.

Create a simple PHP file in the Apache document root.

[ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Tip

If you get a “Permission denied” error when trying to run this command, try logging out and logging back in again to pick up the proper group permissions that you configured in To set file permissions.

In a web browser, enter the URL of the file you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example:

http://my.public.dns.amazonaws.com/phpinfo.php

You should see the PHP information page:

Note

If you do not see this page, verify that the /var/www/html/phpinfo.phpfile was created properly in the previous step. You can also verify that all of the required packages were installed with the following command (the package versions in the second column do not need to match this example output):

If any of the required packages are not listed in your output, install them with the sudo yum install package command.

Delete the phpinfo.php file. Although this can be useful information to you, it should not be broadcast to the Internet for security reasons.

[ec2-user ~]$ rm /var/www/html/phpinfo.php

To secure the MySQL server

The default installation of the MySQL server has several features that are great for testing and development, but they should be disabled or removed for production servers. The mysql_secure_installation command walks you through the process of setting a root password and removing the insecure features from your installation. Even if you are not planning on using the MySQL server, performing this procedure is a good idea.

Enter the current root password. By default, the root account does not have a password set, so press Enter.

Type Y to set a password, and enter a secure password twice. For more information about creating a secure password, see http://www.pctools.com/guides/password/. Make sure to store this password in a safe place.

Note

Setting a root password for MySQL is only the most basic measure for securing your database. When you build or install a database-driven application, you typically create a database service user for that application and avoid using the root account for anything but database administration.

Type Y to remove the anonymous user accounts.

Type Y to disable remote root login.

Type Y to remove the test database.

Type Y to reload the privilege tables and save your changes.

(Optional) Stop the MySQL server if you do not plan to use it right away. You can restart the server when you need it again.

[ec2-user ~]$ sudo service mysqld stop
Stopping mysqld: [ OK ]

(Optional) If you want the MySQL server to start at every boot, enter the following command.

[ec2-user ~]$ sudo chkconfig mysqld on

You should now have a fully functional LAMP web server. If you add content to the Apache document root at /var/www/html, you should be able to view that content at the public DNS address for your instance.

(Optional) Install phpMyAdmin

phpMyAdmin is a web-based database management tool that you can use to view and edit the MySQL databases on your EC2 instance. Follow the steps below to install and configure phpMyAdmin on your Amazon Linux instance.

Important

We do not recommend using phpMyAdmin to access a LAMP server unless you have enabled SSL/TLS in Apache; otherwise, your database administrator password and other data will be transmitted insecurely across the Internet. For information about configuring a secure web server on an EC2 instance, see Tutorial: Configure Apache Web Server on Amazon Linux to use SSL/TLS.

Note

These instructions assume that the same default PHP version is specified in Amazon Linux and in Extra Packages for Enterprise Linux (EPEL). If you encounter compatibility issues with EPEL packages, we recommend installing phpMyAdmin manually. See the phpMyAdmin download page for the latest release. Be sure to verify that the installation requirements match the environment of your Amazon Linux (or other Linux) instance.

Enable the Extra Packages for Enterprise Linux (EPEL) repository from the Fedora project on your instance.

[ec2-user ~]$ sudo yum-config-manager --enable epel

Install the phpMyAdmin package.

[ec2-user ~]$ sudo yum install -y phpMyAdmin

Note

Answer y to import the GPG key for the EPEL repository when prompted.

Configure your phpMyAdmin installation to allow access from your local machine. By default, phpMyAdmin only allows access from the server that it is running on, which is not very useful because Amazon Linux does not include a web browser.

Find your local IP address by visiting a service such as whatismyip.com.

Edit the /etc/httpd/conf.d/phpMyAdmin.conf file and replace the server IP address (127.0.0.1) with your local IP address with the following command, replacing your_ip_address with the local IP address that you identified in the previous step.

In a web browser, enter the URL of your phpMyAdmin installation. This URL is the public DNS address of your instance followed by a forward slash and phpmyadmin. For example:

http://my.public.dns.amazonaws.com/phpmyadmin

You should see the phpMyAdmin login page:

Note

If you get a 403 Forbidden error, verify that you have set the correct IP address in the /etc/httpd/conf.d/phpMyAdmin.conf file. You can see what IP address the Apache server is actually getting your requests from by viewing the Apache access log with the following command:

Is the firewall correctly configured?If you are unable to see the Apache test page, check that the security group you are using contains a rule to allow HTTP (port 80) traffic. For information about adding an HTTP rule to your security group, see Adding Rules to a Security Group.

The application software I want to run on my server is incompatible with the installed PHP version or other software.

This tutorial recommends installing the most up-to-date versions of Apache webserver, PHP, and MySQL. Before installing an additional LAMP application, check its requirements to confirm that it is compatible with your installed environment. If the latest version of PHP is not supported, it is possible (and entirely safe) to downgrade to an older supported configuration. The well-tested previous version of this tutorial called for the following core LAMP packages:

httpd24

php56

mysql55-server

php56-mysqlnd

Related Topics

For more information on transferring files to your instance or installing a WordPress blog on your web server, see the following topics: