How to Install LAMP on CentOS 7
LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP. LAMP is a common web stack used for hosting web content. LAMP is a combination of operating system and open-source software stack.
As a prerequisite, you should already have CentOS 7 Linux installed on your server. Login to SSH as a non-root user. Commands that require elevated privileges are prefixed with sudo.
Adding and Managing Users on CentOS 7
Installing LAMP includes five basic things:
- Check hostname and update system
- Install Apache Webserver
- Install MySQL
- Install PHP
- Install phpMyAdmin
Check hostname and update system
1. To check your hostname, type:
hostname
2. To update your system:
sudo yum update
Install Apache Webserver
Apache is an open-source multi-platform web server.
1. To install Apache, enter the following command:
sudo yum install httpd
2. Start the Apache service:
sudo systemctl start httpd.service
3. To start automatically on every reboot:
sudo systemctl enable httpd.service
4. To test successful installation of Apache webserver, open your web browser (like Google Chrome) and navigate to http://server-ip-address/
Install MySQL
MySQL is replaced with MariaDB in CentOS 7. MariaDB is a drop in replacement for MySQL.
1. To install the MariaDB-server package:
sudo yum install mariadb-server
2. Start automatically on every reboot:
sudo systemctl enable mariadb.service
3. Start MariaDB service:
sudo systemctl start mariadb.service
4. Secure MySQL Installation
Run mysql_secure_installation to secure MariaDB. You will be given the option to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options.
By default, MySQL root password is empty. So, to prevent unauthorized access to MySQL, set root user password. Enter the following command to setup mysql root user password:
mysql_secure_installation
All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Install PHP
1. To install PHP:
sudo yum install php php-pear
2. If you need to install MySQL support for PHP, you also need to install the php-mysql package:
sudo yum install php-mysql
3. Restart Apache webserver:
sudo systemctl restart httpd.service
Install phpMyAdmin
Unfortunately, phpMyAdmin is not available in default repository of CentOS 7. You need to add an additional repo to your system. The EPEL repo (Extra Packages for Enterprise Linux) contains many additional packages, including the phpMyAdmin package.
The EPEL repository can be made available to your server by installing a special package called epel-release. This will reconfigure your repository list and give you access to the EPEL packages.
1. To install epel package:
sudo yum install epel-release
2. Now, you can install the phpMyAdmin package using the yum packaging system:
sudo yum install phpmyadmin
The installation included an Apache configuration file that has already been put into place. You need to modify this to work correctly. Open the file in your text editor:
sudo nano /etc/httpd/conf.d/phpMyAdmin.conf
There are some directory blocks with some conditional logic to explain the access policy for the directory. Currently, this setup is configured to deny access to any connection not being made from the server itself. By default, phpMyAdmin can only be accessed from the localhost itself.
Find and comment the whole /<Directory> section by inserting a hash sign (#) at the beginning of each line.
Add the following lines:
<Directory /usr/share/phpMyAdmin/>
Options none
AllowOverride Limit
Require all granted
</Directory>
Finally, restart the Apache web server to implement your modifications:
sudo systemctl restart httpd.service
To access the interface, go to your server's domain name or public IP address followed by /phpMyAdmin, in your web browser:
http://server_domain_or_IP/phpMyAdmin