- Install LAMP on Amazon Linux 2023
- Step 1: Prepare the LAMP server
- Step 2: Test your LAMP server
- Step 3: Secure the database server
- Step 4: (Optional) Install phpMyAdmin
- Troubleshoot
- I can’t connect to my server using a web browser
- I can’t connect to my server using HTTPS
- Related topics
- How to Install Apache Web Server on Amazon Linux 2
- Prerequisites
- Install Apache Web Server and run simple HTML web page
- Step 1: Install Apache Web Server
- Step 2: Start Apache Web Server
- Step 3: Configure Firewall
- Step 4: Create a Simple HTML Web Page
- Step 5: Test Apache Web Server
- Conclusion
- References
Install LAMP on Amazon Linux 2023
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 2023 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.
Important
These procedures are intended for use with Amazon Linux 2023. If you are trying to set up a LAMP web server on a different distribution, such as Ubuntu or Red Hat Enterprise Linux, this tutorial will not work. For Amazon Linux 2, see Install LAMP on Amazon Linux 2. For Amazon Linux AMI, see Install LAMP on Amazon Linux. For Ubuntu, see the following Ubuntu community documentation: ApacheMySQLPHP . For other distributions, see their specific documentation.
Tasks
Step 1: Prepare the LAMP server
Prerequisites
- This tutorial assumes that you have already launched a new instance using Amazon Linux 2023, 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 Authorize inbound traffic for your Linux instances.
- The following procedure installs the latest PHP version available on Amazon Linux 2023, currently 8.1. If you plan to use PHP applications other than those described in this tutorial, you should check their compatibility with 8.1.
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 might 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 dnf update -y
[ec2-user ~]$
sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
[ec2-user ~]$
sudo dnf install mariadb105-server
[ec2-user ~]$
sudo dnf info package_name
[root@ip-172-31-25-170 ec2-user]# dnf info mariadb105 Last metadata expiration check: 0:00:16 ago on Tue Feb 14 21:35:13 2023. Installed Packages Name : mariadb105 Epoch : 3 Version : 10.5.16 Release : 1.amzn2023.0.6 Architecture : x86_64 Size : 18 M Source : mariadb105-10.5.16-1.amzn2023.0.6.src.rpm Repository : @System From repo : amazonlinux Summary : A very fast and robust SQL database server URL : http://mariadb.org License : GPLv2 and LGPLv2 Description : MariaDB is a community developed fork from MySQL - a multi-user, multi-threaded : SQL database server. It is a client/server implementation consisting of : a server daemon (mariadbd) and many different client programs and libraries. : The base package contains the standard MariaDB/MySQL client programs and : utilities.
[ec2-user ~]$
sudo systemctl start httpd
[ec2-user ~]$
sudo systemctl enable httpd
[ec2-user ~]$
sudo systemctl is-enabled httpd
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ .
- In the left navigator, choose Instances, and select your instance.
- On the Security tab, view the inbound rules. You should see the following rule:
Port range Protocol Source 22 tcp 0.0.0.0/0
Warning
Using 0.0.0.0/0 allows all IPv4 addresses to access your instance using SSH. This is acceptable for a short time in a test environment, but it’s unsafe for production environments. In production, you authorize only a specific IP address or range of addresses to access your instance.
- Type: HTTP
- Protocol: TCP
- Port Range: 80
- Source: Custom
Important
If you are not using Amazon Linux, you might 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
[ec2-user ~]$
sudo usermod -a -G apache ec2-user
[ec2-user ~]$
groups
ec2-user adm wheel apache systemd-journal
[ec2-user ~]$
sudo chown -R ec2-user:apache /var/www
[ec2-user ~]$
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 > \;
[ec2-user ~]$
find /var/www -type f -exec sudo chmod 0664 > \;
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 Configure SSL/TLS on Amazon Linux 2.
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
[ec2-user ~]$
echo "" > /var/www/html/phpinfo.php
http://my.public.dns.amazonaws.com
/phpinfo.php
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.
[ec2-user ~]$
sudo dnf list installed httpd mariadb-server php-mysqlnd
[ec2-user ~]$
rm /var/www/html/phpinfo.php
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
[ec2-user ~]$
sudo systemctl start mariadb
[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.
- 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. 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.
[ec2-user ~]$
sudo systemctl stop mariadb
[ec2-user ~]$
sudo systemctl enable mariadb
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 Configure SSL/TLS on Amazon Linux 2023.
To install phpMyAdmin
[ec2-user ~]$
sudo dnf install php-mbstring php-xml -y
[ec2-user ~]$
sudo systemctl restart httpd
[ec2-user ~]$
sudo systemctl restart php-fpm
[ec2-user html]$
wget
https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
[ec2-user html]$
mkdir phpMyAdmin && tar -xvzf
phpMyAdmin-latest-all-languages.tar.gz
-C phpMyAdmin --strip-components 1[ec2-user html]$
rm
phpMyAdmin-latest-all-languages.tar.gz
[ec2-user ~]$
sudo systemctl start mariadb
http://
my.public.dns.amazonaws.com
/phpMyAdmin- To start with a minimal configuration file, use your favorite text editor to create a new file, and then copy the contents of config.sample.inc.php into it.
- Save the file as config.inc.php in the phpMyAdmin directory that contains index.php .
- Refer to post-file creation instructions in the Using the Setup script section of the phpMyAdmin installation instructions for any additional setup.
For information about using phpMyAdmin, see the phpMyAdmin User Guide .
Troubleshoot
This section offers suggestions for resolving common problems you might 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:
[ec2-user ~]$
sudo systemctl is-enabled httpd
I can’t connect to my server using HTTPS
Perform the following checks to see if your Apache web server is configured to support HTTPS.
- Is the web server correctly configured? After you install Apache, the server is configured for HTTP traffic. To support HTTPS, enable TLS on the server and install an SSL certificate. For information, see Configure SSL/TLS on Amazon Linux 2023.
- Is the firewall correctly configured? Verify that the security group for the instance contains a rule to allow HTTPS traffic on port 443. For more information, see Add 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/
- 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.
How to Install Apache Web Server on Amazon Linux 2
In this tutorial, we will learn how to install Apache web server on Amazon Linux 2. We will also learn how to configure Apache web server to run simple HTML web page.
Prerequisites
Install Apache Web Server and run simple HTML web page
Step 1: Install Apache Web Server
Before we start, we need to update the package list and upgrade the installed packages:
Now, we can install Apache web server:
Step 2: Start Apache Web Server
Now, we can start Apache web server:
sudo systemctl start httpd
sudo systemctl enable httpd
Step 3: Configure Firewall
Now, we need to configure the firewall to allow HTTP traffic:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
Step 4: Create a Simple HTML Web Page
Before we can test Apache web server, we need to change the permissions of the /var/www/html directory:
sudo chmod 777 /var/www/html
Now, we can create a simple HTML web page:
sudo vi /var/www/html/index.html
Apache Web Server Apache Web Server This is a simple HTML web page.
Step 5: Test Apache Web Server
Now, we can test Apache web server by opening the public IP address of our Amazon Linux 2 EC2 instance in a web browser:
Conclusion
In this tutorial, we learned how to install Apache web server on Amazon Linux 2. We also learned how to configure Apache web server to run simple HTML web page.
References