How to build a LAMP stack (Linux, Apache, MySQL, PHP) on CentOS 7
build LAMP
install apache
install mysql
install php
centOS 7
This article will present how to build a LAMP. LAMP is just an acronym for the software the stack uses.
- Linux - Operating System.
- Apache - HTTP server
- MySQL/MariaDB - database.
- PHP - programming language.
The article assumes that the Linux is already installed, locally or a small VM in the cloud.
Step 1 - Install Apache
-
Before you begin any installation, make sure that your software is up to date. To do that run the following command:
$ sudo yum update
-
Install Apache:
$ sudo yum install httpd
-
Start Apache:
$ sudo systemctl start httpd.service
-
Set Apache to start on server boot:
$ sudo systemctl enable httpd.service
-
Verify that Apache is installed by going to using any web browser:
http://the server's IP address.
If Apache is installed, the Apache Test Page displays like in the image bellow.
-
Open the 80 port
If the page is not displayed most probably the port 80 is not open for public access (from outside of the server). To open the port follow the next steps:
-
Run the command to add the port 80 to the firewall
$ firewall-cmd --zone=public --add-port=2888/tcp --permanent
-
Run the command to reload the new rule added
$ firewall-cmd --zone=public --add-port=2888/tcp --permanent
-
Install MySQL
-
Install MariaDB, which is a community-developed fork of MySQL:
$sudo yum install mariadb-server mariadb
-
Start the service:
$ sudo systemctl start mariadb
-
Set MySQL to start on server boot:
$ sudo systemctl enable mariadb.service
-
Run this command to finish setting up the installation:
$ sudo mysql_secure_installation
You will be asked for the root password. Because you didn't set it earlier, press Enter to set a password now.
- Type "Y" to set the root password.
- Enter and confirm the new password.
- You will be asked more questions as part of the security configuration. It is a best practice to respond "Y" to these system prompts because will disable the remote access for root user, will delete test database and will remove the anonymous user.
Install PHP
-
Install PHP:
$ sudo yum install php php-mysql
Enter "Y" + Enter key to install.
- Restart Apache to read the new settings:
$ sudo systemctl restart httpd.service
Install PHP modules
If your applications require any PHP modules, you can install them using the following steps:
-
View available PHP modules:
$ yum search php-
This command will help you to see the exact module name.
-
The list displays the available PHP modules. To view the description of a specific package, use the following command:
$ yum info <the name of the package>
-
Install the package you want:
$sudo yum install <the name of the package>
Note: don't forget to restart apache after the module installation.
Test PHP processing on Apache
-
Create a new PHP file under the
/var/www/html
directory:$ sudo vi /var/www/html/info.php
-
When the file opens type i (or Insert) key to start the insert mode, then type in the following code:
<?php phpinfo(); ?>
-
Press Esc to exit the insert mode and then type:
:wq!
to save the changes.
-
To verify it worked, type this URL into your browser:
http://the server's IP address/info.php
A page displays with the PHP version, extensions, build date, and other information.
Now you are ready to deploy dynamic web application.