LEMP LÀ GÌ?

CÁC BƯỚC CÀI ĐẶT LEMP TRÊN CENTOS 7

CHUẨN BỊ MÔI TRƯỜNG

Hãy chắc chắn rằng server CentOS 7 của bạn đang được cập nhật mới nhất, hoặc gõ lệnh sau để cập nhật và cài phần mềm cần thiết:

yum update -y && yum install nano yum-utils -y

Vô hiệu hoá SELinux theo bài viết: Cách vô hiệu hóa SELinux trên CentOS

Cài đặt các Repo cần thiết

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

 

CÀI ĐẶT NGINX WEB SERVER

Chạy lệnh sau để cài đặt Nginx:

yum install -y nginx

Khởi động Service nginx:

systemctl start nginx.service
systemctl enable nginx.service

Tiếp theo cấu hình firewall mở port cho Nginx, mặc định trong centos 7 sử dụng FirewallD.

firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Để kiểm tra Nginx hoạt động chưa các bạn mở trình duyệt web truy cập địa chỉ ip của server (http://your-ip-address ), nếu xuất hiện màn hình Default page như dưới đây là thành công.

CÀI ĐẶT PHP VÀ MODULE

Enable PHP 7.3 và cài đặt:

yum-config-manager --enable remi-php73
yum install -y php php-fpm php-common

Cài đặt module php cần thiết cho web của bạn, chứ không cần phải bắt buộc cài đặt hết:

  • APCu (php-pecl-apc) – APCu userland caching
  • CLI (php-cli) – Command-line interface for PHP
  • PEAR (php-pear) – PHP Extension and Application Repository framework
  • PDO (php-pdo) – A database access abstraction module for PHP applications
  • MySQL (php-mysqlnd) – A module for PHP applications that use MySQL databases
  • Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
  • XML (php-xml) – A module for PHP applications which use XML
  • MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling

Để cài đặt các module trên dùng lệnh sau:

yum install -y php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pecl-memcache php-pecl-memcached php-mbstring

CẤU HÌNH NGINX

Mở file config của NGINX:

nano /etc/nginx/nginx.conf

Chỉnh worker_processes bằng với số CPU mà bạn muốn Nginx sử dụng để xử lý.

Cấu hình virtual hosts mặc định của Nginx:

nano /etc/nginx/conf.d/default.conf

Xoá nội dung mặc định rồi dán đoạn cấu hình sau vào:

server {
listen 80 default_server;

location / {
        root /var/www/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
   }

error_page 404 /404.html;

location = /404.html {
       root /usr/share/nginx/html;
   }

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_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}

 

CÀI ĐẶT DATABASE SERVER (MYSQL/MARIADB)

MariaDB là một nhánh của nhà phát triển MySQL gốc Monty Widenius. MariaDB tương thích với MySQL và ở đây ta sẽ cài đặt sử dụng MariaDB thay vì MySQL.

Tạo MariaDB YUM repository như sau:

nano /etc/yum.repos.d/MariaDB.repo

Dán đoạn nội dung sau vào file:

# MariaDB 10.3 CentOS repository list
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb] 
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB 
gpgcheck=1

Lưu lại và thoát.

Chạy lệnh update:

yum -y update

Chạy lệnh sau để cài đặt MariaDB:

yum -y install mariadb-server mariadb MariaDB-client

Khởi động dịch vụ Mariadb và cho phép khởi động cùng hệ thống:

systemctl start mariadb.service
systemctl enable mariadb.service

Tiếp theo chúng ta cần cấu hình cho MariaDB trong lần đầu sử dụng:

mysql_secure_installation

Thiết lập như sau:

[root@server1 ~]# mysql_secure_installation

Enter current password for root (enter for none): <-- Enter

Set root password? [Y/n] <-- Enter

New password: <-- Nhập password mới cho Mysql

Re-enter new password: <-- Nhập lại password mới 

Remove anonymous users? [Y/n] <-- Enter

Disallow root login remotely? [Y/n] <-- Enter

Remove test database and access to it? [Y/n] <-- Enter

Reload privilege tables now? [Y/n] <-- Enter

Thanks for using MariaDB!

 

CẤU HÌNH PHP-FPM

Chỉnh sửa user và group

nano /etc/php-fpm.d/www.conf

Điều chỉnh lại User và Group như bên dưới:

user = nginx
group = nginx

Restart dịch vụ php-fpm

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

 

KIỂM TRA HOẠT ĐỘNG

Thư mục gốc web theo cấu hình Virtual host mặc định phía tren là /var/www/html/. Ta tạo một tệp PHP (info.php) trong thư mục này để kiểm tra Apache xử lý PHP.

File info.php sẽ hiển thị thông tin chi tiết phiên bản PHP mà chúng ta cài đặt.

nano /var/www/html/info.php

Dán nội dung sau vào file:

<?php
    phpinfo();
?>

Lưu lại.

Bây giờ các bạn mở trình duyệt lên và gõ địa chỉ: http://your-ip-address/info.php, nếu kết quả hiện thị như hình dưới là việc cài đặt của chúng ta đã thành công.

Như vậy, qua bài viết này Zhost đã hướng dẫn các bạn cài đặt LEMP Stack trên CentOS 7.

Nếu gặp bất kỳ thắc mắc nào, hãy liên hệ với chúng tôi để được giải đáp.

Chúc các bạn thành công!

Leave a Reply

Your email address will not be published. Required fields are marked *