LEMP là gì?
LEMP là một bộ công cụ phần mềm để xây dựng và chạy các ứng dụng web trên các máy chủ. Nó tương tự như LAMP, nhưng thay vì sử dụng Apache làm máy chủ web, nó sử dụng Nginx (engine x) thay thế.
LEMP là viết tắt của:
- L: Linux, hệ điều hành sử dụng để chạy máy chủ.
- E: Nginx (engine x), máy chủ web được sử dụng để chạy ứng dụng web.
- M: MySQL hoặc MariaDB, cơ sở dữ liệu quan hệ được sử dụng để lưu trữ dữ liệu của ứng dụng web.
- P: PHP, ngôn ngữ lập trình được sử dụng để phát triển các ứng dụng web động.
Các bước cài đặt LEMP
Bước 1. Chuẩn bị CentOS 7
Để đảm bảo máy chủ CentOs7 được cập nhật mới nhất bằng lệnh.
yum update -y
Bước 2: Cài đặt các Repo cần thiết
CentOS 7 EPEL repository
yum install epel-release
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
Bước 3: Cài đặt NGINX
yum install nginx -y
Khởi động lại nginx và cho phép nginx tự khởi động
systemctl restart nginx systemctl enable nginx
Trên CentOs7 mặc định sẽ sử dụng firewalld, nên cần thực hiện mở port với firewalld bằng lệnh sau
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
Bước 4: Cài đặt MariaDB
Sử dụng lệnh sau để cài đặt MariaDB
yum install -y mariadb mariadb-server
Khởi động lại MariaDB và cho phép MariaDB tự khởi động
systemctl restart mariadb systemctl enable mariadb
Cài đặt ban đầu và cấu hình mật khẩu cho user root MariaDB
mysql_secure_installation
Enter current password for root (enter for none): Enter Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] y Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y Thanks for using MariaDB!
Bước 5: Cài đặt và cấu hình PHP-FPM
Enable PHP 7.4 và cài đặt các module
yum-config-manager --enable remi-php74
yum install -y php php-fpm php-ldap php-zip php-embedded php-cli php-mysql php-common php-gd php-xml php-mbstring php-mcrypt php-pdo php-soap php-json php-simplexml php-process php-curl php-bcmath php-snmp php-pspell php-gmp php-intl php-imap perl-LWP-Protocol-https php-pear-Net-SMTP php-enchant php-pear php-devel php-zlib php-xmlrpc php-tidy php-opcache php-cli php-pecl-zip unzip gcc
Sau khi cài đặt xong ta có thể kiểm tra bằng lệnh
php --version
Chỉnh sửa user và group
vi /etc/php-fpm.d/www.conf user = nginx group = nginx
Restart dịch vụ php-fpm
systemctl stop php-fpm.service systemctl start php-fpm.service
Bước 6: Cầu hình NGINX
Mở file config của NGINX để chỉnh sửa cấu hình:
nano /etc/nginx/conf.d/default.conf
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; } }
Khởi động lại nginx
systemctl restart nginx
Chúc các bạn thành công…!