LNMP环境一键部署

LNMP环境一键部署脚本:

#!/bin/bash#用于安装LNMP环境[ ! -d /software/ ] && mkdir /software#功能选择菜单menu(){
        echo -e "\033[31;32m                              LNMP编译安装脚本                                 \033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo -e "\033[34m   此脚本能直接在rhel7.5、Centos 7.5上成功执行   \033[0m"
        echo -e "\033[33m   安装包版本:  nginx:1.14.0   mysql5.7.23   php7.2.6   \033[0m"
        echo "   相关依赖包:pcre-devel、gd-devel、openssl、openssl-devel、php-mysql、bzip2   "
        echo "               perl-devel、libxml2-devel bzip2-devel libcurl-devel   "
        echo -e "\033[47;34m------------安装需联网下载软件包,若下载地址失效,需自行更新下载地址------------\033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo -e "\033[33m   nginx安装目录:/usr/local/nginx,日志目录:/data/logs/nginx/   \033[0m"
        echo -e "\033[33m   mysql安装目录:/usr/local/mysql,数据存放目录:/data/mysql/   \033[0m"
        echo -e "\033[33m   php安装目录:/usr/local/php7   \033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo -e "\033[34m请选择:\033[0m"
        echo -e "\033[36m0、安装依赖包   1、安装nginx   2、源码编译安装mysql   3、安装php   \033[0m"
        echo -e "\033[36m4、整合nginx和php   5、启动nginx、mysql、php-fpm服务   \033[0m"
        echo -e "\033[36m6、一键安装并部署lnmp   7、退出脚本   \033[0m"
        echo -e "\033[31;32m================================================================================\033[0m"
        echo
        read -p "请输入数字:0-5[单独安装](单独安装需要先安装依赖包),6[一键安装],7[退出脚本]: " num}#(0)安装依赖包install_package(){
        cd /software

	    yum install -y vim-enhanced lrzsz net-tools gcc rsync epel-release wget unzip curl
        yum install -y bzip2-devel openssl-devel gnutls-devel gcc gcc-c++ cmake ncurses-devel bison-devel libaio-devel openldap  openldap-devel
        yum install -y autoconf bison libxml2-devel libcurl-devel libevent libevent-devel gd-devel expat-devel numactl        if [ ! -f libmcrypt-2.5.8.tar.gz ]
        then
            wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz        fi

        [ ! -d libmcrypt-2.5.8 ] && tar -zxf libmcrypt-2.5.8.tar.gz        cd libmcrypt-2.5.8
        ./configure && make && make install

        if [ $? -eq 0 ]
        then
               sleep 3               echo -e "\033[36m==========依赖包安装成功==========\033[0m"
          else
               echo -e "\033[31m**********安装依赖包失败,请检查**********\033[0m"
        fi}#(1)编译安装nginxinstall_nginx(){
        cd /software        #1、新建nginx系统用户
        id nginx &> /dev/null        [ $? -ne 0 ] && useradd -r -s /sbin/nologin nginx        #2、新建nginx日志存放目录
        [ ! -d /data/logs/nginx/ ] &&  mkdir -pv /data/logs/nginx/ && chown -R  nginx:nginx /data/logs/nginx/        #3、定义nginx安装的版本
        NGINX_VERS=nginx-1.14.0        #4、下载nginx
        if [ ! -f $NGINX_VERS.tar.gz ]
        then
           echo -e "\033[34m==========正在下载nginx源码包==========\033[0m"
           curl -O  http://nginx.org/download/$NGINX_VERS.tar.gz        fi

        #5、解压下载包
        [ ! -d $NGINX_VERS ] && tar xf $NGINX_VERS.tar.gz        #6、进入nginx解压后的目录
        cd $NGINX_VERS

        #7、编译安装
        ./configure --prefix=/usr/local/nginx \
        --user=nginx \
        --group=nginx \
        --http-log-path=/data/logs/nginx/access.log \
        --error-log-path=/data/logs/nginx/error.log \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_image_filter_module \
        --with-http_stub_status_module &&  make && make install

        if [ $? -eq 0 ]
        then
               sleep 3               echo -e "\033[36m==========nginx编译安装成功==========\033[0m"
        else
               echo -e "\033[31m**********nginx编译安装失败,请检查**********\033[0m"
               exit 1        fi
	
	    cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running \`nginx -t\` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPIDKillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
	    systemctl enable nginx && systemctl daemon-reload}#(2)源码编译安装mysqlinstall_mysql(){
        cd /software

        VERSION=5.7.23      #mysql版本

        if [ ! -f mysql-$VERSION.tar.gz ]
        then
            #下载mysql
            echo -e "\033[34m==========下载mysql源码包==========\033[0m"
            curl -o mysql-$VERSION.tar.gz https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-$VERSION-linux-glibc2.12-x86_64.tar.gz        fi

        # 1、创建mysql用户
        id mysql &> /dev/null        [ $? -ne 0 ] && useradd -r -s /sbin/nologin mysql        #2、解压mysql
        echo -e "\033[34m==========解压mysql...==========\033[0m"

        if [ -f mysql-$VERSION.tar.gz ]
        then
           tar xvf mysql-$VERSION.tar.gz           mv mysql-$VERSION-* /usr/local/mysql        fi
	
	    #3、创建数据库数据存放目录、安装目录
        [ ! -d  /data/mysql/ ] && mkdir -pv /data/mysql/	    [ ! -f /usr/local/mysql/log/error.log ] && mkdir -pv /usr/local/mysql/log/ && touch /usr/local/mysql/log/error.log        chown -R mysql:mysql  /data/mysql/        chown -R mysql:mysql  /usr/local/mysql/        #4、初始化
	
	    cd /usr/local/mysql	
        if [ $? -eq 0 ]
        then
            echo -e "\033[34m==========数据库开始初始化==========\033[0m"
            ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/  --datadir=/data/mysql/	        if [ $? -eq 0 ]
	        then
		        echo -e "\033[36m==========数据库初始化成功==========\033[0m"
	        else
		        echo -e "\033[31m**********编译安装错误!初始化失败**********\033[0m"
            	exit 1	        fi
        fi

        #5、配置数据库配置文件
        cat > /etc/my.cnf << EOF
[client]
port    = 3306
socket = /usr/local/mysql/tmp/mysql.sock

[mysql]
default-character-set=utf8

[mysqld]
default-storage-engine=INNODB
character_set_server=utf8
explicit_defaults_for_timestamp
basedir=/usr/local/mysql/
datadir=/data/mysql/
socket=/usr/local/mysql/tmp/mysql.sock
log_error = /usr/local/mysql/log/error.log

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

        [ ! -f /etc/init.d/mysql ] && \cp  /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql        
	    sed -i '46s/basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysql	    sed -i '47s/datadir=/datadir=\/data\/mysql/' /etc/init.d/mysql	
        chmod +x /etc/init.d/mysql	
        #6、配置环境变量
        n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l`
	
	    if [ $n -eq 0 ]
	    then
            echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile            source /etc/profile        else 
            source /etc/profile	    fi

        #7、创建日志目录、/var/run/mysqld/
        [ ! -d /var/log/mysql/ ] && mkdir /var/log/mysql/ && chown -R mysql:mysql /var/log/mysql/        [ ! -d /var/run/mysqld/ ] && mkdir /var/run/mysqld/ && chown -R mysql:mysql /var/run/mysqld/	    [ ! -d /usr/local/mysql/tmp/ ] && mkdir /usr/local/mysql/tmp/ && chown -R mysql:mysql /usr/local/mysql/tmp/	
	    #8、配置成mysqld服务并启动
	    cat > /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

#连接数限制
LimitNOFILE=65535
LimitNPROC=65535

#Restart配置可以在进程被kill掉之后,让systemctl产生新的进程,避免服务挂掉
#Restart=always
PrivateTmp=false
EOF

	    systemctl daemon-reload && systemctl enable mysqld && systemctl start mysqld && systemctl daemon-reload	
	    if [ $? -eq 0 ]
	    then
	        echo -e "\033[36m==========mysql安装成功并启动==========\033[0m"
	    fi	
	   
	    n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l`
	
	    if [ $n -eq 0 ]
	    then
            echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile            source /etc/profile        else
            source /etc/profile	    fi
	    
	    mysql -uroot -D mysql -e "UPDATE user SET authentication_string=PASSWORD("123456789") WHERE user='root';"
	    mysql -uroot -e "FLUSH PRIVILEGES;"
	    mysql -uroot -p123456789 -e "grant all privileges on *.* to root@'%'  identified by '123456789';"
	
	    if [ $? -eq 0 ]
        then
           echo -e "\033[36m==========数据库root密码修改为123456789成功==========\033[0m"
        else
           echo -e "\033[31m**********数据库root密码修改为123456789失败,为空**********\033[0m"
        fi	}#(3)编译安装phpinstall_php(){
        cd /software
        
        PHPVERS=7.2.6       #PHP版本
        DLOAD_PHP=http://mirrors.sohu.com/php/php-$PHPVERS.tar.gz  #PHP下载地址

        #0 、创建php-fpm用户,用户名:php-fpm
        id php-fpm &> /dev/null        [ $? -ne 0 ] && useradd -r -s /sbin/nologin php-fpm        #1、下载php
        [ ! -f  php-$PHPVERS.tar.gz ] && echo -e "\033[36m正在下载php源码包...\033[0m"  && curl -O $DLOAD_PHP

        #2、解压php
        [ ! -d php-$PHPVERS/ ] && tar xf php-$PHPVERS.tar.gz        echo -e "\033[36m编译安装php需要很长时间,请慢慢等待...\033[0m"
        sleep 3        cd php-$PHPVERS/        #3、编译安装
        ./configure  --prefix=/usr/local/php7 \
        --with-config-file-path=/usr/local/php7/etc \
        --with-mysqli=mysqlnd  \
        --with-pdo-mysql=mysqlnd \
	    --with-mysql=/usr/local/mysql \
        --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock \
        --with-iconv-dir \
        --with-freetype-dir \
        --with-jpeg-dir \
        --with-png-dir \
        --with-zlib \
        --with-bz2 \
        --with-libxml-dir \
        --with-curl \
        --with-gd \
        --with-openssl \
        --with-mhash  \
        --with-xmlrpc \
        --with-pdo-mysql \
        --with-libmbfl \
        --with-onig \
        --with-pear \
        --enable-xml \
        --enable-bcmath \
        --enable-shmop \
        --enable-sysvsem \
        --enable-inline-optimization \
        --enable-mbregex \
        --enable-fpm \
        --enable-mbstring \
        --enable-pcntl \
        --enable-sockets \
        --enable-zip \
        --enable-soap \
        --enable-opcache \
        --enable-pdo \
        --enable-mysqlnd-compression-support \
        --enable-maintainer-zts  \
        --enable-session \
        --with-fpm-user=php-fpm \
        --with-fpm-group=php-fpm  && make -j 2 && make -j 2 install

        if [ $? -eq 0 ]
        then
            echo -e "\033[36m==========php编译安装成功==========\033[0m"
        else
            echo -e "\033[31m**********php编译安装失败,请检查**********\033[0m"
            exit 1        fi

        #4、配置php-fpm服务文件
        [ ! -d /usr/local/php7/etc/ ] && mkdir /usr/local/php7/etc/
        \cp php.ini-production  /usr/local/php7/etc/php.ini        sed -i '/post_max_size/s/8/16/g;/max_execution_time/s/30/300/g;/max_input_time/s/60/300/g;s#\;date.timezone.*#date.timezone \= Asia/Shanghai#g' /usr/local/php7/etc/php.ini
        \cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm        chmod +x /etc/init.d/php-fpm        #5、将php-fpm添加到系统服务
        chkconfig --add php-fpm        chkconfig php-fpm on        #6、php-fpm配置文件
        cd /usr/local/php7/        cat > etc/php-fpm.conf << EOF
[global]
pid = /usr/local/php7/var/run/php-fpm.pid
error_log = /usr/local/php7/var/log/php-fpm.log
[www]
listen = 127.0.0.1:9000
listen.mode = 666
listen.owner = nobody
listen.group = nobody
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
EOF

        #7、编译ldap模块
        echo -e "\033[34m==========安装ldap模块==========\033[0m"
        cd /software/php-$PHPVERS/ext/ldap
        \cp -af /usr/lib64/libldap* /usr/lib/
        /usr/local/php7/bin/phpize        [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install
        sed -i '/\;extension=bz2/aextension=ldap.so' /usr/local/php7/etc/php.ini        #8、编译gettext模块
        echo -e "\033[34m==========安装gettext模块==========\033[0m"
        cd /software/php-$PHPVERS/ext/gettext
        \cp -af /usr/lib64/libldap* /usr/lib/
        /usr/local/php7/bin/phpize        [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install
        sed -i '/\;extension=bz2/aextension=gettext.so' /usr/local/php7/etc/php.ini	
	    echo -e "\033[36m==========PHP安装步骤完成==========\033[0m"}#(4)整合nginx和phpconfig_lnmp(){
        cd /usr/local/nginx        #1、编辑nginx配置文件
	    cat > conf/nginx.conf << EOF
user nobody nobody;	
worker_processes  1;
error_log /data/logs/nginx/error.log crit;
pid /run/nginx.pid;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  1024;
}

http {
    include       mime.types;
    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  /data/logs/nginx/access.log  main;

    server_tokens       off;
    sendfile        	on;
    send_timeout        3m;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    client_header_timeout 3m;
    client_body_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;    

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;

    default_type  application/octet-stream;
    include  /usr/local/nginx/conf.d/*.conf;
}
EOF
        #2、将server配置段从nginx.conf分离出来
        [ ! -d conf.d ] && mkdir conf.d	
	
        #3、创建conf.d/server.conf文件整合nginx与php
        cat > conf.d/server.conf << EOF
server {
	listen       80;
	server_name  localhost;
	location / {
		root   /usr/local/nginx/html;
		index  index.php index.html index.htm;
	}
	
	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        	root   html;
        }	

	location ~ \.php$ {
	root   /usr/local/nginx/html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
	include        fastcgi_params;
	}
}
EOF

        #4、修改conf/fastcgi_params
        > conf/fastcgi_params        cat > conf/fastcgi_params << EOF
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       \$query_string;
fastcgi_param  REQUEST_METHOD     \$request_method;
fastcgi_param  CONTENT_TYPE       \$content_type;
fastcgi_param  CONTENT_LENGTH     \$content_length;
fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        \$fastcgi_script_name;
fastcgi_param  REQUEST_URI        \$request_uri;
fastcgi_param  DOCUMENT_URI       \$document_uri;
fastcgi_param  DOCUMENT_ROOT      \$document_root;
fastcgi_param  SERVER_PROTOCOL    \$server_protocol;
fastcgi_param  REMOTE_ADDR        \$remote_addr;
fastcgi_param  REMOTE_PORT        \$remote_port;
fastcgi_param  SERVER_ADDR        \$server_addr;
fastcgi_param  SERVER_PORT        \$server_port;
fastcgi_param  SERVER_NAME        \$server_name;
EOF

        #5、关闭SELinux、防火墙
        setenforce 0 && sed -i 's/enforcing/disabled/g' /etc/selinux/config
        systemctl stop firewalld && systemctl disable firewalld        #6、创建测试页,备份nginx默认的页面

        cat > html/1.php << EOF

EOF

        if [ $? -eq 0 ]
        then
            sleep 3            echo -e "\033[36m==========整合nginx和php成功==========\033[0m"
        else
            echo -e "\033[31m**********整合nginx和php失败,请检查**********\033[0m"
            # exit 1
        fi}#(5)启动nginx、mysql、php-fpm服务start_service(){
	    systemctl daemon-reload && systemctl start nginx 
       
	    if [ $? -eq 0 ]
            then
                echo -e "\033[36m==========nginx服务启动成功==========\033[0m"
        else
            echo -e "\033[31m**********nginx服务启动失败**********\033[0m"
        fi

        systemctl start mysqld  &&  /etc/init.d/php-fpm start        if [ $? -eq 0 ]
        then
            sleep 3            echo -e "\033[36m==========mysql、php-fpm服务启动成功==========\033[0m"
            echo -e "\033[33m查看端口启用情况:\033[0m"
            ss -tnl            echo -e "\033[33m端口:80、3306、9000已启动!\033[0m"
            echo -e "\033[36m==========编译安装lnmp已完成==========\033[0m"
            echo -e "\033[36m==========数据库root密码为123456789==========\033[0m"
            echo -e "\033[36m--------打开浏览器输入你的ip/1.php,看看测试页--------\033[0m"
        else
             echo -e "\033[31m**********mysql、php-fpm服务启动失败**********\033[0m"
        fi
        
        source /etc/profile}#脚本运行入口run_install(){
        while true;do
        menu        case $num in
        "0")
                #0、安装依赖包
                    echo -e "\033[34m==========安装依赖包==========\033[0m"
                    install_package                    ;;
        "1")
                #1、编译安装nginx
                    echo -e "\033[34m==========编译安装nginx==========\033[0m"
                    install_nginx                    ;;
        "2")
                #2、编译安装mysql
                    echo -e "\033[34m==========编译安装mysql==========\033[0m"
               	    install_mysql                    ;;
        "3")    #3、编译安装php
                    echo -e "\033[34m==========编译安装php==========\033[0m"
                    install_php                    ;;
        "4")    #4、整合nginx和php
                    echo -e "\033[34m==========整合nginx和php==========\033[0m"
               	    config_lnmp                    ;;
        "5")    #5、启动nginx、mysql、php-fpm服务
                    echo -e "\033[34m==========启动nginx、mysql、php-fpm服务==========\033[0m"
                    start_service                    ;;
        "6")    #6、一键编译安装lnmp
                    echo -e "\033[34m==========一键编译安装并配置lnmp==========\033[0m"
                    install_package
                    install_nginx
                    install_mysql
                    install_php
                    config_lnmp
                    start_service                    exit 0                    ;;
        "7")    #7、退出脚本
                    exit 0                    ;;
          *)
                    ;;
        esac        done}#调用脚本运行入口run_install

手动单独安装可以参考这里:https://blog.csdn.net/miss1181248983/article/details/80890649


上一篇:CentOS搭建LNMP环境


下一篇:乌班图系统搭建lnmp环境