0

Install an Apache web server with PHP and MySQL in Amazon Linux

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 -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.
[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).

apache_test_page

 

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
http://my.public.dns.amazonaws.com/phpinfo.php

You should see the PHP information page:

phpinfo

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
Installed Packages
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 package command.

 

 

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
  1. When prompted, type a password for the root account.
    1. Type the current root password. By default, the root account does not have a password set. Press Enter.
    2. Type Y to 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.

      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.

  2. Type Y to remove the anonymous user accounts.
  3. Type Y to disable the remote root login.
  4. Type Y to remove the test database.
  5. Type Y to 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.

 

 

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 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.

Note

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 /var/www/html.
[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 phpMyAdmin-latest-all-languages.tar.gz tarball.
[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:
http://my.public.dns.amazonaws.com/phpMyAdmin

You should see the phpMyAdmin login page:

phpmyadmin

 

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.