How to Install Apache 2.4 & PHP 7.4 on Amazon Linux
Abstract: This will show you the default Apache page. Step 3 – Installing PHP PHP latest versions are available under amazon-linux-extras repositories. Make sur
This tutorial will help you for installing Apache/HTTPD 2.4 and PHP 7 on Amazon Linux system.
MySQL installation is not part of this tutorial. So if you also need MySQL server on your Amazon Linux, follow this tutorial.
Step 1 – Initial SetupLog in to your Amazon Linux 2 system via SSH. Its good to keep pacakges up to date, so execute the following command to update yum packages to latest:
sudo yum update -y
Step 2 – Install Apache 2.4
The Apache (httpd) packages are available under the default yum repositories on Amazon Linux 2 systems.
Execute the following command to Apache with mod_ssl on your system.
sudo yum install -y httpd httpd-tools mod_ssl
The httpd package will install Apache (HTTPD) and mod_ssl extension will provide cryptographic support to your Apache web server.
After installation set Apache to auto-start and also start once.
sudo systemctl enable httpd
sudo systemctl start httpd
Apache has been installed on your Amazon Linux system. Access to the server in a web browser using IP address, This will show you the default Apache page.
Step 3 – Installing PHPPHP latest versions are available under amazon-linux-extras repositories. Make sure you have installed extras repository on your system.
To install amazon-linux-extras package, type:
sudo yum install amazon-linux-extras -y
Once the extras repository is configured on your system. Search for the available PHP versions under the extras repository.
sudo amazon-linux-extras | grep php
_ php7.2 available \
_ lamp-mariadb10.2-php7.2 available \
_ php7.3 available \
42 php7.4=latest enabled [ =stable ]
Next, enable the desired topic for PHP version. The following command will enable PHP 7.4 topic on your Amazon Linux 2 system. You can also choose another PHP version’s listed in above command:
sudo amazon-linux-extras enable php7.4
Finally, install the required PHP packages along with the required PHP extensions.
sudo yum clean metadata
sudo yum install php php-common php-pear
sudo yum install php-{cgi,curl,mbstring,gd,mysqlnd,gettext,json,xml,fpm,intl,zip}
PHP has been successfully installed on your Amazon Linux machine.
Type below command to check PHP version:
php -v
PHP 7.4.11 (cli) (built: Oct 21 2020 19:12:26) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
To check active version for Apache, create a PHP info file under default document root using the following command.
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
The access the info.php file in a web browser:
http://server-ip/info.php
Step 4 – Create A New Virtual HostAfter installing packages create your first virtual host. To create virtual host for your domain example.com, edit the Apache configuration file and create virtual host like below:
vi /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80> ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog /var/log/httpd/example.com-error_log CustomLog /var/log/httpd/example.com-access_log combined </VirtualHost>123456789<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog /var/log/httpd/example.com-error_log CustomLog /var/log/httpd/example.com-access_log combined</VirtualHost>
After changing restart Apache service
sudo systemctl restart httpd
Conclusion
In this tutorial, you have learned to install Apache 2.4 and PHP 7 on your Amazon Linux machine.