How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 7
140 views0September 29, 2022Updated on December 2, 2022host_know_user
Introduction
A “LAMP” stack is a collection of open-source software that is often deployed together on a server to allow it to run dynamic webpages and web apps. This word is an abbreviation for the Linux operating system and the Apache web server. The site data is commonly stored in a MySQL database, and PHP is used to process dynamic content.
You can install MySQL on most Linux systems by downloading the mysql-server package from your system’s default package management repositories. On CentOS 7, however, the mysql-server package will actually install MariaDB, a community-developed variant of the MySQL relational database management system that can be used in place of MySQL. As a result, this guide will show you how to install a LAMP stack (Linux, Apache, MariaDB, and PHP) on a CentOS 7 server.
Prerequisites
Before you begin, you should have a non-root user account set up on your server.
Step 1 : Installing the Apache Web Server
Apache is a well-known open-source web server used to provide web pages to visitors. It can be configured to serve PHP pages.
Using the CentOS package manager, yum. install Apache. A package manager allows you to install most items from a CentOS repository.
To install the httpd Apache package, run the following command in your terminal:
$ sudo yum install httpd
To validate the Apache installation, enter Y when prompted. Once the installation is complete, use the following command to start your Apache server:
$ sudo systemctl start httpd
Enter your public IP address or domain name in your web browser to see if your server is up and operating.
If you don’t have a domain name directed at your server or don’t know what your server’s public IP address is, use the following command:
$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
This will print a variety of addresses. You can test each one on your browser.
Another option is to employ an outside party to inform you how it perceives your server. You can accomplish this by using the following command to query a specific server for your IP address:
$ curl http://icanhazip.com
Whichever technique you use, enter your IP address into your web browser to ensure that your server is operational.
http://your_server_IP_address
The default CentOS 7 Apache landing page will be displayed:
You can make Apache start on boot by typing:
$ sudo systemctl enable httpd.service
Step 2 : Set up MySQL (MariaDB)
You can install MariaDB once your web server is up and running. It will arrange and provide access to databases where your website’s information might be stored.
Run the following command to install the MariaDB software package:
$ sudo yum install mariadb-server
Start MariaDB once the installation is complete:
$ sudo systemctl start mariadb
With the following command, you may make MariaDB start on boot:
It is advised that you run a security script that comes pre-installed with MariaDB to increase the security of your database server. This script will disable access to your database system and delete several insecure default settings.
$ sudo systemctl enable mariadb.service
Begin the interactive script by typing:
$ sudo mysql_secure_installation
This script will guide you through a series of prompts where you can modify your MariaDB configuration. The initial prompt will request your current database root password. This is not the same as the system root user. The database root user is an administrative user who has complete control over the database system. This password will be blank because you just installed MariaDB and haven’t made any configuration modifications. At the prompt, type ENTER.
The next prompt asks if you want to create a database root password. Enter N and press ENTER.
From there, press Y and then ENTER to accept the defaults for all further questions. This will remove anonymous users and the test database, deactivate remote root login, and load these new rules so that the server immediately recognises the changes you’ve made.
When you’re completed, go to the MariaDB console and type:
$ sudo mysql
This authenticates you as the administrative database user root on the MariaDB server:
Output Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
For added protection, create specialised user accounts with limited privileges for each database. This is especially critical if you intend to host many databases on your server.
Create a database called example_database and a user called example_user to demonstrate such a setup. You can change the values in these names.
To create a new database, use the following command from your MariaDB console:
You can create a new user and grant them complete access to the newly established custom database. This user’s password is defined as password, in the following command, but you should replace this value with a secure password:
MariaDb [(none)]> GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
This command grants the example_user user full access to the example_database database while prohibiting this user from creating or changing any other databases on your server.
To reload and save the capabilities you just given to example user, use the FLUSH statement:
MariaDb [(none)]> FLUSH PRIVILEGES;
Exit the MariaDB shell :
You can check if the new user has the necessary rights by logging back into the MariaDB console with the example user credentials you set earlier:
$ mysql -u example_user –p
note of the -p argument in this command, which will prompt you for the password you specified when creating the example user user. After login in to the MariaDB console, use the following statement to ensure that you have access to the example database database:
MariaDb [(none)]> SHOW DATABASES;
In the output, your example database should be listed:
Output +——————–+ | Database | +——————–+ | example_database | | information_schema | +——————–+ 2 rows in set (0.000 sec)
Enter the following command to leave the MariaDB shell:
MariaDb [(none)]> exit
Step 3 : Installing PHP
You have Apache installed to serve content and MariaDB installed to store and manage data. PHP will execute code in order to display dynamic content to the user. You will also need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. As dependencies, core PHP packages will be installed automatically.
Using yum, run the following command to install the php and php-mysql packages:
$ sudo yum install php php-mysql
To enable the PHP module you installed, restart the Apache web server:
$ sudo systemctl restart httpd.service
Your server is now set up with all of the components required for your LAMP stack application. The following step is to test your configuration to ensure that everything is in order.
Step 4: Put PHP to the test on your Apache Web Server.
On CentOS 7, the default Apache installation will create a document root at /var/www/html. You don’t need to change any of Apache’s default settings for PHP to work properly on your web server.
You can, however, modify the default permission settings on your Apache document root folder. This allows you to use your regular system user to create and modify files in that directory without having to prefix each command with sudo.
The following command will transfer ownership of the default Apache document root to a user and group named hostzop, so make sure to replace the highlighted username and group in this command with the username and group for your system:
$ sudo chown -R hostzop.hostzop /var/www/html/
To ensure that the web server works as expected, create a PHP test file. Create this file with your preferred text editor. The following examples make use of CentOS 7’s default vi text editor.
In the var/www/html directory, create a PHP file called info.php:
/var/www/html/info.php
This creates a blank PHP file in the /var/www/html directory. To enter the vi editor’s INSERT mode, press I. This allows you to type and edit text within the text editor. Enter the following PHP code:
/var/www/html/info.php
<?php phpinfo(); ?>
This PHP code displays information about your server’s PHP environment. When you’ve finished making changes to this file, press the ESC key to exit vi’s INSERT mode. To save and close the file, type:wq – a semicolon and the letter wq in lowercase.
You can check whether your web server displays PHP content correctly by going to your server’s public IP address and then typing /info.php:
http://your_server_IP_address/info.php
In your browser, a page similar to the one below will be displayed:
This page provides information about your server from the standpoint of PHP. It is useful for debugging and ensuring that your settings are correctly applied. After reviewing the pertinent information about your PHP server, it is best to delete this file because it contains sensitive information about your PHP environment and CentOS server.
You can delete this file with rm command:
$ rm /var/www/html/info.php
If you need to access the information again later, you can always recreate this page. Following that, you can use PHP to test the database connection.
Conclusion
Using Apache as a web server, you’ve created a flexible foundation for serving PHP websites and applications to your visitors. You’ve configured Apache to handle PHP requests and a MariaDB database to store data for your website.