LEMP là một nhóm các phần mềm mã nguồn mở được cài trên máy chủ Webserver, được viết tắt từ Linux, NGINX (Engine x), MySQL, và PHP. LEMP tương tự như LAMP chỉ khác web server trên nền tảng Nginx thay vì Apache. Đặc điểm Nginx là một web server mã nguồn mở hoạt động nhanh và ít tài nguyên hệ thống hơn Apache. Đối với các hệ thống nhẹ sự khác nhau giữa Apache và Nginx là không đáng kể, tuy nhiên ở các hệ thống nặng thì Nginx thể hiện sự vượt trội hơn rất nhiều so với Apache.
Mục lục
Dưới đây là các bước thực hiện cài đặt hoàn chỉnh LEMP.
Trước khi cài đặt các service ta cần update hệ thống cập nhật các bản vá mới nhất
sudo yum install epel-release -y
Sau đó bạn cài các thành phần cần thiết cho server
sudo yum install gcc libxml2-devel libXpm-devel gmp-devel libicu-devel \
t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel \
libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel \
libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel \
mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel \
libtool-ltdl-devel libc-client-devel postgresql-devel bison gcc make -y
Bước 1: Cài đặt PHP
Trong bài viết này kỹ thuật Gsviec hướng dẫn bạn cài đặt PHP version 7, với các phiên bản php khác người dùng thực hiện tương tự
Cài đặt thêm gói epel và webtatic repository
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Sau khi chạy lệnh bên trên kia xong bạn chỉ cần chạy lên bên dưới để cài đặt PHP và PHP-FPM
yum install php70w php70w-fpm php70w-opcache \
php70w-bcmath php70w-cli php70w-common \
php70w-devel php70w-gd php70w-intl php70w-mbstring \
php70w-mcrypt php70w-mysqlnd php70w-pdo php70w-pecl-imagick \
php70w-xml php70w-json --skip-broken -y
yum remove ImageMagick-last-6.9.5.10-1.el7.remi.x86_64 -y
yum install php70w-pecl-imagick -y
Có thể kiểm tra lại trạng thái hoạt động và phiên bản đang sử dụng bằng:
# php -v
PHP 7.0.15 (cli) (built: Jan 19 2017 21:35:05) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.15, Copyright (c) 1999-2017, by Zend Technologies
root at gsviec.com in ~
mặc định php-fpm không có chế độ tự khỏi động khi bạn reboot server do đó bạn cần phải bật nó lên, chỉ cần chạy lệnh sau:
# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
Đối với các phiên bản PHP thấp hơn muốn cấp lên PHP 7 thì cần thận trọng cân nhắc để đảm bảo việc update không ảnh hưởng tới hoạt động ổn định của hệ thống sau khi nâng cấp. Trong trường hợp bạn vẫn muốn nâng cấp, bạn có thể thực hiện:
yum install yum-plugin-replace
yum replace php-common --replace-with=php70w-common
Bước 2: Cài đặt MySQL
Cài gói thêm gói repo
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
Tiến hành cài đặt và khởi động mysql
yum update -y
sudo yum install -y mysql-server
sudo systemctl start mysqld
tương tự như trường hợp php bạn bật chế độ tự khỏi động cho mysql qua dòng lệnh sau:
systemctl enable mysqld
Bước 3: Cài đặt NGINX
Chỉ cần một dòng lệnh duy nhất:
sudo yum install nginx -y
Đặt chế độ NGINX tự khởi động khi khởi động lại máy chủ và khởi động nó
systemctl start nginx && systemctl enable nginx
bạn dùng lệnh ifconfig
để biết IP là gì, trong trường hợp của tôi là 45.55.77.186, sau đó bạn truy cập qua URL để thấy kết quả hoặc qua dòng lệnh
curl 45.55.77.186
Kết quả sẽ có dạng tương tự như thế này
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Page for the Nginx HTTP Server on Fedora</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
/*<![CDATA[*/
body {
background-color: #fff;
color: #000;
font-size: 0.9em;
chúc mừng bạn đã cài đặt xong LEMP, nhưng trong các dự án thực tế bạn thường dùng VHOST để cấu hình cho website của bạn, do đó bạn cần cấu hình vhost cho nó
Bước 4: Cấu hình tạo virtual host trên NGINX
Trước tiên bạn mở file /etc/nginx/nginx.conf sau đó thêm vào đoạn code include /etc/nginx/vhost/*.conf;
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include /etc/nginx/vhost/*.conf;
include /etc/nginx/vhost/*.conf;
có nghĩa là nó sẽ gộp các file cấu hình trong vhost vào nginx, kế đến bạn tạo thư mục vhost, trong thư mục vhost là cấu hình cho website tương ứng của bạn
Ví dụ dưới đây demo cho cấu hình wordpress:
server {
listen 80;
server_name gsviec.dev;
index index.php index.html index.htm;
root /usr/share/nginx/html/gsviec.dev;
location / {
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
#neu muon dùng socket unit unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Lưu lại cấu hình và khởi động lại dịch vụ NGINX
systemctl restart nginx
Kết luận
Trên đây tôi đã hướng dẫn các bạn cách cài đặt LEMP trên Centos nếu bạn muốn cài đặt cho Ubuntu thao tác thứ tự cũng như thế, nhưng các dòng lệnh có thể khác đi, bài tới tôi sẽ hướng dẫn bạn
“`
Comments are closed.