Nhật ký Ngày... Tháng.. Năm...
blank
Trang chủ » Hệ thống » Cài đặt PHP Web Server hoàn hảo trên Centos 7

Cài đặt PHP Web Server hoàn hảo trên Centos 7

Trong bài hướng dẫn này, Nhật Ký sẽ hướng dẫn bạn  Step-by-step cài đặt 1 php web server trê CentOS 7 siêu chuẩn.

Đầu tiên các bạn cần chuẩn bị một VPS mới tinh bằng cách Reinstall hoặc Rebuild, sử dụng CentOS 6 hoặc 7, bản 32bit hoặc 64bit đều được. Nên sử dụng bản CentOS 7 64bit.

Bước 1: Tối ưu và update hệ thống

yum update && yum upgrade
yum install nano -y

Bước 2: Dùng hocvps script để cài 1 php website chuẩn.

curl -sO https://hocvps.com/install && bash install

Chạy lệnh trên và làm theo hướng dẫn của lệnh. Sau khi kết thúc, chúng ta sẽ dựng được 1 server gồm có nginx , php 7.1 mariadb cùng các tối ưu đi kèm. Lúc này cổng SSH mặc định là 22 đã được đổi sang 2222. Gần như xong bước này bạn đã có 1 máy chủ hoàn hảo rồi, các cấu hình của hocvps đều rất tốt cho hệ thống.

Bước 3: Nâng cấp PHP

yum install yum-utils
# Kích hoạt PHP 7.4
yum-config-manager --enable remi-php74
# Gỡ bỏ PHP7.1 hiện tại
yum-config-manager --disable remi-php71
# Kiểm tra lại toàn bộ danh sách
yum repolist all
# Cập nhật hệ thống lên php74
yum update 

Sau bước này, chạy lệnh php -v, nếu phiên bản của bạn đã lên 7.4 thì bạn đã làm đúng, nếu không hãy kiểm tra lại.

Bước 4: Cài đặt Nginx – Hỗ trợ Pagespeed

Nginx bản thân nó đã là một web server có hiệu suất hoạt động rất tốt. Tuy nhiên vẫn có nhiều cách để tối ưu thêm và một trong số đó là sử dụng module được phát triển bởi Google có tên PageSpeed. Nếu bạn đã từng test qua tốc độ website trên https://developers.google.com/speed/pagespeed/insights/ của google thì bạn có thể đã gặp các khuyến nghị của google về module PageSpeed cho nginx để nâng cao hiệu suất máy chủ.

Các lệnh cài đặt bạn thực hiện lần lượt:

# Cài đặt các gói cần thiết
yum -y install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel
# Cài đặt nginx mới nhất
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz && tar -xzvf nginx-1.16.1.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz && tar -xzvf openssl-1.1.1d.tar.gz
# Mã nguồn PageSpeed
cd /usr/local/src
NPS_VERSION=1.13.35.2-stable
wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VERSION}.zip
unzip v${NPS_VERSION}.zip
nps_dir=$(find . -name "*pagespeed-ngx-${NPS_VERSION}" -type d)
cd "$nps_dir"
NPS_RELEASE_NUMBER=${NPS_VERSION/beta/}
NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz
[ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
wget ${psol_url}
tar -xzvf $(basename ${psol_url}) 

Các phiên bản nên chọn mới nhất, cập nhật tại đây hoặc trên trang chủ của nginx, openssl và google pagespeed

Lấy lại thông tin cấu hình hiện tại của nginx

# Chạy lệnh
nginx -V
# Kết quả trả về sẽ dạng:
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

Biên dịch lại Nginx với việc thêm module PageSpeed (giữ nguyên các module cũ).

# Trỏ về nginx folder
cd /usr/local/src/nginx-1.16.1
# Cấu hình nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.1.1d --add-module=/usr/local/src/incubator-pagespeed-ngx-1.13.35.2-stable
# Biên dịch
make
make install
# Hệ thống chạy xong mà báo: make[1]: Leaving directory `/usr/local/src/nginx-1.16.1' thì thành công!

Sau đó chạy lệnh `service nginx restart` để khởi động lại nginx.

Cấu hình module ngx_pagespeed chuẩn

mkdir /var/ngx_pagespeed_cache
chown nginx:nginx /var/ngx_pagespeed_cache

Tạo file /etc/nginx/pagespeed.conf. Đây là file cấu hình mình tổng hợp và sẽ sử dụng cho phần tiếp theo.

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { add_header "" "";
}
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
pagespeed RewriteLevel PassThrough;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters canonicalize_javascript_libraries;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters combine_heads;
pagespeed EnableFilters elide_attributes;
pagespeed EnableFilters extend_cache;
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
pagespeed EnableFilters inline_google_font_css;
pagespeed EnableFilters lazyload_images;
pagespeed LazyloadImagesAfterOnload on;
pagespeed EnableFilters rewrite_css;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters trim_urls;
pagespeed EnableFilters remove_comments;
pagespeed EnableFilters remove_quotes;
pagespeed EnableFilters convert_png_to_jpeg;
pagespeed EnableFilters convert_jpeg_to_webp;
pagespeed EnableFilters recompress_images;
pagespeed EnableFilters recompress_webp;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters sprite_images;
pagespeed EnableFilters make_show_ads_async;
pagespeed EnableFilters make_google_analytics_async;
pagespeed EnableFilters insert_dns_prefetch;
pagespeed EnableFilters prioritize_critical_css;
pagespeed EnableFilters outline_javascript;
pagespeed CriticalImagesBeaconEnabled false;

 

Bước 5: Cài đặt chứng chỉ Let’s Encrypt SSL A+

Trước mình hay dùng thằng https://certbot.eff.org/ để cấu hình tự động, nhưng sau 1 thời gian sử dụng thấy có 1 số điểm bất tiện nên mình đã quay lại cách cũ trên hocvps. Thực hiện các lệnh sau.

mkdir /etc/nginx/ssl/
openssl dhparam 2048 -out /etc/nginx/ssl/dhparam.pem
# Install Git
yum -y install git
# Clone Let's Encrypt repository
git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
# Stop Nginx
service nginx stop
# Issue SSL Let's Encrypt
/opt/letsencrypt/certbot-auto certonly --standalone 

Nhập email ( dùng để thông báo chứng chỉ hết hạn, vì ssl chỉ có thời hạn 90 ngày) và tên miền cần tạo để đăng kí ssl. Nếu thành công, tại /etc/letsencrypt/live/ sẽ có tên miền bạn vừa khai báo.

Bước 6: Cài đặt memcache, redis

# Cài đặt repo
yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# Cài đặt memcache
yum install memcached
# Cấu hình memcache
nano /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="10240"
CACHESIZE="128"
OPTIONS="-l 127.0.0.1 -U 0"
# Đăng kí memcache
chkconfig memcached on
service memcached start
# Cài đặt memcache cho php
yum --enablerepo=remi,remi-php74 install php-pecl-memcached php-pecl-memcache
service php-fpm restart
service nginx restart
# Mở cổng trên vps nếu cần
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
service iptables restart
# Cài đặt redis
yum install redis
chkconfig redis on
service redis start
yum --enablerepo=remi,remi-php74 install redis php-pecl-redis
# Restart PHP
service php-fpm restart

Bước 7: Tùy biến nhỏ cho HocVPS Script

Sửa lại file /etc/hocvps/menu/them-domain:

#!/bin/bash
. /etc/hocvps/scripts.conf
echo -n "Nhap vao domain ban muon them roi an [ENTER]: "
read domain
if [ "$domain" = "" ]; then echo "Ban nhap sai, vui long nhap chinh xac" exit
fi
if [ -f /etc/nginx/conf.d/$domain.conf ]; then echo "Phat hien thay $domain da ton tai tren he thong" echo "Chao tam biet...!" exit
fi
mkdir -p /home/$domain/public_html
mkdir -p /home/$domain/logs
chmod 777 /home/$domain/logs
chown nginx:nginx /home/$domain
chown -R nginx:nginx /home/*/public_html
chown -R nginx:nginx /home/*/private_html
chown -R nginx:nginx /var/lib/php
cat > "/etc/nginx/conf.d/$domain.conf" <<END
server { listen 80; server_name $domain; rewrite ^(.*) https://$domain\$1 permanent;
}
server { listen 443 ssl http2; ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS"; ssl_session_cache shared:SSL:50m; ssl_session_timeout 1d; ssl_dhparam /etc/nginx/ssl/dhparam.pem; add_header Strict-Transport-Security "max-age=31536000" always; # access_log off; access_log /home/$domain/logs/access.log; # error_log off; error_log /home/$domain/logs/error.log; root /home/$domain/public_html; index index.php index.html index.htm; server_name $domain; include /home/$domain/public_html/*.conf; include /etc/nginx/pagespeed.conf; location / { try_files $uri $uri/ /index.php?$args; } # For WordPress Site location /wp-admin/ { auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; } location ~* /wp-content/.*.php$ { deny all; access_log off; log_not_found off; } location ~* /wp-includes/.*.php$ { deny all; access_log off; log_not_found off; } location ~* /(?:uploads|files)/.*.php$ { deny all; access_log off; log_not_found off; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$;	include /etc/nginx/fastcgi_params;	fastcgi_pass 127.0.0.1:9000;	fastcgi_index index.php; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 32k; fastcgi_buffers 8 16k; fastcgi_busy_buffers_size 32k; fastcgi_temp_file_write_size 32k; fastcgi_intercept_errors on;	fastcgi_param SCRIPT_FILENAME /home/$domain/public_html$fastcgi_script_name; } location ~ /\.(?!well-known).* { deny all; access_log off; log_not_found off; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~* \.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|ttf|woff|woff2)$ { gzip_static off; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; access_log off; expires 30d; break; } location ~* \.(txt|js|css)$ { add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; access_log off; expires 30d; break; }
}
END
service nginx stop
/opt/letsencrypt/certbot-auto certonly --standalone -d $domain
service nginx start
echo "$domain da duoc them thanh cong vao server"
echo "Upload code len /home/$domain/public_html/" 

Bước 8: Thành quả

Bây giờ cần thêm domain, bạn chạy lệnh hocvps -> 2 -> Nhập tên miền. Hệ thống sẽ tự động cấu hình chuẩn cho domain, cài đặt ssl cùng các bảo mật cho WordPress.

Đây là ảnh demo trên vps của mình.

blank

blank

Chúc các bạn cấu hình server thành công!

 

blank

Cương Phạm

Thêm bình luận