1. Giới thiệu về NginX
Nginx là một máy chủ sử dụng phổ biến giao thức HTTP, HTTPS, SMTP, POP3 và IMAP , cũng như dùng làm cân bằng tải (load balancer), HTTP cache và máy chủ web (web server). Nginx tập trung vào việc phục vụ số lượng kết nối đồng thời lớn (high concurrency), hiệu suất cao và sử dụng bộ nhớ thấp. Nginx được biết đến bởi sự ổn định cao, nhiều tính năng, cấu hình đơn giản và tiết kiệm tài nguyên.
2. Các bước triển khai
B1: Sửa Hosts file và Hostname của máy cài Nginx
#vi /etc/hosts
9.9.9.10 web.viettel.com.vn web
#vi /etc/hostname
web
B2. Tạo file "nginx.repo" (phải cài từ Internet vì trong đĩa không có Nginx)
# vi /etc/yum.repos.d/nginx.repo
Viết nội dung cho file "nginx.repo"
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
B3: Cài đặt Nginx, PHP và các gói bổ trợ
# yum install nginx -y *
# yum install php * -y
B4: Cài đặt các gói bổ sung cho PHP-MYSQL
# yum install php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc * -y
B5: Cài đặt gói PHP-FPM hỗ trợ cho NginX
# yum install php-fpm * -y
B6: Vô hiệu hóa Apache (httpd) và kích hoạt nginx, php-fpm
systemctl stop httpd
systemctl disable httpd
systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm
B7: Kiểm tra phiên bản của nginx và php-fpm
nginx -v
php-fpm -v
B8: Mở Port 80 cho Web Server
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
firewall-cmd --list-all
B9: Mở File default.conf và xóa sạch rồi copy nội dung như sau:
# vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name web.viettel.com.vn;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php5-fpm.sock; #(không copy đoạn chú thích này: kết nối giữa Nginx và PHP theo Socket sẽ tăng tính ổn định và nhanh hơn)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
B10: Mở File nginx.conf chỉnh sửa thêm một số nội dung nếu cần
#vi /etc/nginx/nginx.conf
user nginx; <= Gán quyền cho user nginx
worker_processes 1; <= Cần chỉnh lại worker_processes bằng với số lượng CPU core bạn được sử dụng
error_log /var/log/nginx/error.log warn; <= Cấu hình đường dẫn file error_log
pid /var/run/nginx.pid;
events {
worker_connections 1024; <= số kết nối tối đa tai 1 thời điểm; mặc định là 1024; trường hợp bạn muốn nâng cao hơn có thể tùy chỉnh nhưng không nên cao hơn quá 2048.
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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; <= Cấu hình log access
sendfile on;
server_tokens off; <= Cấu hình ẩn phiên bản nginx đang sử dụng
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include /etc/nginx/conf.d/*.conf; <= Đường dẫn gán file cấu hình virtual hosts cho từng site
}
B11: Thiết lập lại User và Group chạy PHP
vi /etc/php-fpm.d/www.conf
Gõ lệnh :set nu tìm đến dòng 12, 31, 32, 39,40 bỏ dấu ; và sửa lại thành
listen = /var/run/php5-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
Lưu lại thiết lập và khởi động lại PHP-FPM
systemctl restart php-fpm
systemctl restart nginx
B12: Cài đặt MYSQL Server
# yum install -y mariadb mariadb-server
B13: Start mysql server lên khởi động cùng mỗi khi khởi động lại Server
systemctl start mariadb.service
systemctl enable mariadb.service
B14: đổi lại mật khẩu của tài khoản Root quản trị Mysql server
#/usr/bin/mysqladmin -u root password 123456a@
B15: Cài đặt PHPMYADMIN cho NGINX
+Download phpmyadmin về máy
+Sử dụng WinsCP để transfer thư mục phpmyadmin vào máy chủ nginx theo đường dẫn /usr/share/nginx/html
- Giải nén gói cài đặt phpMyAdmin-3.5.1-all-languages.tar.gz
[root@centos7 ~]# cd /usr/share/nginx/html
[root@centos7 html]#tar -zxvf phpMyAdmin-3.5.1-all-languages.tar.gz
-Đổi tên thư mục sau khi giải nén
[root@centos7 html]# mv phpMyAdmin-3.5.1-all-languages admin
+Tìm file php.ini (/etc/php.ini).
[root@centos7 ~]# vi /etc/php.ini
Tìm dòng 1357: session.save_path = "/tmp" bỏ dấu ; và đổi thành session.save_path = "/var/lib/php/session/" (chú ý phải có dòng này)
B16:
Cấp quyền chủ sở hữu cho toàn bộ thư mục
[root@centos7 ~]# chown
-R nginx:nginx /var/lib/php/session/
B17:
Khởi động lại php-fpm, nginx
systemctl restart php-fpm
systemctl restart nginx
B18: truy cập vào http://9.9.9.9/admin/ để tạo Database tên là anln (IP của nginx).
+Đăng nhập vào với tài khoản đã tạo trong mysql (Username/Password: root/123456a@)
+Tạo Database với tên anln
+ Sử dụng WinsCP và upload Web site vào /home/nginx/bkap/public_html/
B19: Vào file Connec: /usr/share/nginx/html /Connect => sửa mật khẩu và tên cơ sở dữ liệu: $ketnoi = mysql_connect("localhost","root","123456a@") và mysql_select_db("anln", $ketnoi ) Chú ý trên Hosting phải sửa cả tên Root tương ứng với tài khoản.
B20: Sửa file functions: /usr/share/nginx/html/Website/Gio_hang/functions.php
require('/usr/share/nginx/html/Connect/Connect.php ');
B21: Kiểm tra
Chú ý: nêu muốn đổi thư mục thì phải sửa đường dẫn và phải phân quyền cho thư mục
mkdir -p /nginx/log
touch /nginx/log/error.log
mkdir -p /nginx/html
chown -R nginx:nginx /nginx (Trao toàn bộ thư mục /home/nginx cho user và group NGINX)
3. Video hướng dẫn