Install LAMP Stack on CentOS 7

In this article we will show you how to install LAMP Stack (Apache, MySQL/MariaDB and PHP on Linux) on a CentOS 7 server. This is one of the most common stack used for hosting websites and easy to maintain, build and deploy.

Prerequisites

  • Server with CentOS 7 installed.
  • root access to the server.
  • Public facing IP address.

Update the system to latest.

yum update

Install Apache

Apache package is available in the default CentOS repository and can be installed with the below command.

yum install httpd

After the installation completes, we will start and enable the service so that it is started as soon as the server boots up.

systemctl start httpd

systemctl enable httpd

And to confirm if the Apache is running on the server,

systemctl status httpd

Output:

[root@vps ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-12-01 19:51:18 UTC; 11s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 8138 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─8138 /usr/sbin/httpd -DFOREGROUND
           ├─8139 /usr/sbin/httpd -DFOREGROUND
           ├─8140 /usr/sbin/httpd -DFOREGROUND
           ├─8141 /usr/sbin/httpd -DFOREGROUND
           ├─8142 /usr/sbin/httpd -DFOREGROUND
           └─8143 /usr/sbin/httpd -DFOREGROUND

Update Firewall Settings

To make your pages available to public, you will have to edit your firewall rules to allow HTTP and HTTPS requests on your web server by using the following commands.

firewall-cmd --permanent --zone=public --add-service=http

firewall-cmd --permanent --zone=public --add-service=https

firewall-cmd --reload

From your browser,

Open up the IP address of the server

http://Your-Server-IP-Address

Apache Page

Install MariaDB

MariaDB is another popular Database service which is a community-developed, commercially supported fork of the MySQL relational database management system.

Run the below command to install MariaDB,

yum install mariadb mariadb-server

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB and verify the status using the commands below.

systemctl enable mariadb

systemctl start mariadb

systemctl status mariadb

Output:

[root@vps ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-12-01 20:00:38 UTC; 6s ago
  Process: 8791 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 8756 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 8790 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─8790 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─8955 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var...

Secure MariaDB

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

While running the above command, you will be prompted with several security settings. Below are some of the recommendations that should be performed.

It is best advised to create a root password for accessing MariaDB,

Set root password? [Y/n] Y

By default, a MariaDB installation has an anonymous user, remove this,

Remove anonymous users? [Y/n] Y

Allow root user to be connected only via localhost,

Disallow root login remotely? [Y/n] Y

By default, MariaDB comes with a database named 'test' that anyone can access, this is not required and can be removed,

Remove test database and access to it? [Y/n] Y

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] Y

If you made any kind of mistake during the process, do not worry, you can just re-run the mysql_secure_installation command once again and update the settings.

Install PHP

Now let us move on-to the last step of the process, installing PHP. PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.

Now, by default, PHP 5.4 is shipped as default PHP version on CentOS 7.

This is old and reached EOL (End of Life), we will instead go with much latest version of PHP using Remi-Repository.

Enable Remi Repository

The latest versions of PHP are available on remi repositories.

We will install the required dependencies for this,

yum install epel-release yum-utils

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Select the PHP Version

At the time of writing this article, we had versions 8.0, 8.1 and 8.2 available.

You can choose which ever version you would like, in this example, we will use 8.1

Enable the PHP version from remi repo,

yum-config-manager --enable remi-php81

If you like to install PHP 8.0 or 8.2, you can just replace remi-php81 with remi-php80 or remi-php82.

Install PHP and it's commonly used modules,

yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Now, verify the installation with,

php --version

Output:

[root@vps ~]# php --version
PHP 8.1.13 (cli) (built: Nov 22 2022 14:42:07) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies

Congratulations, you've now successfully installed LAMP Stack with the latest version of PHP. Be proud of yourself.

If you bump into any sort of issues during the whole process, please contact us by opening a support ticket and our team will help you out.