Hướng dẫn cài đặt LAMP trên linux

Hướng dẫn cài đặt LAMP trên linux

18/10/2020 Off By dat09

Trong bài này, mình sẽ hướng dẫn cho bạn cách cài đặt một hệ thống LAMP trên hệ điều hành Linux CentOS 7. LAMP bao gồm Linux, Apache, MySQL, PHP. Hướng dẫn này chủ yếu tập trung vào việc cài đặt, bạn có thể tham khảo về khái niệm LAMP chi tiết ở đây[TODO].

Các thành phần sẽ cài đặt

ComponentVersionDescription
Apacheapache-2.4Máy chủ web apache
Mysqlmariadb-server-5.5Hệ cơ sở dữ liệu mysql
PHPphp-7.3Ngôn ngữ lập trình web php

Cài đặt apache

Chạy dòng lệnh bên dưới

[dat09@devk2x.com ~]$ sudo yum install httpd
[sudo] password for dat09:
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.viethosting.com
 * extras: mirrors.viethosting.com
 * updates: mirrors.viethosting.com
base                                                                                                                                  | 3.6 kB  00:00:00
docker-ce-stable                                                                                                                      | 3.5 kB  00:00:00
extras                                                                                                                                | 2.9 kB  00:00:00
updates                                                                                                                               | 2.9 kB  00:00:00
(1/3): extras/7/x86_64/primary_db                                                                                                     | 206 kB  00:00:00
(2/3): updates/7/x86_64/primary_db                                                                                                    | 4.5 MB  00:00:01
(3/3): docker-ce-stable/x86_64/primary_db                                                                                             |  46 kB  00:00:01
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-93.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-93.el7.centos for paackage: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-93.el7.centos.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-93.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                               Arch                             Version                                         Repository                      Size
=============================================================================================================================================================
Installing:
 httpd                                 x86_64                           2.4.6-93.el7.centos                             base                           2.7 M
Installing for dependencies:
 httpd-tools                           x86_64                           2.4.6-93.el7.centos                             base                            92 k
 mailcap                               noarch                           2.1.41-2.el7                                    base                            31 k

Transaction Summary
=============================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 2.8 M
Installed size: 9.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): httpd-tools-2.4.6-93.el7.centos.x86_64.rpm                                                                                     |  92 kB  00:00:00
(2/3): mailcap-2.1.41-2.el7.noarch.rpm                                                                                                |  31 kB  00:00:00
(3/3): httpd-2.4.6-93.el7.centos.x86_64.rpm                                                                                           | 2.7 MB  00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                        3.0 MB/s | 2.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mailcap-2.1.41-2.el7.noarch                                                                                                               1/3
  Installing : httpd-tools-2.4.6-93.el7.centos.x86_64                                                                                                    2/3
  Installing : httpd-2.4.6-93.el7.centos.x86_64                                                                                                          3/3
  Verifying  : httpd-tools-2.4.6-93.el7.centos.x86_64                                                                                                    1/3
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                                                                               2/3
  Verifying  : httpd-2.4.6-93.el7.centos.x86_64                                                                                                          3/3

Installed:
  httpd.x86_64 0:2.4.6-93.el7.centos

Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-93.el7.centos                                           mailcap.noarch 0:2.1.41-2.el7

Complete!

Vậy là đã cài xong apache 2.4, start apache.

[dat09@devk2x.com ~]$ sudo systemctl start httpd

Để kiểm tra trạng thái sau khi cài xong

[dat09@devk2x.com ~]$ sudo 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 Sat 2020-10-03 10:14:46 +07; 51min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1083 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/s                                                                             ec"
   CGroup: /system.slice/httpd.service
           ├─1083 /usr/sbin/httpd -DFOREGROUND
           ├─1109 /usr/sbin/httpd -DFOREGROUND
           ├─1110 /usr/sbin/httpd -DFOREGROUND
           ├─1111 /usr/sbin/httpd -DFOREGROUND
           ├─1112 /usr/sbin/httpd -DFOREGROUND
           └─1113 /usr/sbin/httpd -DFOREGROUND

Oct 03 10:14:45 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
Oct 03 10:14:46 localhost.localdomain httpd[1083]: AH00558: httpd: Could not ...
Oct 03 10:14:46 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.

Mặc định khi cài linux centos thì firewall sẽ chặn requset http ở port 80, do vậy chúng ta phải cấu hình để firewall cho phép chúng ta request từ trình duyệt web với port 80 tới server.

[dat09@devk2x.com ~]$ sudo firewall-cmd --permanent --add-service=http
success
[dat09@devk2x.com ~]$ sudo firewall-cmd --reload
success

Kiểm tra địa chỉ server

[dat09@devk2x.com ~]$ hostname -I
192.168.1.105 192.168.56.102 10.0.4.15

Ở đây có 3 ip vì khi cài đặt centos thì mặc định sẽ có 3 card mạng tương ứng với một địa chỉ IP, mình có thể sử dụng một trong ba cái IP trên để request tới server.

Mở trình duyện và gõ http://your-ip , nếu không gặp vấn đề gì thì sẽ hiển thị trang test của apache server như bên dưới.

Cài đặt MySQL (MariaDB)

MariaDB là một nhánh của hệ quản trị cơ sở dữ liệu Mysql , phần chi tiết bạn có thể tham khảo ở đây[TODO].

Còn bây giờ để cài đặt mariaDB thì hãy chạy lệnh bên dưới

[dat09@devk2x.com ~]$ sudo yum install mariadb-server mariadb
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: centos-hcm.viettelidc.com.vn
 * extras: centos-hcm.viettelidc.com.vn
 * updates: centos-hcm.viettelidc.com.vn
base                                                                                                                                  | 3.6 kB  00:00:00
docker-ce-stable                                                                                                                      | 3.5 kB  00:00:00
extras                                                                                                                                | 2.9 kB  00:00:00
updates                                                                                                                               | 2.9 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.65-1.el7 will be installed
---> Package mariadb-server.x86_64 1:5.5.65-1.el7 will be installed
--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.65-1.el7.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.65-1.el7.x86_64
--> Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.65-1.el7.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.023-6.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
--> Running transaction check
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                                         Arch                           Version                                   Repository                    Size
=============================================================================================================================================================
Installing:
 mariadb                                         x86_64                         1:5.5.65-1.el7                            base                         8.7 M
 mariadb-server                                  x86_64                         1:5.5.65-1.el7                            base                          11 M
Installing for dependencies:
 perl-Compress-Raw-Bzip2                         x86_64                         2.061-3.el7                               base                          32 k
 perl-Compress-Raw-Zlib                          x86_64                         1:2.061-4.el7                             base                          57 k
 perl-DBD-MySQL                                  x86_64                         4.023-6.el7                               base                         140 k
 perl-DBI                                        x86_64                         1.627-4.el7                               base                         802 k
 perl-IO-Compress                                noarch                         2.061-2.el7                               base                         260 k
 perl-Net-Daemon                                 noarch                         0.48-5.el7                                base                          51 k
 perl-PlRPC                                      noarch                         0.2020-14.el7                             base                          36 k

Transaction Summary
=============================================================================================================================================================
Install  2 Packages (+7 Dependent packages)

Total download size: 21 M
Installed size: 110 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): mariadb-server-5.5.65-1.el7.x86_64.rpm                                                                                         |  11 MB  00:00:03
(2/9): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                                                                 |  32 kB  00:00:00
(3/9): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                                                                  |  57 kB  00:00:00
(4/9): perl-DBD-MySQL-4.023-6.el7.x86_64.rpm                                                                                          | 140 kB  00:00:00
(5/9): perl-DBI-1.627-4.el7.x86_64.rpm                                                                                                | 802 kB  00:00:00
(6/9): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                                                        | 260 kB  00:00:00
(7/9): mariadb-5.5.65-1.el7.x86_64.rpm                                                                                                | 8.7 MB  00:00:04
(8/9): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                                                          |  51 kB  00:00:00
(9/9): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                                                            |  36 kB  00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                        5.2 MB/s |  21 MB  00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mariadb-5.5.65-1.el7.x86_64                                                                                                             1/9
  Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                2/9
  Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                               3/9
  Installing : perl-IO-Compress-2.061-2.el7.noarch                                                                                                       4/9
  Installing : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                         5/9
  Installing : perl-PlRPC-0.2020-14.el7.noarch                                                                                                           6/9
  Installing : perl-DBI-1.627-4.el7.x86_64                                                                                                               7/9
  Installing : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                         8/9
  Installing : 1:mariadb-server-5.5.65-1.el7.x86_64                                                                                                      9/9
  Verifying  : 1:mariadb-server-5.5.65-1.el7.x86_64                                                                                                      1/9
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                         2/9
  Verifying  : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                         3/9
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                                                                                       4/9
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                               5/9
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                                                                                               6/9
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                7/9
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                                                                                           8/9
  Verifying  : 1:mariadb-5.5.65-1.el7.x86_64                                                                                                             9/9

Installed:
  mariadb.x86_64 1:5.5.65-1.el7                                             mariadb-server.x86_64 1:5.5.65-1.el7

Dependency Installed:
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBD-MySQL.x86_64 0:4.023-6.el7 perl-DBI.x86_64 0:1.627-4.el7
  perl-IO-Compress.noarch 0:2.061-2.el7        perl-Net-Daemon.noarch 0:0.48-5.el7         perl-PlRPC.noarch 0:0.2020-14.el7

Complete!

Sau khi quá trình cài đặt xong thì chúng ta cần start MariaDB

[dat09@devk2x.com ~]$ sudo systemctl start mariadb
[dat09@devk2x.com ~]$

Bây giờ cơ sở dữ liệu MySQL đã được chạy, tuy nhiên chúng ta cần chạy một tập lệnh để setting một số chính sách bảo mật đơn giản để an toàn hơn cho cơ sở giữ liệu.

[dat09@devk2x.com ~]$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

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

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

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Cài đặt PHP

Để cài đặt chúng ta chạy lệnh bên dưới

[dat09@devk2x.com ~]$ sudo yum install php php-mysql
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos-hcm.viettelidc.com.vn
 * extras: centos-hcm.viettelidc.com.vn
 * updates: centos-hcm.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.4.16-48.el7 for package: php-5.4.16-48.el7.x86_64
---> Package php-mysql.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 5.4.16-48.el7 for package: php-mysql-5.4.16-48.el7.x86_64
--> Running transaction check
---> Package php-cli.x86_64 0:5.4.16-48.el7 will be installed
---> Package php-common.x86_64 0:5.4.16-48.el7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-48.el7.x86_64
---> Package php-pdo.x86_64 0:5.4.16-48.el7 will be installed
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                                Arch                               Version                                    Repository                        Size
=============================================================================================================================================================
Installing:
 php                                    x86_64                             5.4.16-48.el7                              base                             1.4 M
 php-mysql                              x86_64                             5.4.16-48.el7                              base                             102 k
Installing for dependencies:
 libzip                                 x86_64                             0.10.1-8.el7                               base                              48 k
 php-cli                                x86_64                             5.4.16-48.el7                              base                             2.7 M
 php-common                             x86_64                             5.4.16-48.el7                              base                             565 k
 php-pdo                                x86_64                             5.4.16-48.el7                              base                              99 k

Transaction Summary
=============================================================================================================================================================
Install  2 Packages (+4 Dependent packages)

Total download size: 4.9 M
Installed size: 18 M
Is this ok [y/d/N]: y
Downloading packages:
(1/6): libzip-0.10.1-8.el7.x86_64.rpm                                                                                                 |  48 kB  00:00:00
(2/6): php-5.4.16-48.el7.x86_64.rpm                                                                                                   | 1.4 MB  00:00:01
(3/6): php-common-5.4.16-48.el7.x86_64.rpm                                                                                            | 565 kB  00:00:00
(4/6): php-mysql-5.4.16-48.el7.x86_64.rpm                                                                                             | 102 kB  00:00:00
(5/6): php-pdo-5.4.16-48.el7.x86_64.rpm                                                                                               |  99 kB  00:00:00
(6/6): php-cli-5.4.16-48.el7.x86_64.rpm                                                                                               | 2.7 MB  00:00:01
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                        2.7 MB/s | 4.9 MB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libzip-0.10.1-8.el7.x86_64                                                                                                                1/6
  Installing : php-common-5.4.16-48.el7.x86_64                                                                                                           2/6
  Installing : php-cli-5.4.16-48.el7.x86_64                                                                                                              3/6
  Installing : php-pdo-5.4.16-48.el7.x86_64                                                                                                              4/6
  Installing : php-mysql-5.4.16-48.el7.x86_64                                                                                                            5/6
  Installing : php-5.4.16-48.el7.x86_64                                                                                                                  6/6
  Verifying  : php-cli-5.4.16-48.el7.x86_64                                                                                                              1/6
  Verifying  : php-pdo-5.4.16-48.el7.x86_64                                                                                                              2/6
  Verifying  : php-mysql-5.4.16-48.el7.x86_64                                                                                                            3/6
  Verifying  : libzip-0.10.1-8.el7.x86_64                                                                                                                4/6
  Verifying  : php-common-5.4.16-48.el7.x86_64                                                                                                           5/6
  Verifying  : php-5.4.16-48.el7.x86_64                                                                                                                  6/6

Installed:
  php.x86_64 0:5.4.16-48.el7                                                 php-mysql.x86_64 0:5.4.16-48.el7

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7        php-cli.x86_64 0:5.4.16-48.el7        php-common.x86_64 0:5.4.16-48.el7        php-pdo.x86_64 0:5.4.16-48.el7

Complete!

Restart php service

[dat09@devk2x.com ~]$ sudo systemctl restart httpd.service
[dat09@devk2x.com ~]$

Kiểm tra thông tin php được cài đặt trên web , tạo file php info

[dat09@devk2x.com ~]$ sudo vi /var/www/html/info.php

Input nội dung sau

<?php phpinfo(); ?>

Gõ trên trình duyệt: http://your_server_IP_address/info.php

Đến đây thì chúng ta đã cài đặt xong LAMP trên linux, từ bây giờ đã có thể bắt đầu build một dự án trên server vừa cài xong, chi tiết hơn thì bạn có thể coi chi tiết cách cài đặt cấu hình một website trên máy chủ linux[TODO].