The following procedures help you install an Apache web server with PHP and MariaDB (a community-developed fork of MySQL) support on your Amazon Linux 2 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 set up a LAMP web server on Amazon Linux AMI, see Tutorial: Install a LAMP Web Server with the Amazon Linux AMI.
Important
If you are trying to set up a LAMP web server on an Ubuntu or Red Hat Enterprise Linux instance, this tutorial will not work for you. 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.
Step 1: Prepare the LAMP Server
Prerequisites
This tutorial assumes that you have already launched a new instance using Amazon Linux 2, 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.
To prepare the LAMP server
- 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.1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo yum <span class="">update</span> -y</code></strong> - Install the
lamp-mariadb10.2-php7.2
andphp7.2
Amazon Linux Extras repositories to get the latest versions of the LAMP MariaDB and PHP packages for Amazon Linux 2.1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class=""><span class="">sudo</span> <span class="">amazon-linux-extras</span> <span class="">install</span> <span class="">-y</span> <span class="">lamp-mariadb10</span><span class="">.2-php7</span><span class="">.2</span> <span class="">php7</span><span class="">.2</span></code></strong>Note
If you receive an error stating
sudo: amazon-linux-extras: command not found
, then your instance was not launched with an Amazon Linux 2 AMI (perhaps you are using the Amazon Linux AMI instead). You can view your version of Amazon Linux with the following command.1<strong class="userinput"><code class="">cat /etc/system-<span class="">release</span></code></strong>To set up a LAMP web server on Amazon Linux AMI , see Tutorial: Install a LAMP Web Server with the Amazon Linux AMI.
- Now that your instance is current, you can install the Apache web server, MariaDB, and PHP software packages.
Use the yum install command to install multiple software packages and all related dependencies at the same time.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo yum <span class="">install</span> -y httpd mariadb-<span class="">server</span></code></strong>Note
You can view the current versions of these packages with the following command:
1<strong class="userinput"><code class=""><span class="">yum</span> <span class="">info</span> <em class="replaceable">package_name</em></code></strong> - Start the Apache web server.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">start</span> httpd</code></strong> - Use the systemctl command to configure the Apache web server to start at each system boot.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">enable</span> httpd</code></strong>
You can verify that httpd is on by running the following command:
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">is</span>-enabled httpd</code></strong> - Add a security rule to allow inbound HTTP (port 80) connections to your instance if you have not already done so. By default, a launch-wizard-
N
security group was set up for your instance during initialization. This group contains a single rule to allow SSH connections.- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- Choose Instances and select your instance.
- Under Security groups, choose view inbound rules.
- You should see the following list of rules in your default security group:
12Security Groups associated with i-1234567890abcdef0Ports Protocol Source launch-wizard-<em class="replaceable"><code class="">N</code></em> 22 tcp 0.0.0.0/0 ✔
Using the procedures in Adding Rules to a Security Group, add a new inbound security rule with the following values:- Type: HTTP
- Protocol: TCP
- Port Range: 80
- Source: Custom
- 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).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.
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 by default is owned by root.
To allow the ec2-user
account to manipulate files in this directory, you must modify the ownership and permissions of the directory. There are many ways to accomplish this task. In this tutorial, you add ec2-user
to the apache
group, to give the apache
group ownership of the /var/www
directory and assign write permissions to the group.
To set file permissions
- Add your user (in this case,
ec2-user
) to theapache
group.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo usermod -a -G apache <em class="replaceable">ec2-<span class="">user</span></em></code></strong> - Log out and then log back in again to pick up the new group, and then verify your membership.
- Log out (use the exit command or close the terminal window):
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class=""><span class="">exit</span></code></strong> - To verify your membership in the
apache
group, reconnect to your instance, and then run the following command:
12<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">groups</code></strong><code class="">ec2-user adm wheel apache systemd-journal</code>
- Log out (use the exit command or close the terminal window):
- Change the group ownership of
/var/www
and its contents to theapache
group.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo chown -R ec2-user:apache /<span class="">var</span>/www</code></strong> - To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of
/var/www
and its subdirectories.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo chmod <span class="">2775</span> /<span class="">var</span>/www && find /<span class="">var</span>/www -<span class="">type d -exec sudo chmod 2775 </span>{} \;</code></strong> - To add group write permissions, recursively change the file permissions of
/var/www
and its subdirectories:
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">find /<span class="">var</span>/www -<span class="">type f -exec sudo chmod 0664 </span>{} \;</code></strong>
Now, ec2-user
(and any future members of the apache
group) can add, delete, and edit files in the Apache document root, enabling you to add content, such as a static website or a PHP application.
To secure your web server (Optional)
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 visit, the content of webpages 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.
For information about enabling HTTPS on your server, see Tutorial: Configure Apache Web Server on Amazon Linux to use SSL/TLS.
Step 2: Test Your LAMP 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.
To test your LAMP server
- Create a PHP file in the Apache document root.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">echo <span class="">"<?php phpinfo(); ?>"</span> > <span class="">/var/www</span><span class="">/html/phpinfo</span>.php</code></strong>
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:
1http://<em class="replaceable"><code class=""><span class="">my</span><span class="">.public</span><span class="">.dns</span><span class="">.amazonaws</span><span class="">.com</span></code></em>/phpinfo.php
You should see the PHP information page:Note
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.1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo yum <span class="">list</span> installed httpd mariadb-server php-mysqlnd</code></strong>If any of the required packages are not listed in your output, install them with the sudo yum install
package
command. Also verify that thephp7.2
andlamp-mariadb10.2-php7.2
extras are enabled in the out put of the amazon-linux-extras command. - Delete the
phpinfo.php
file. Although this can be useful information, it should not be broadcast to the internet for security reasons.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">rm /<span class="">var</span>/www/html/phpinfo.php</code></strong>
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.
Step 3: Secure the Database Server
The default installation of the MariaDB 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 MariaDB server, we recommend performing this procedure.
To secure the MariaDB server
- Start the MariaDB server.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">start</span> mariadb</code></strong> - Run mysql_secure_installation.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class=""><span class="">sudo</span> mysql_secure_installation</code></strong>- 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.
- 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 MariaDB 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 the remote root login. - Type
Y
to remove the test database. - Type
Y
to reload the privilege tables and save your changes.
- When prompted, type a password for the root account.
- (Optional) If you do not plan to use the MariaDB server right away, stop it. You can restart it when you need it again.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">stop</span> mariadb</code></strong> - (Optional) If you want the MariaDB server to start at every boot, type the following command.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">enable</span> mariadb</code></strong>
Step 4: (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 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.
To install phpMyAdmin
- Install the required dependencies.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo yum <span class="">install</span> php-mbstring -y</code></strong> - Restart Apache.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class=""><span class="">sudo</span> systemctl restart httpd</code></strong> - Restart
php-fpm
.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class=""><span class="">sudo</span> systemctl restart php-fpm</code></strong> - Navigate to the Apache document root at
/var/www/html
.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">cd /<span class="">var</span>/www/html</code></strong> - 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:
1<code class="">[ec2-user html]$ </code><strong class="userinput"><code class=""><span class="">wget</span> <em class="replaceable">https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-<span class="">all</span>-languages.tar.gz</em></code></strong> - Create a
phpMyAdmin
folder and extract the package into it with the following command.
1<code class="">[ec2-user html]$ </code><strong class="userinput"><code class=""><span class="">mkdir</span> <span class="">phpMyAdmin</span> && <span class="">tar</span> <span class="">-xvzf</span> <em class="replaceable"><span class="">phpMyAdmin-latest-all-languages</span><span class="">.tar</span><span class="">.gz</span></em> <span class="">-C</span> <span class="">phpMyAdmin</span> <span class="">--strip-components</span> 1</code></strong> - Delete the
phpMyAdmin-latest-all-languages.tar.gz
tarball.
1<code class="">[ec2-user html]$ </code><strong class="userinput"><code class=""><span class="">rm</span> <em class="replaceable"><span class="">phpMyAdmin-latest-all-languages</span><span class="">.tar</span><span class="">.gz</span></em></code></strong> - (Optional) If the MySQL server is not running, start it now.
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">start</span> mariadb</code></strong> - 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:
1http://<em class="replaceable"><code class=""><span class="">my</span><span class="">.public</span><span class="">.dns</span><span class="">.amazonaws</span><span class="">.com</span></code></em>/phpMyAdmin
You should see the phpMyAdmin login page: - Log in to your phpMyAdmin installation with the
root
user 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.
Troubleshooting
This section offers suggestions for resolving common problems you may encounter while setting up a new LAMP server.
I can’t connect to my server using a web browser.
Perform the following checks to see if your Apache web server is running and accessible.
- Is the web server running?
You can verify that httpd is on by running the following command:
1<code class="">[ec2-user ~]$ </code><strong class="userinput"><code class="">sudo systemctl <span class="">is</span>-enabled httpd</code></strong>If the httpd process is not running, repeat the steps described in To prepare the LAMP server.
- 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.
Related Topics
For more information about transferring files to your instance or installing a WordPress blog on your web server, see the following documentation:
For more information about the commands and software used in this tutorial, see the following webpages:
- Apache web server: http://httpd.apache.org/
- MariaDB database server: https://mariadb.org/https://mariadb.org/
- PHP programming language: http://php.net/
- The
chmod
command: https://en.wikipedia.org/wiki/Chmod - The
chown
command: https://en.wikipedia.org/wiki/Chown
For more information about registering a domain name for your web server, or transferring an existing domain name to this host, see Creating and Migrating Domains and Subdomains to Amazon Route 53 in the Amazon Route 53 Developer Guide.