Hướng Dẫn Cài đặt Và Cấu Hình LEMP Stack Trên CentOS 7 - 123HOST

Giới thiệu

LEMP Stack là một nhóm các phần mềm nguồn mở được cài đặt cùng nhau để xây dựng server để quản lý các website động và ứng dụng web. Thuật ngữ này là từ viết tắt từ chính 4 thành phần của nó:

  • L - Hệ điều hành Linux.
  • E - Nginx phát âm là "engine-ex", là một webserver nhanh, nhẹ, hiệu suất cao.
  • M - MySQL hoặc MariaDB, hệ quản trị cơ sở dữ liệu quan hệ.
  • P - Ngôn ngữ lập trình PHP.

Trong bài viết này, mình sẽ hướng dẫn các bạn cài đặt và cấu hình LEMP trên hệ điệu hành CentOS 7.

Yêu cầu thiết yếu

  • Distro CentOS 7
  • Sử dụng user có quyền sudo
  • Có kiến thức cơ bản về PHP, MySQL

Bước 1: Cài đặt Nginx

Nginx không có sẵn trong kho lưu trữ CentOS mặc định nên trước tiên chúng ta cần thêm kho lưu trữ EPEL bằng cách chạy lệnh sau:

bash (non-root)sudo yum install epel-release -y _

Tiếp theo, cài đặt Nginx bằng lệnh sau:

bash (non-root)sudo yum install nginx -y _

Sau khi đã cài đặt xong, khởi đông và enable Nginx bằng cách:

bash (non-root)sudo systemctl enable --now nginx _

Kiểm tra trạng thái của Nginx:

bash (non-root)systemctl status nginx _ Kết quả● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2022-01-06 23:23:40 +07; 10s ago Process: 1445 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1442 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1441 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1447 (nginx) CGroup: /system.slice/nginx.service ├─1447 nginx: master process /usr/sbin/nginx └─1448 nginx: worker process Jan 06 23:23:40 centos1 systemd[1]: Starting The nginx HTTP and reverse proxy server... Jan 06 23:23:40 centos1 nginx[1442]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Jan 06 23:23:40 centos1 nginx[1442]: nginx: configuration file /etc/nginx/nginx.conf test is successful Jan 06 23:23:40 centos1 systemd[1]: Started The nginx HTTP and reverse proxy server.

Chú ý, mặc định Nginx hoạt động ở cổng 80, vì vậy nếu đang sử dụng firewalld hoặc iptables thì cần cấu hình mở cổng 80 như sau:

bash (non-root)sudo firewall-cmd --zone=public --permanent --add-service=http && sudo firewall-cmd --reload _

hoặc:

bash (non-root)sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT _

Kiểm tra xem Nginx đã bind trên cổng 80 hay chưa bằng lệnh:

bash (non-root)sudo netstat -pnltu | grep nginx _ Kết quảtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1402/nginx: master tcp6 0 0 :::80 :::* LISTEN 1402/nginx: master

Lưu ý, để sử dụng được lệnh netstat, cần cài đặt gói net-tools:

bash (non-root)sudo yum install net-tools _

Chú ý, nếu Nginx không bind được vào cổng 80 thì cần thay đổi trạng thái của SELinux thành permissive hoặc disabled và restart Nginx:

bash (non-root)sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && sudo setenforce 0 && sudo systemctl restart nginx _

Để chắc chắn Nginx đã cài đặt chính xác, trên trình duyệt web, hãy truy cập địa chỉ IP của server, kết quả nhận được sẽ là 1 trang HTML giống như này:

nginx default webpage

Hướng dẫn xem IP hiện tại của server: link

Nếu bạn sử dụng server chỉ có giao diện dòng lệnh (CLI) thì đừng lo, vẫn có cách kiểm tra bằng công cụ curl:

bash (non-root)curl http://<ip_của_server> _

Kết quả trả về nội dung của trang web mặc định của Nginx:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Welcome to CentOS</title> <style rel="stylesheet" type="text/css"> ... </body> </html>

Bước 2: Cài đặt MariaDB

Lưu ý: Kho lưu trữ mặc định của CentOS 7 sẽ cài đặt MariaDB ngay cả khi bạn gõ lệnh cài đặt MySQL. MariaDB là một nhánh dựa trên MySQL do cộng đồng phát triển khi Oracle mua lại MySQL. Hai cơ sở dữ liệu hoạt động gần như tương tự nhau.

MariaDB 5.5 được cung cấp đi kèm với kho lưu trữ măc định của CentOS. Tuy khá ổn định nhưng phiên bản này đã rất lỗi thời.

123HOST đã có bài viết hướng dẫn cài MariaDB mới nhất, bạn có thể xem tại đây

Trong bài viết này, mình sẽ hướng dẫn thêm 1 cách để cài đặt bất kỳ phiên bản MariaDB nào

Đầu tiên, hãy truy cập vào trang kho lưu trữ của MariaDB và tạo tệp repo cho phiên bản MariaDB mà bạn muốn. Với MariaDB 10.6 trên CentOS 7 thì cần chọn như sau:

mariadb repo

Lưu ý: những phiên bản có tag RC hay alpha đều chưa ổn định, tốt nhất là bạn không nên chọn nó.

Bây giờ, copy và paste nội dung vừa sinh ra vào 1 tệp trong đường dẫn /etc/yum.repos.d :

bash (non-root)sudo vi /etc/yum.repos.d/MariaDB.repo _ /etc/yum.repos.d/MariaDB.repo# MariaDB 10.6 CentOS repository list - created 2022-01-07 08:37 UTC # https://mariadb.org/download/ [mariadb] name = MariaDB baseurl = https://download.nus.edu.sg/mirror/mariadb/yum/10.6/centos7-amd64 gpgkey=https://download.nus.edu.sg/mirror/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1

Lưu và thoát file, sau đó tiến hành cài đặt MariaDB bằng lệnh:

bash (non-root)sudo yum install MariaDB-server MariaDB-client -y _

Tiếp theo, khi quá trình cài đặt hoàn tất, hãy khởi động và enable MariaDB:

bash (non-root)sudo systemctl enable --now mariadb _

Kiểm tra trạng thái của MariaDB:

bash (non-root)systemctl status mariadb _ Kết quả● mariadb.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2021-12-24 11:16:32 +07; 10s ago Process: 16823 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS) Process: 16740 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS) Main PID: 16822 (mysqld_safe) CGroup: /system.slice/mariadb.service ├─16822 /bin/sh /usr/bin/mysqld_safe --basedir=/usr └─16987 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-f... Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: MySQL manual for more instructions. Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: Please report any problems at http://mariadb.org/jira Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: The latest information about MariaDB is available at http://mariadb.org/. Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: You can find additional information about the MySQL part at: Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: http://dev.mysql.com Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: Consider joining MariaDB's strong and vibrant community: Dec 24 11:16:30 centos1 mariadb-prepare-db-dir[16740]: https://mariadb.org/get-involved/ Dec 24 11:16:30 centos1 mysqld_safe[16822]: 211224 11:16:30 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'. Dec 24 11:16:30 centos1 mysqld_safe[16822]: 211224 11:16:30 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql Dec 24 11:16:32 centos1 systemd[1]: Started MariaDB database server.

Cấu hình bảo mật cho MariaDB:

bash (non-root)sudo mariadb-secure-installation _

MariaDB sẽ yêu cầu cung cấp root password hiện tại (root password cho MariaDB chứ không phải root password của CentOS), nhưng chúng ta chỉ vừa mới cài đặt MariaDB. Vì vậy, chỉ cần nhấn enter:

Kết quả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...

Tiếp theo, Switch to unix_socket authentication: xác thực bằng unix_socket, chọn Y để bật, n để tắt.

Kết quảSetting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation. You already have your root account protected, so you can safely answer 'n'. Switch to unix_socket authentication [Y/n] Y Enabled successfully! Reloading privilege tables.. ... Success! You already have your root account protected, so you can safely answer 'n'.

Sau đó, prompt sẽ hỏi chúng ta có muốn đặt mật khẩu mới cho MariaDB root user hay không. Nhấn Enter, nhập mật khẩu mà bạn muốn và xác nhận lại mật khẩu đó:

Kết quảChange the root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!

Nhấn ENTER cho các câu hỏi còn lại để chấp nhận các giá trị mặc định. Thao tác này sẽ xóa một số người dùng và cơ sở dữ liệu mẫu, vô hiệu hóa đăng nhập root từ xa (chỉ được phép đăng nhập root từ localhost) và reload cấu hình.

Kết quả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] ... 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] ... 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] - 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] ... 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ây giờ, MariaDB Server, tiến hành đăng nhập bằng root user mà chúng ta vừa đặt mật khẩu:

bash (non-root)mysql -u root -p _

Sử dụng cú pháp MySQL để tạo database:

bashCREATE DATABASE example_database; _

Từ khóa » Cài đặt Lemp Server 2012