Công cụ thành viên

Công cụ trang web


web_server:cai-dat-lemp-cho-centos7

Cài đặt LEMP cho VPS để chạy NukeViet 4 với CentOS 7

Trước khi cài đặt, hãy tiến hành tắt SELinux đi bằng cách mở file /etc/sysconfig/selinux và tìm

SELINUX= rồi sửa thành như dưới đây:

SELINUX=disabled

Sửa xong, hãy gõ lệnh reboot để khởi động lại máy chủ và đăng nhập vào lại SSH.

Nạp package cần thiết

Bài viết dành cho hệ điều hành CentOS 7 và chưa cài bất cứ cái gì vào, sau đó cập nhật những gói update

yum update -y

Do cả NGINX và PHP-FPM đều không có sẵn trong gói Yum mặc định của CentOS nên chúng ta phải nạp một package từ bên ngoài vào.

Bạn chạy lần lượt 2 lệnh sau:

yum install epel-release -y
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Tạo repos cho NGINX

rm -f um.repos.d/nginx.repo
cat > "/etc/yum.repos.d/nginx.repo" <<END
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
END

Cài đặt NGINX và PHP-FPM

Cài NGINX và PHP cùng các module cần thiết:

yum --enablerepo=remi,remi-php56 install nginx php-fpm php-mysql php-common php-mbstring php-mcrypt php-gd php-xml php-zip php-memcached php-opcache -y

Kích hoạt NGINX, PHP và thêm 2 ứng dụng này vào danh sách ứng dụng tự khởi động nếu reboot server (gõ lần lượt các lệnh):

sudo systemctl start nginx
sudo systemctl enable nginx

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Kiểm tra phiên bản NGINX và PHP-FPM:

nginx -v
php-fpm -v

Mặc định CentOS đã chặn cổng 80 nên bạn cần chạy 2 lệnh dưới đây để mở cổng 80 của webserver ra:

Nếu dùng firewalld

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload

Nếu dùng iptables

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

Bây giờ bạn hãy truy cập vào địa chỉ là http://ip-vps rồi xem nó đã hiện trang Welcome của NGINX chưa nhé.

Thiết lập lại user và group chạy PHP

Mặc định PHP-FPM sẽ chỉ định user tên là apache và group tên apache để chạy nó, nhưng ở đây chúng ta không sử dụng Apache mà là NGINX nên bạn cần mở file /etc/php-fpm.d/www.conf lên và tìm:

user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

Thay thành

user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Bây giờ bạn hãy mở file default.conf trong thư mục /etc/nginx/conf.d/ Tìm đoạn

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

Sửa thành

        root   /usr/share/nginx/html;
        index  index.html index.php index.htm;

Tìm đoạn

    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

Sửa thành

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

Chạy đoạn code sau để sửa lỗi session

sed -i 's/php_value\[session.save_handler\]/;php_value\[session.save_handler\]/g' /etc/php-fpm.d/www.conf
sed -i 's/php_value\[session.save_path\]/;php_value\[session.save_path\]/g' /etc/php-fpm.d/www.conf
sed -i 's/php_value\[soap.wsdl_cache_dir\]/;php_value\[soap.wsdl_cache_dir\]/g' /etc/php-fpm.d/www.conf
sed -i 's/;session.save_path = \"\/tmp\"/session.save_path = \"\/tmp\"/g' /etc/php.ini 

Khởi động lại PHP-FPM

systemctl restart nginx
systemctl restart php-fpm

Cài đặt MariaDB

Để cài đặt MariaDB 10.1 trên CentOS 7, bạn cần thêm một repository sau vào hệ thống bằng cách chạy lệnh sau:

cat > "/etc/yum.repos.d/Mariadb.repo" <<END
# MariaDB 10.1 CentOS repository list - created 2015-12-06 14:53 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
END

Cài đặt hoặc update MariaDB

yum install -y MariaDB MariaDB-server

Khởi động MariaDB và tự động chạy khi boot

systemctl start mariadb
systemctl enable mariadb

Bắt đầu cài đặt

Chạy lệnh

/usr/bin/mysql_secure_installation

Ngay bước đầu tiên bạn sẽ bị hỏi root password, do mới cài đặt nên tất nhiên chưa có password, nhấn Enter để tiếp tục

Output tương tự như sau:

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!

Cài đặt NukeViet 4

Chạy lần lượt các lệnh sau để download code mới nhất

yum install wget unzip
cd /root/
wget https://github.com/nukeviet/nukeviet/releases/download/4.0.29/nukeviet4.0official-setup.zip
unzip nukeviet4.0official-setup.zip
mv ./nukeviet/* /usr/share/nginx/html/
chown -R nginx:nginx /usr/share/nginx/html/
rm -f /usr/share/nginx/html/index.html

Bây giờ bạn hãy truy cập vào địa chỉ là http://ip-vps/ rồi thực hiện cài đặt bình thường nhé, đến bước kiểm tra rewite không được cứ bỏ qua nó nhé.

Cấu hình rewite

Hãy mở file default.conf trong thư mục /etc/nginx/conf.d/ tìm đến đoạn sau (đã sửa lúc trước)

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

Thêm lên trên đoạn sau:

	if ($request_filename ~ /robots.txt$){
		rewrite ^(.*)$ /robots.php?action=$http_host break;
	}

        # Thiết lập rewrite chỉ có từ NukeViet 4.3.00
	rewrite ^/install/check\.rewrite$ /install/rewrite.php break;

	rewrite ^/(.*?)sitemap\.xml$ /index.php?nv=SitemapIndex break;
	rewrite "^/(.*?)sitemap\-([a-z]{2})\.xml$" /index.php?language=$2&nv=SitemapIndex break;
	rewrite "^/(.*?)sitemap\-([a-z]{2})\.([a-zA-Z0-9-]+)\.xml$" /index.php?language=$2&nv=$3&op=sitemap break;

	if (!-e $request_filename){
		rewrite (.*)(\/|\.html)$ /index.php;
		rewrite /(.*)tag/(.*)$ /index.php;
	}
	
	rewrite ^/seek\/q\=([^?]+)$ /index.php?nv=seek&q=$1 break;
	rewrite ^/search\/q\=([^?]+)$ /index.php?nv=news&op=search&q=$1 break;
	rewrite ^/([a-zA-Z0-9\-]+)\/search\/q\=([^?]+)$ /index.php?nv=$1&op=search&q=$2 break; 
	rewrite ^/([a-zA-Z0-9-\/]+)\/([a-zA-Z0-9-]+)$ /$1/$2/ break; 
	rewrite ^/([a-zA-Z0-9-]+)$ /$1/ break;		

	location ~ ^/admin/([a-z0-9]+)/(.*)$ {
		deny all;
	}

	location ~ ^/(config|includes|install|vendor)/(.*)$ {
		deny all;
	}

	location ~ ^/data/(cache|ip|ip6|logs)/(.*)$ {
		deny all;
	}

	location ~ ^/(files|uploads|themes)/(.*).(php|ini|tpl|php3|php4|php5|phtml|shtml|inc|pl|py|jsp|sh|cgi)$ {
		deny all;
	}	

Lưu lại

Sửa file data/config_global.php của nukeviet

tìm đến dòng

$sys_info['supports_rewrite']=false;

Sửa thành

$sys_info['supports_rewrite']='rewrite_mode_apache';

Lưu lại và Khởi động lại PHP-FPM

service php-fpm restart
service nginx restart

Đăng nhập quản trị site, vào menu Cấu hình → Cấu hình chung lưu lại.

Ra ngoài site để kiểm tra rewite.

web_server/cai-dat-lemp-cho-centos7.txt · Sửa đổi lần cuối: 2018/06/01 09:23 bởi hoaquynhtim99