Trong bài viết này mình sẽ hướng dẫn các bạn cách cài đặt bộ Webserver nổi tiếng LEMP bao gồm: Linux + Nginx + MariaDB + PHP (PHP-FPM) trên CentOS 7.

Hướng dẫn cài đặt LEMP (Nginx, PHP, MariaDB) trên CentOS 7

  • Nginx (đọc là Engine x) là một Webserver nhỏ gọn, ít tốn RAM, mạnh mẽ và có hiệu suất hoạt động cao.
  • PHP nên chọn phiên bản 7.4 hoặc 8.1.
  • MariaDB là phiên bản mã nguồn mở của MySQL, hoạt động giống hệt MySQL. Bắt đầu từ CentOS 7 mặc định đã bắt đầu hỗ trợ MariaDB, các bạn nên sử dụng thay cho MySQL ngay từ bây giờ.

Cài đặt Nginx và PHP trên CentOS 7

Các thao tác thực hiện bằng dòng lệnh, dùng phần mềm Bitvise SSH Client để quản trị.

Thêm repo cần thiết

yum -y install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Cài đặt Nginx, PHP

yum --enablerepo=remi,remi-php81 install -y nginx php-fpm php-common

Lưu ý: Thay số 81 bằng 74 nếu muốn cài đặt phiên bản PHP 7.4.

Cài đặt PHP module

yum --enablerepo=remi,remi-php81 install -y php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-mysql php-pgsql php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-bcmath php-intl php-zip php-imagick

Lưu ý: Thay số 81 bằng 74 nếu muốn cài đặt phiên bản PHP 7.4.

Stop httpd (Apache) server, Start Nginx và PHP-FPM

systemctl stop httpd.service
systemctl start nginx.service
systemctl start php-fpm.service

Tự động khởi động Nginx, PHP-FPM và tắt httpd

systemctl disable httpd.service
systemctl enable nginx.service
systemctl enable php-fpm.service

Cấu hình Nginx và PHP-FPM

Cấu hình Nginx

Sửa tập tin /etc/nginx/nginx.conf, chỉnh worker_processes bằng với số CPU của VPS. Bạn có thể dùng lệnh nano hoặc tải về cập nhật bằng Notepad và tải lên lại.

Sửa tập tin /etc/nginx/conf.d/default.conf như sau:

#
# The default server
#
server {
    listen       80;
    server_name example.com;
    root   /usr/share/nginx/html;

    location / {
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_connect_timeout 1000;
        fastcgi_send_timeout 1000;
        fastcgi_read_timeout 1000;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
    }

    include /usr/share/nginx/html/nginx.conf;
}

Thay example.com bằng tên miền của bạn. Sau đó khởi động lại Nginx.

systemctl restart nginx.service

Theo cấu hình này, website sẽ được chứa ở thư mục /usr/share/nginx/html. Thư mục này cần cấp quyền đọc/ghi/xóa/sửa dữ liệu.

cd /usr/share/nginx/html
chown -R nginx:nginx *
Cấu hình PHP-FPM

Sửa tập tin /etc/php-fpm.d/www.conf, Thay user = apachegroup = apache sang nginx.

[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]

Khởi động lại PHP-FPM:

systemctl restart php-fpm.service

Bây giờ, bạn có thể vào địa chỉ IP của VPS để chạy thử website.

Lưu ý: nếu bạn truy cập thẳng vào IP hoặc khi khởi động lại VPS mà báo lỗi không kết nối được thì hãy open port http và https:

systemctl disable firewalld.service
systemctl stop firewalld.service
yum -y install iptables-services
systemctl start iptables.service
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
systemctl enable iptables.service
service iptables save
systemctl restart iptables.service

Cài đặt MariaDB trên CentOS 7

Thêm MariaDB repo

Đến địa chỉ này để lấy cấu hình. Thêm cấu hình vào tập tin /etc/yum.repos.d/MariaDB.repo. Mẫu cho phiên bản MariaDB 10.5 cài trên CentOS 7 64 bit.

# MariaDB 10.5 CentOS repository list - created 2021-09-05 04:37 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Lưu ý: Nếu bạn muốn sử dụng phiên bản MariaDB mặc định (5.5) bạn có thể bỏ qua bước này.

Cài đặt hoặc update MariaDB

yum install -y mariadb mariadb-server

Khởi động MariaDB và tự động chạy khi boot

systemctl start mariadb.service
systemctl enable mariadb.service

Cấu hình MariaDB

/usr/bin/mysql_secure_installation

Đối với MariaDB 10.6 trở lên, các bạn dùng lệnh.

mariadb-secure-installation

Ngay bước đầu tiên bạn sẽ bị hỏi root password, do mới cài đặt nên tất nhiên chưa có password, nhấn Enter để đặt mật khẩu và nhập y ở các câu hỏi tiếp theo.

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] y
New password:
Re-enter new password:
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!

Đăng nhập vào MySQL

Đăng nhập MySQL bạn dùng lệnh: mysql -u root -p. Để tạo một cơ sở dữ liệu, dùng lệnh: CREATE DATABASE tên_csdl;.

Cập nhật VPS

Để cập nhật CentOS trên VPS, dùng lệnh:

yum -y update

Tham khảo thêm: