[工具] Shell 半自动化部署 LNMP 平台 + SSL (CentOS Linux 7 版)

介绍

基本信息

作者:朱明宇
脚本名称:半自动化部署 LNMP 平台 + SSL
作用:半自动化安装 LNMP,即 Nginx、Mariadb、PHP、php-fpm,实现 HTTPS

使用方法

为了安全起见,设置系统用户和数据库用户和数据库权限的工作请按照脚本最后的步骤进行手动设置

注意

1. 执行本脚本之前请确保 Nginx、PHP、网站程序文件、SQL、SSL 的公钥、SSL 的私钥、shell、本脚本 8 个文件,如果是新建网站的话,不用准备 SQL 文件
2. Nginx 文件:请将 Nginx 源码安装包放在 /root/
3. PHP 文件:请将 PHP 源码安装包放在 /root/
4. 网站程序文件:请将网站程序文件的名字中包含“eternalcenter-backup-”字段,并且是 tar 包,并放在 /root/
5. SQL 文件:如果是恢复网站的话,请将备份的数据文件 *.sql 放在 /root/,注意这是一个 MySQL 备份文件
6. SSL 的公钥文件和 SSL 私钥文件:请将网站安全证书的公钥和私钥分别命名为:eternalcenter.com.crt 和 eternalcenter.com.key,并放在 /root/
7. shell 文件:请将监控脚本命名为 port_monitoring.sh 并放在 /root/
8. 除此之外 /root/ 目录下请不要放任何文件
9. 服务器的系统需要是 CentOS 7 版本
10. 服务器系统要配置好可用的软件源(最好是软件数量最多的官方版本)
11. 服务器要能够连接外网

补充

1. Nginx 下载网址:https://nginx.org/
2. PHP 下载网址:https://www.php.net/downloads.php
3. WordPress 是一款开源的 php 网站程序,建议使用,下载网址:https://cn.wordpress.org/download/
4. 网站安全证书的公钥和私钥可以在这里申请:https://freessl.cn/

其他

  运行此脚本所需要的文件如下,请尽量使用图中的命名规则给他们命名,并将他们全部放在 /root/ 目录下,请保证 /root/ 下有且仅有这些文件,并以 root 的身份运行,否则脚本会执行失败。其中:

  eternalcenter-backup-2019-08-12-13.sql 指的是 mariadb 的数据库备份文件,如果是新建网站的话可以不要。

  eternalcenter-backup-2019-08-13.tar 指的是网页程序的压缩包,必须是 tar 格式,如果是新建的网站的话,这个也可以换成,新网站的网页程序。

  eternalcenter.com.crt 指的是网站的公钥,需要单独申请,方法可以在搜索引擎上搜索。

  eternalcenter.com.key 指的是网站的私钥,需要单独申请,方法可以在搜索引擎上搜索。

  install.sh 就是指的本脚本。

  nginx-1.16.0.tar.gz 指的是 nginx 的源码安装包,这里的 -1.16.0.tar.gz 可以任意命名,只用保证这个文件名字里包含 nginx 就行了,这个文件可以在 nginx 官网上下载。

  php-7.2.2-.tar.xz 指的是 php 的源码安装包,同样 这里的 -7.2.2-tar.xz 可以任意命名,只用保证这个文件名字包含 php 就行了,这个文件可以在 php 官网上下载。

  port_monitoring.sh 只是一个监控端口的脚本,可以不要,使用者可以根据自我需求写一个,站主是通过这个脚本定期检查并自动启动需要启动但是没有启动的端口,关闭不需要但是启动了的端口。

脚本

#!/bin/bash

# variable initialization
cd
nginxp=`ls | grep "nginx"`
phpp=`ls | grep "php"`
webp=`ls | egrep "eternalcenter-backup-.*tar$"`
nginxr=${nginxp%%.tar*}
phpr=${phpp%%.tar*}


# upgrade system
yum makecache
yum -y update

yum clean all
yum repolist


# remove useless software
yum -y remove php*

yum -y remove mariadb*

systemctl stop postfix
yum -y remove postfix

systemctl stop chronyd
yum -y remove chrony

yum -y update


# install dependency package
yum -y install gcc pcre-devel openssl-devel libxml2-devel curl-devel libpng libpng-devel


# install nginx
cd
tar -xvf $nginxp -C /root/
cd $nginxr

useradd -s /sbin/nologin nginx
./configure --user=nginx --group=nginx --with-http_ssl_module
make && make install
echo 'worker_processes  1;

events {
    worker_connections  65536;
}

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        limit_req zone=one burst=5;
        server_name www.eternalcenter.com eternalcenter.com;

        rewrite ^/(.*)$ https://eternalcenter.com/$1 permanent;
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        }

    server {
        listen       443 ssl;
        server_name www.eternalcenter.com eternalcenter.com;

        if ($request_method !~ ^(GET|POST)$){
        return 444;
        }

        ssl_certificate      /usr/local/nginx/ssl/eternalcenter.com.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/eternalcenter.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi.conf;
        } 

        location / {
        root html;
        index index.php index.html index.htm;
        }

        location ~ ^/\.user\.ini {
        deny all;
        }
    
        location ~*\.(jpd|jpeg|gif|png|css|js|ico|xml)$ {
        expires 30d;
        }

        error_page  404              /404.html;
        }

        gzip on;
	gzip_min_length 1000;
	gzip_comp_level 4;
	gzip_types text/plain test/css application/json application/x-javascript text/xml application/xml
	application/xml+rss text/javascripts;

	client_header_buffer_size 1k;
	large_client_header_buffers 4 4k;

	open_file_cache max=2000 inactive=20s;
	open_file_cache_valid  60s;
	open_file_cache_min_uses 5;
	open_file_cache_errors off;

}' > /usr/local/nginx/conf/nginx.conf
cd
mkdir /usr/local/nginx/ssl
mv eternalcenter.com.crt /usr/local/nginx/ssl
mv eternalcenter.com.key /usr/local/nginx/ssl

# install php
cd
tar -xvf $phpp -C /root/
cd $phpr
useradd -s /sbin/nologin php-fpm 
./configure --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --prefix=/usr/local/php --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd --enable-mbstring --with-curl --with-gd --with-zlib
make && make install
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
chown -R php-fpm:php-fpm /usr/local/nginx/html/

# install mariadb
yum -y install mariadb mariadb-server mariadb-devel
sed -i "/^datadir/a log_bin=ec" /etc/my.cnf
sed -i "/^datadir/a binlog_format="mixed"" /etc/my.cnf
sed -i "/^datadir/a server_id=51" /etc/my.cnf
systemctl enable mariadb

# deploy web software
cd
rm -rf /usr/local/nginx/html/*
mkdir websoftcache
tar -xvf $webp -C websoftcache
cd websoftcache
websoftcachen=`ls | wc -l`

if [ $websoftcachen -ne 1 ];then
        cd
        tar -xvf $webp -C /usr/local/nginx/html/
else
        webdir=`ls`
        cd $webdir
        mv * /usr/local/nginx/html/
fi

chown -R php-fpm:php-fpm /usr/local/nginx/html

# recover database
systemctl start mariadb
cd
mysql -uroot < *.sql &> /dev/null
systemctl stop mariadb

# set the concurrency number accepted by the system to 65535
echo '* soft nofile 65535
* hard nofile 65535' >> /etc/security/limits.conf

# open firewall port
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
systemctl restart firewalld

# move the monitoring scripts that need to be used regularly to the specified location(/root/shell/port_monitoring.sh you need to write it yourself)
mkdir /shell
mv port_monitoring.sh /shell
chmod u+x /shell/*

# set up regular automatic running monitoring script(/root/shell/port_monitoring.sh you need to write it yourself)
echo '*/1 * * * * root /shell/port_monitoring.sh' >> /etc/crontab

# add another user(for data backup)
useradd zhumingyu
mkdir /cache
chown -R zhumingyu:zhumingyu /cache

# delete redundant files
cd
rm -rf *

# restart the server
reboot