Introduction
LAMP stands for Linux, Apache, MySQL, and PHP. In this tutorial, we will explain how to install LAMP for single or multiple sites.
Prerequisites
- Server with Operating System Linux CentOS 7 Minimal Installation ISO
- Root access
-
SSH Tools
- Putty For Windows
- OpenSSH in Linux/macOS (available by default)
Step 1 - Login and Update System
Before we start, make sure your system has the latest updates for the version of CentOS 7 installed.
yum update -y
If you want to install this in a server/vps, make sure you first login via SSH and then run the command above.
SSH login using a terminal:
Note: In this example 203.0.113.23 is the IP address of your server
Before continuing with the installation, make sure your CentOS has wget
and the nano
text editor installed. If not, install with the following command:
yum install wget nano -y
Step 2 - Install Web Server: Apache
In CentOS, web server Apache provided by httpd
package. Install it with the following command:
yum install httpd -y
After the installation has finished, we need to start the httpd service with the following command:
systemctl start httpd
Now open your server's IP address in your browser. If the default apache page is shown, the installation was successful. Now we need to make sure httpd runs after booting the server, in case of a restart.
systemctl enable httpd
If httpd is not running, you can check if your server has another service running on port 80 with the command netstat -tulpen | grep 80
.
systemctl status httpd
Example output of the command
[root@your_host ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-03-14 08:13:10 EDT; 4s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 15255 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─15255 /usr/sbin/httpd -DFOREGROUND
├─15256 /usr/sbin/httpd -DFOREGROUND
├─15257 /usr/sbin/httpd -DFOREGROUND
├─15258 /usr/sbin/httpd -DFOREGROUND
├─15259 /usr/sbin/httpd -DFOREGROUND
└─15260 /usr/sbin/httpd -DFOREGROUND
Mar 14 08:13:09 your_host systemd[1]: Starting The Apache HTTP Server...
Mar 14 08:13:10 your_host systemd[1]: Started The Apache HTTP Server.
Step 3 - Install MySQL
wget https://repo.mysql.com//mysql80-community-release-el7-2.noarch.rpm
yum localinstall mysql80-community-release-el7-2.noarch.rpm
yum install mysql-community-server -y
We need to start MySQL and enable it by default
systemctl start mysqld
systemctl enable mysqld
Example output of the command
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2019-03-14 08:17:19 EDT; 19s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 15425 (mysqld)
Status: "SERVER_OPERATING"
CGroup: /system.slice/mysqld.service
└─15425 /usr/sbin/mysqld
Mar 14 08:17:13 localhost systemd[1]: Starting MySQL Server...
Mar 14 08:17:19 localhost systemd[1]: Started MySQL Server.
Step 3.1 - Login to MySQL
By default, MySQL will create a temporary password for root, you can check it with this command:
grep 'temporary password' /var/log/mysqld.log
To login, we need to run:
mysql -uroot -p
Now enter the temporary password. After login, you can change the password (recommended), with this command (start from ALTER):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'holuP455##';
Note: 'holuP455##' is just an example. Replace it with your password.
The password needs unique characters, otherwise it will prompt an error like this:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'holuP455';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'holuP455##';
Query OK, 0 rows affected (0.02 sec)
To exit from mysql, run the command: quit
.
Step 4 - Install PHP
Before we install PHP, we need to activate epel and remi, to make sure we get the latest PHP. If we are using the default repository, it will install PHP 5.4 (which is too old).
yum install epel-release -y
yum localinstall http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils -y
yum-config-manager --enable remi-php72
yum update -y
Now run this command to install PHP 7.2 + MySQL Module :
yum install php72 php72-php-mysqlnd -y
Verify the PHP version with the command php -v
:
[root@localhost ~]# php -v
PHP 7.2.16 (cli) (built: Mar 5 2019 14:45:10) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
If you need more php modules, you can search for them with the command:
yum search php72 | more
Step 5 - Install phpMyAdmin (Optional)
You can install phpMyAdmin from packages with the following command:
yum install phpMyAdmin -y
You can't access phpMyAdmin directly after the installation has finished. We need to edit the httpd conf before it can be accessed. Execute the command:
nano /etc/httpd/conf.d/phpMyAdmin.conf
and check this config. Add "Require all granted" to the config.
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
Require all granted <= add this line for allow all access
</RequireAny>
</IfModule>
To access phpMyAdmin, enter the following URL in your browser: http://203.0.113.23/phpmyadmin
Step 6 - Configuring Multiple Sites
Now, we need setup a virtual host. This is required if you will be using a single server for hosting multiple domain names. For example:
- Index Domain: example.com
- Sub Domain: holu.example.com
Step 6.1 - Create Directory
mkdir -p /var/www/html/example.com/public_html
mkdir -p /var/www/html/holu.example.com/public_html
Step 6.2 - Create Example PHP Files
echo "<?php echo 'This is example.com'; ?>" > /var/www/html/example.com/public_html/index.php
echo "<?php echo 'This is holu.example.com'; ?>" > /var/www/html/holu.example.com/public_html/index.php
Step 6.3 - Configure httpd
We need to create new config files.
Settings for example.com domain
Open config file
nano /etc/httpd/conf.d/example.conf
then, paste this config:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/example.com/public_html
ErrorLog /var/www/html/example.com/error.log
</VirtualHost>
Settings for holu.example.com domain
Open config file
nano /etc/httpd/conf.d/holu.example.conf
then, paste this config:
<VirtualHost *:80>
ServerName www.holu.example.com
ServerAlias holu.example.com
DocumentRoot /var/www/html/holu.example.com/public_html
ErrorLog /var/www/html/holu.example.com/error.log
</VirtualHost>
Step 6.4 - Restart httpd Service
You can restart Apache with the following command:
systemctl restart httpd
Now you can access both domains, example.com or holu.example.com, with a single server.
- if you want save all php files, html, css, or js, make sure you save in public_html.
- if you need to add more sites, just repeat step 6 with another name in the conf and the folder.
Conclusion
Your server is now ready to start building websites with static or dynamic content. This all is possible with a single server by using Apache, MySQL, and PHP.