HomeLinuxCài đặt LEMP Stack cơ bản trên CentOS 7

Cài đặt LEMP Stack cơ bản trên CentOS 7

by Quốc Cường
15 views

LEMP là gì?

LEMP là một nhóm phần mềm nguồn mở được cài đặt cùng nhau (tương tự như LAMP) để cho phép máy chủ lưu trữ các trang web động và ứng dụng web. Thuật ngữ này là một từ viết tắt đại diện cho hệ điều hành Linux, với máy chủ web Nginx, máy chủ cơ sở dữ liệu MariaDB hoặc MySQL và nội dung động được xử lý bởi PHP.

Cài đặt LEMP Stack

Bước 1: cài đặt NGINX

Trước tiên chúng ta cần cài đặt Epel repository

root@systuts:~# yum install epel-release
    

Bây giờ kho lưu trữ EPEL đã được cài đặt trên máy chủ của bạn, hãy cài đặt Nginx bằng lệnh yum sau:

root@systuts:~# yum install -y nginx
    

Khi quá trình cài đặt hoàn tất, chúng ta cần khởi động NGINX và cho phép tự khởi động khi boot bằng lệnh sau:

root@systuts:~# systemctl enable nginx
root@systuts:~# systemctl enable nginx
    

Mở port HTTP và HTTPS trên Firewalld và kiểm tra truy cập:

root@systuts:~# firewall-cmd --permanent --add-service=http
root@systuts:~# firewall-cmd --permanent --add-service=https
root@systuts:~# firewall-cmd --reload
    

Reload lại Firewalld

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

root@systuts:~# yum install -y mariadb-server mariadb
    

Khi quá trình cài đặt hoàn tất, chúng ta cần khởi động MariaDB và cho phép tự khởi động khi boot bằng lệnh sau:

root@systuts:~# systemctl enable mariadb
root@systuts:~# systemctl start mariadb
    

Bây giờ MariaDB Server của chúng ta đã chạy, bước tiếp theo chúng ta cần bảo mật cho MariaDB Server bằng cách chạy lệnh sau:

root@systuts:~#  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] 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!

    

Bước 3: cài đặt PHP

Phiên bản PHP có sẵn theo mặc định trong các máy chủ CentOS 7 đã lỗi thời và vì lý do đó, chúng ta sẽ cần cài đặt kho lưu trữ gói của bên thứ ba để có được PHP 7+ và cài đặt nó trên máy chủ CentOS 7 của bạn.

Remi là kho lưu trữ gói phổ biến cung cấp các bản phát hành PHP cập nhật nhất cho các máy chủ CentOS để cài đặt kho Remi cho CentOS 7, hãy chạy:

root@systuts:~# yum install -y yum-utils http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    

Sau khi cài đặt xong, bạn sẽ cần chạy một lệnh để kích hoạt kho lưu trữ chứa phiên bản PHP ưa thích của bạn.

Note: bạn có thể cài đặt các phiên bản php khác bằng cách thay số hiệu phiên bản như sau: yum-config-manager --enable remi-php[number]

Trong hướng dẫn này, chúng ta sẽ cài đặt PHP 7.4, hiện là phiên bản ổn định được cập nhật nhất của PHP. Để kích hoạt gói Remi chính xác để cài đặt PHP 7.4, hãy chạy:

Bây giờ chúng ta có thể tiến hành sử dụng yum để cài đặt PHP như bình thường. Lệnh sau sẽ cài đặt tất cả các gói cần thiết để cài đặt PHP 7.4 trong Nginx và cho phép nó kết nối với cơ sở dữ liệu dựa trên MySQL:

root@systuts:~# yum-config-manager --enable remi-php74
root@systuts:~# yum install -y php php-fpm php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc php-opcache php-mysql
    

Để xác nhận rằng PHP có sẵn như là phiên bản bạn đã chọn, hãy chạy:

root@systuts:~# php --version
PHP 7.4.11 (cli) (built: Sep 29 2020 10:17:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.11, Copyright (c), by Zend Technologies
    

PHP hiện đã được cài đặt thành công trên hệ thống của bạn. Tiếp theo, chúng ta cần thực hiện một vài điều chỉnh cho cấu hình mặc định.

Mở tệp cấu hình /etc/php-fpm.d/www.conf bằng vi hoặc trình chỉnh sửa bạn chọn:

root@systuts:~# vi /etc/php-fpm.d/www.conf
    

Tìm các giá trị sau và sửa theo đề xuất bên dưới.

user = apache               user = nginx
group = apache               group = nginx
listen = 127.0.0.1:9000    =====>     listen = /var/run/php_fpm.sock
;listen.owner = nobody               listen.owner = nginx
;listen.group = nobody              listen.group = nginx
;listen.mode = 0660              listen.mode = 0660

Save lại và khởi động PHP-FPM, chạy lệnh:

root@systuts:~# systemctl enable php-fpm
root@systuts:~# systemctl start php-fpm
    

Bước 4: cấu hình NGINX xử lý các mã nguồn PHP

Chỉnh sửa file default.conf, nếu không có thì tạo mới bằng lệnh:

root@systuts:~# vi /etc/nginx/conf.d/default.conf
    

Dán đoạn cấu hình sau vào default.conf và thay server_domain_or_IP bằng IP VPS của bạn.

server {
    listen  80;
    server_name  server_domain_or_IP;

    root   /var/www/html/;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php_fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Lưu config và restart lại NGINX:

root@systuts:~# systemctl restart nginx
    

Bước 5: Upload website và kiểm tra

Như vậy, sau khi hoàn tất các bước trên bạn đã có cho mình một Web Server LEMP cơ bản có thể chạy được website.

Bước tiếp theo chúng ta cần tạo 1 trang web mẫu để kiểm tra, chạy lệnh:

root@systuts:~# touch /var/www/html/index.php | echo "Hello Systuts.net';?>" > /var/www/html/index.php
    

Sau khi hoàn tất, các bạn mở trình duyệt và truy cập IP của Server hoặc domain để hiển thị trang web:

Cài đặt LEMP Stack cơ bản trên CentOS 7

Note: Sau khi trang web hiển thị thành công các bạn đã có thể upload source code website của các bạn lên server.

Bước 6: cài đặt phpmyadmin quản lý database

Các bạn download source phpmyadmin về bằng lệnh:

root@systuts:~# cd ~ && wget https://files.phpmyadmin.net/phpMyAdmin/4.9.6/phpMyAdmin-4.9.6-english.tar.gz
    

Giải nén source vừa tải về:

root@systuts:~# tar -xzvf phpMyAdmin-4.9.6-english.tar.gz
root@systuts:~# mv phpMyAdmin-4.9.6-english /usr/share/phpmyadmin
    

Thêm đoạn config sau vào /etc/nginx/conf.d/default.conf:

root@systuts:~# vi /etc/nginx/conf.d/default.config
    
server {
    listen  80;
    server_name  server_domain_or_IP;

    root   /var/www/html/;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php_fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
location  /phpmyadmin{
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php_fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}

Save lại và restart nginx, mở trình duyệt và nhập vào đường dẫn: http://your_ip_address/phpmyadmin nhập vào username root password của MariaDB đã cài đặt ở bước 2.

Cài đặt LEMP Stack cơ bản trên CentOS 7

Troubleshoot

Cài đặt LEMP Stack cơ bản trên CentOS 7

Nếu các bạn vào phpmyadmin bị lỗi session, chạy lệnh:

root@systuts:~# chown -R nginx:nginx /var/lib/php/session/
root@systuts:~# chmod 700 /var/lib/php/session
    

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

You may also like

Leave a Comment