SHELL脚本一键搭建LNMP环境(2021最新版,用不了私信我)

一键搭建LNMP环境

环境准备

需要准备的环境:
1 、未安装过http和mysql的干净Centos7环境
2、可以连外网,需要下载在线源,需要的依赖包本地源无法满足
3、 将附带的软件包上传到root家目录

链接:https://pan.baidu.com/s/1T2-IVRl9WA8EFCALYohm-g
提取码:1234

LNMP一键安装包下载

lnmp一键搭建脚本

#!/bin/bash

#set -x

mkdir -p /etc/yum.repos.d/repo.bak && mv  /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak  2>1&>/dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo  2>1&>/dev/null

yum clean all  2>1&>/dev/null
yum list  2>1&>/dev/null


function exit_script() {
    
                                                          
    exit 1                                                                    
}     

echo ""
echo ""
echo ""
echo "--------------------------------------------------------"
echo "欢迎使用LNMP一键搭建脚本!"
echo "--------------------------------------------------------"
echo "请将以下软件包准备到脚本当前目录,否则执行脚本可能报错 "
echo "--------------------------------------------------------"
echo "boost_1_59_0.tar.gz|"
echo "--------------------"
echo "mysql-5.7.19.tar.gz|"
echo "--------------------"
echo "nginx-1.14.1.tar.gz|"
echo "------=-------------"
echo "pcre-8.41.tar.gz   |"
echo "--------------------"
echo "php-7.1.24.tar.gz  |"
echo "-------------------"
echo "epel-7.repo        |"
echo "-------------------"
echo "您当前目录为$PWD |"
echo "-------------------"
ls -l *.gz epel-7.repo 2>1&>/dev/null




if [ $? -ne 0 ] ; then

echo "软件包准备不足,请准备齐全再试!"

echo "-------------------------------"

exit_script

fi


FILENUM=`ls -l *.gz epel-7.repo|wc -l`

if [ "$FILENUM" -ne "6" ] ; then

echo""
echo""
echo "软件包准备不足,请准备齐全再试!"
echo""
echo""

exit_script

fi

echo ""
echo ""
echo "-------------------------------------------------------------------------------"
echo "软件包已符合条件,一键搭建LNMP架构整个过程将在20分钟左右执行完成,请耐心等待…………"

echo "-------------------------------------------------------------------------------"
echo ""
echo ""
echo ""
echo ""

sleep 5



mv epel-7.repo /etc/yum.repos.d/

#部署 LNMP 架构需要安装依赖包
yum -y install \
make cmake gcc gcc-c++ flex bison file libtool libtool-libs \
autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype \
freetype-devel libxml2 libxml2-devel zlib zlib-devel \
glib2 glib2-devel bzip2 bzip2-devel libevent ncurses \
ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5-devel libidn libidn-devel openssl openssl-devel \
gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof \
automake pcre*

#创建 Nginx 运行用户

useradd -M -s /sbin/nologin nginx

tar xf /root/pcre-8.41.tar.gz -C /usr/local/src/

#编译安装nginx

tar zxf /root/nginx-1.14.1.tar.gz -C /usr/local/src/

cd /usr/local/src/nginx-1.14.1 && \
./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module --with-http_stub_status_module --with-http_addition_module \
--with-http_sub_module --with-http_flv_module --with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx

#--with-http_dav_module #启用支持(增加 PUT,DELETE,MKCOL:创建集合,COPY和 MOVE 方法) 默认关闭,需要编译开启
#--with-http_stub_status_module #启用支持(获取 Nginx 上次启动以来的工作状态)
#--with-http_addition_module #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
#--with-http_sub_module #启用支持(允许一些其他文本替换 Nginx 相应中的一些文本)
#--with-http_flv_module #启用支持(提供支持 flv 视频文件支持)
#--with-http_mp4_module #启用支持(提供支持 mp4 视频文件支持,提供伪流媒体服务端支持)
#--with-pcre=/usr/local/src/pcre-8.37 #需要注意,这里指的是源码,用#./configure --help|grep pcre 查看帮助


make -j 2 && make install

#备份nginx配置文件

cp /usr/local/nginx/conf/nginx.conf{
    
    ,.bak}

cat > /usr/local/nginx/conf/nginx.conf <<eof

user  nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    
    
    worker_connections  1024;
}


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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
    
    
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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   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|php5)?$ {
    
    
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html\$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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    
    
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    
    
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

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

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

}

eof

#启动nginx服务

/usr/local/nginx/sbin/nginx

#编辑配置环境变量,让系统读到 Nginx 路径
#echo "export PATH=/usr/local/nginx/sbin:$PATH" >  /etc/profile.d/nginx.sh

#读取变量
#. /etc/profile.d/nginx.sh

#创建软连接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

#生成服务启动脚本

echo  '#!/bin/bash                                            '  >   /etc/init.d/nginx
echo  '# chkconfig: - 99 2                                    '  >>   /etc/init.d/nginx
echo  '# description: Nginx Service Control Script            '  >>   /etc/init.d/nginx
echo  'PROG="/usr/local/nginx/sbin/nginx"                     '  >>   /etc/init.d/nginx
echo  'PIDF="/usr/local/nginx/logs/nginx.pid"                 '  >>   /etc/init.d/nginx
echo  'case "$1" in                                           '  >>   /etc/init.d/nginx
echo  'start)                                                 '  >>   /etc/init.d/nginx
echo  '$PROG                                                  '  >>   /etc/init.d/nginx
echo  ';;                                                     '  >>   /etc/init.d/nginx
echo  'stop)                                                  '  >>   /etc/init.d/nginx
echo  'kill -3 $(cat $PIDF)                                   '  >>   /etc/init.d/nginx
echo  ';;                                                     '  >>   /etc/init.d/nginx
echo  'restart)                                               '  >>   /etc/init.d/nginx
echo  '$0 stop &> /dev/null                                   '  >>   /etc/init.d/nginx
echo  'if [ $? -ne 0 ] ; then continue ; fi                   '  >>   /etc/init.d/nginx
echo  '$0 start                                               '  >>   /etc/init.d/nginx
echo  ';;                                                     '  >>   /etc/init.d/nginx
echo  'reload)                                                '  >>   /etc/init.d/nginx
echo  'kill -1 $(cat $PIDF)                                   '  >>   /etc/init.d/nginx
echo  ';;                                                     '  >>   /etc/init.d/nginx
echo  '*)                                                     '  >>   /etc/init.d/nginx
echo  'echo "Userage: $0 { start | stop | restart | reload }" '  >>   /etc/init.d/nginx
echo  'exit 1                                                 '  >>   /etc/init.d/nginx
echo  'esac                                                   '  >>   /etc/init.d/nginx
echo  'exit 0                                                 '  >>   /etc/init.d/nginx

#配置服务开机自动启动
chmod +x /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

#检查nginx是否有语法错误
nginx -t 

#重载nginx配置文件
nginx -s reload

#设置Mysql
yum -y remove boost-*

#卸载自带的数据库
yum -y remove mysql mariadb-*


#添加用户和组
groupadd mysql
useradd -M -s /sbin/nologin -r -g mysql mysql

tar xf /root/boost_1_59_0.tar.gz -C /usr/local/src/
tar xf /root/mysql-5.7.19.tar.gz -C /usr/local/src/
cd /usr/local/src/mysql-5.7.19/

#规划安装目录
mkdir -p /var/lib/mysql/data
chown -R mysql:mysql /var/lib/mysql

cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql \
-DMYSQL_DATADIR=/var/lib/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/boost_1_59_0

#DCMAKE_INSTALL_PREFIX:指定 MySQL 程序的安装目录,默认/usr/local/mysql
#DEFAULT_CHARSET:指定服务器默认字符集,默认 latin1
#DEFAULT_COLLATION:指定服务器默认的校对规则,默认 latin1_general_ci
#ENABLED_LOCAL_INFILE:指定是否允许本地执行 LOAD DATA INFILE,默认 OFF
#WITH_COMMENT:指定编译备注信息
#WITH_xxx_STORAGE_ENGINE:指定静态编译到 mysql 的存储引擎,MyISAM,MERGE,
#MEMBER 以及 CSV 四种引擎默认即被编译至服务器,不需要特别指定。
#WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
#SYSCONFDIR:初始化参数文件目录
#MYSQL_DATADIR:数据文件目录
#MYSQL_TCP_PORT:服务端口号,默认 3306
#MYSQL_UNIX_ADDR:socket 文件路径,默认/tmp/mysql.sock


echo "安装时间较长,预计需要20分钟,已开启2倍内核加速,开多容易卡死" &&  make -j 2 && make install

#编辑配置文件
cat > /etc/my.cnf <<eof
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
port=3306
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
[mysql]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
eof

#添加 path 路径,让系统能读到 mysql 的命令
echo "export PATH=/var/lib/mysql/bin:$PATH" > /etc/profile.d/mysql.sh

#使修改生效
. /etc/profile.d/mysql.sh


#生成服务启动脚本
cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

#初始化数据库

cd /var/lib/mysql/data/
rm -rf /var/lib/mysql/data/*
/var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql  --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data

#启动数据库

/etc/init.d/mysqld start > mysql.pd  2>/dev/null


PD=`cat mysql.pd |grep -i succeed |wc -l`

if [ $PD -eq 1 ] ;then
echo "succeed!" 
else
cd /var/lib/mysql/data/
rm -rf /var/lib/mysql/data/*
/var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql  --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
fi

/etc/init.d/mysqld start  2>/dev/null


#安装并配置数据库

yum -y install  php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel  \
curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel

tar zxf /root/php-7.1.24.tar.gz -C /usr/local/src/ && cd /usr/local/src/php-7.1.24


./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/ --enable-fpm --with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir \
--with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath \
--enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl \
--enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd  \
--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets \
--with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo \
--enable-maintainer-zts 

#--with-config-file-path #设置 php.ini 的搜索路径。默认为 PREFIX/lib
#--with-mysql #mysql 安装目录,对 mysql 的支持 7.0 版本没有此参数
#--with-mysqli #mysqli 扩展技术不仅可以调用 MySQL 的存储过程、处理 MySQL 事务,而且还可以使访问数据库工作变得更加稳定。是一个数据库驱动
#--with-iconv-dir #种字符集间的转换
#--with-freetype-dir #打开对 freetype 字体库的支持
#--with-jpeg-dir #打开对 jpeg 图片的支持
#--with-png-dir #打开对 png 图片的支持
#--with-zlib #打开 zlib 库的支持,实现 GZIP 压缩输出 
#--with-libxml-dir=/usr #打开 libxml2 库的支持,libxml 是一个用来解析 XML 文档的函数库
#--enable-xml #支持 xml 文档
#--disable-rpath #关闭额外的运行库文件
#--enable-bcmath #打开图片大小调整,用到 zabbix 监控的时候用到了这个模块
#--enable-shmop #shmop 共享内存操作函数,可以与 c/c++通讯
#--enable-sysvsem #加上上面 shmop,这样就使得你的 PHP 系统可以处理相关的 IPC 函数(活动在内核级别)。
#--enable-inline-optimization #优化线程
#--with-curl #打开 curl 浏览工具的支持
#--with-curlwrappers #运用 curl 工具打开 url 流 ,新版 PHP5.6 已弃用
#--enable-mbregex #支持多字节正则表达式
#--enable-fpm #CGI 方式安装的启动程序,PHP-FPM 服务
#--enable-mbstring #多字节,字符串的支持
#--with-gd #打开 gd 库的支持,是 php 处理图形的扩展库,GD 库提供了一系列用来处理图片的 API,使用 GD 库可以处理图片,或者生成图片。
#--enable-gd-native-ttf #支持 TrueType 字符串函数库
#--with-openssl #打开 ssl 支持
#--with-mhash #支持 mhash 算法扩展
#--enable-pcntl #freeTDS 需要用到的,pcntl 扩展可以支持 php 的多线程操作
#--enable-sockets #打开 sockets 支持
#--with-xmlrpc #打开 xml-rpc 的 c 语言
#--enable-zip #打开对 zip 的支持
#--enable-soap #扩展库通过 soap 协议实现了客户端与服务器端的数据交互操作
#--with-mcrypt #mcrypt 算法扩展


#编译安装
make -j 2 && make install

#配置 php 和 php-fpm
cp /usr/local/src/php-7.1.24/php.ini-production /usr/local/php/php.ini

#PHP-FPM 配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf

#PHP-FPM 启动脚本
sed -i 's/nobody/nginx/g' /usr/local/php/etc/php-fpm.conf

cp /usr/local/src/php-7.1.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

chkconfig php-fpm on

##启动PHP-FPM
/etc/init.d/php-fpm start

##检查PHP-FMP是否启动成功
netstat -antup | grep php-fpm

#写入PHP测试页面
cat > /usr/local/nginx/html/index.php <<eof
<?php
 phpinfo();
 ?>
eof


#关闭防火墙
systemctl stop firewalld
setenforce 0


echo " "
echo "LNMP搭建完成"
echo " "

猜你喜欢

转载自blog.csdn.net/weixin_45412401/article/details/112385307
今日推荐