In this tutorial we will show you how to install an Apache web server with PHP and MySQL support on your Amazon Linux instance (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.
To install and start the LAMP web server with the Amazon Linux AMI (APACHE)
- Connect to your instance.
- 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 that you have the latest security updates and bug fixes.The
-yoption 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.
[ec2-user ~]$ sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
- Start the Apache web server.
[ec2-user ~]$ sudo service httpd start
Starting httpd: [ OK ]
- Use the chkconfig command to configure the Apache web server to start at each system boot.
[ec2-user ~]$ sudo chkconfig httpd on
The chkconfig command does not provide any confirmation message when you successfully use it to enable a service.
You can verify that httpd is on by running the following command:
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- Test your web server. In a web browser, type the public DNS address (or the public IP address) of your instance. If there is no content in
/var/www/html, 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 Columns (the gear-shaped icon) and choose Public DNS).
To test your LAMP web server
If your server is installed and running, and your file permissions are set correctly, your
ec2-user account should be able to create a PHP file in the
/var/www/html directory that is available from the internet.
- Create a PHP file in the Apache document root.
[ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
- 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, type the URL of the file that you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example
You should see the PHP information page:
If you do not see this page, verify that the
/var/www/html/phpinfo.php file 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.
[ec2-user ~]$ sudo yum list installed httpd24 php70 mysql56-server php70-mysqlnd
Loaded plugins: priorities, update-motd, upgrade-helper
httpd24.x86_64 2.4.25-1.68.amzn1 @amzn-updates
mysql56-server.x86_64 5.6.35-1.23.amzn1 @amzn-updates
php70.x86_64 7.0.14-1.20.amzn1 @amzn-updates
php70-mysqlnd.x86_64 7.0.14-1.20.amzn1 @amzn-updates
If any of the required packages are not listed in your output, install them using the sudo yum install
To secure the database 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, we recommend performing this procedure.
- Start the MySQL server.
[ec2-user ~]$ sudo service mysqld start
- Run mysql_secure_installation.
[ec2-user ~]$ sudo mysql_secure_installation
- When prompted, type a password for the root account.
- Type the current root password. By default, the root account does not have a password set. Press Enter.
Yto set a password, and type a secure password twice. For more information about creating a secure password, see https://identitysafe.norton.com/password-generator/. Make sure to store this password in a safe place.
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.
Yto remove the anonymous user accounts.
Yto disable the remote root login.
Yto remove the test database.
Yto reload the privilege tables and save your changes.
- (Optional) If you do not plan to use the MySQL server right away, stop it. You can restart it 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, type 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.
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.
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 are transmitted insecurely across the internet. For security recommendations from the developers, see Securing your phpMyAdmin installation. For general information about securing a web server on an EC2 instance, see Tutorial: Configure Apache Web Server on Amazon Linux to use SSL/TLS.
The Amazon Linux package management system does not currently support the automatic installation of phpMyAdmin in a PHP 7 environment. This tutorial describes how to install phpMyAdmin manually.
- Log in to your EC2 instance using SSH.
- Install the required dependencies.
[ec2-user ~]$ sudo yum install php70-mbstring.x86_64 php70-zip.x86_64 -y
- Restart Apache.
[ec2-user ~]$ sudo service httpd restart
- Navigate to the Apache document root at
[ec2-user ~]$ cd /var/www/html
- Select a source package for the latest phpMyAdmin release from https://www.phpmyadmin.net/downloads. To download the file directly to your instance, copy the link and paste it into a wget command, as in this example:
[ec2-user html]$ wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
- Create a phpMyAdmin folder and extract the package into it using the following command.
[ec2-user html]$ mkdir phpMyAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C phpMyAdmin --strip-components 1
- Delete the
[ec2-user html]$ rm phpMyAdmin-latest-all-languages.tar.gz
- (Optional) If the MySQL server is not running, start it now.
[ec2-user ~]$ sudo service mysqld start
Starting mysqld: [ OK ]
- In a web browser, type the URL of your phpMyAdmin installation. This URL is the public DNS address (or the public IP address) of your instance followed by a forward slash and the name of your installation directory. For example:
You should see the phpMyAdmin login page:
- Log in to your phpMyAdmin installation with the
rootuser name and the MySQL root password you created earlier.Your installation must still be configured before you put it into service. To configure phpMyAdmin, you can manually create a configuration file, use the setup console, or combine both approaches.For information about using phpMyAdmin, see the phpMyAdmin User Guide.