Giới thiệu
Trong hướng dẫn này, Quân sẽ hướng dẫn bạn cài đặt một webserver sử dụng LAMP trên máy chủ CentOS 8. Mặc dù MySQL có sẵn từ các kho lưu trữ mặc định trong CentOS 8, và hướng dẫn này sẽ hướng dẫn quy trình thiết lập LAMP với MariaDB làm hệ thống quản lý cơ sở dữ liệu.
Điều kiện
Để thực hiện theo hướng dẫn này bạn cần SSH vào CentOS 8 với quyền root, hoặc với tư cách sudo.
Các bước thực hiện
- Lưu ý: Dấu
$
là tập lệnh, bạn sẽ cần copy lệnh sau dấu$
Bước 1: Cài đặt Apache
Bạn cần thực hiện cài đặt gói httpd trước, sử dụng lệnh sau để cài đặt
$ dnf install httpd
Sau khi cài đặt hoàn tất, bạn thực hiện chạy lệnh sau để start httpd
$ systemctl start httpd
Lưu ý: Nếu firewalld đang hoạt động, bạn cần phải mở port 80 (HTTP)
Để kiểm tra Firewalld bạn sử dụng lệnh sau, nếu runing bạn cần mở port, nếu deactivate bạn có thể bỏ qua bước mở port
$ systemctl status firewalld
$ firewall-cmd --permanent --add-service=http
- Reload lại firewalld để áp dụng
$ firewall-cmd --reload
Sau khi đã cài đặt hoàn tất httpd và reload firewalld. Bạn truy cập vào trình duyệt, nhập vào domain đã trỏ về IP hoặc nhập trực tiếp IP Public vào. Bạn sẽ thấy một trang như ảnh dưới là đã hoàn tất bước cài đặt HTTPD
Bước 2: Cài đặt MariaDB
Mình sẽ cài đặt MariaDB, một nhánh phát triển cộng đồng của máy chủ MySQL gốc của Oracle. Để cài bạn thực hiện chạy các lệnh sau
$ dnf install mariadb-server
Quá trình cài đặt kết thúc bạn thực hiện bật và khởi động MariaDB
$ systemctl start mariadb $ systemctl status mariadb
Để cải thiện tính bảo mật máy chủ cơ sở dữ liệu của bạn, bạn sẽ phải chạy lệnh bảo mật được cài đặt sẵn với MariaDB như sau.
$ 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): # Bấm enter 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] y New password: ## Nhập vào password bạn cần đặt cho MariaDB Re-enter new password: ## Nhập ;lại password bạn cần đặt cho MariaDB Password updated successfully! Reloading privilege tables.. ... Success! 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] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! 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!
Bước 3: Cài đặt PHP
Để cài đặt gói php
và php-mysqlnd
bằng trình quản lý gói dnf, bạn chạy lệnh sau.
$ dnf install php php-mysqlnd
Sau khi cài đặt hoàn tất, bạn cần khởi động lại Apache để kích hoạt các module PHP
$ systemctl restart httpd
Bước 4: Kiểm tra PHP với Apache
Cài đặt Apache mặc định trên CentOS 8 sẽ tạo ra một tài liệu gốc nằm ở /var/www/html
. Bạn không cần phải thực hiện bất kỳ thay đổi nào đối với các cài đặt mặc định của Apache để PHP hoạt động chính xácc trong máy chủ web của bạn.
Mình sẽ tạo thử 1 file PHP có tên là info.php tại /var/www/html
với thông tin như sau để kiểm tra và mã PHP sau đây sẽ hiển thị thông tin về môi trường PHP hiện tại đang chạy trên máy chủ:
$ vi /var/www/html/info.php
<? php phpinfo(); ?>
Xin chúc mừng bạn đã thực hiện cài đặt thành công LAMP lên máy chủ CentOS 8 của mình, qua bài viết này mong sẽ có thêm những kiến thức và thông tin bổ ích cho bạn. Hãy theo dõi để cập nhật thêm những thông tin, bài viết mới nhé, Chúc bạn thành công!