centos8 LNMP shell一键脚本

#!/bin/bash
# set -x
#基础环境
environment(){
    echo "正在更新和下载依赖软件,请耐心等待!"
    #更新yum
    mkdir -p /etc/yum.repos.d/repo
    yum -y install wget curl
    mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo/
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo  2>1&>/dev/null
    yum makecache  2>1&>/dev/null
    yum  install epel-release  2>1&>/dev/null
    yum -y update  2>1&>/dev/null
    #下载需要的所有依赖包
    yum -y install epel-release
    yum -y update
    yum -y install make gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxslt libxslt-devel gd-devel libxml2-devel libpng libpng-devel curl-devel numactl cmake ncurses-devel libtirpc-devel libtool sqlite-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel

    #关闭防火墙
    systemctl stop firewalld  2>1&>/dev/null
    systemctl disable firewalld  2>1&>/dev/null
    systemctl stop iptables  2>1&>/dev/null
    systemctl disable iptables  2>1&>/dev/null
    #关闭selinux
    setenforce 0 2>1&>/dev/null
    sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
    #设置时区
    timedatectl set-timezone Asia/Shanghai
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    #下载二进制包
    mkdir -p /opt/tools && cd /opt/tools
    wget http://nginx.org/download/nginx-1.18.0.tar.gz 2>1&>/dev/null
    wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz  2>1&>/dev/null
    wget http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-boost-8.0.20.tar.gz  2>1&>/dev/null
    wget https://www.php.net/distributions/php-7.4.16.tar.gz  2>1&>/dev/null
    wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz  2>1&>/dev/null
    echo "依赖环境已完成安装"
}
#调用函数
environment

#安装nginx
echo "开始安装nginx"
cd /opt/tools
ls -l nginx*.gz 2>1&>/dev/null
if [ $? ! -eq "0" ] ; then
    echo "没有准备nginx1.18 软件包!"
    exit 1
fi
#创建用户
useradd -M -s /sbin/nologin nginx
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
#配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-debug \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream_realip_module \
--with-stream

#编译安装
make -j4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
#制作service
cat >/usr/lib/systemd/system/nginx.service <<eof
[Unit]                                          
Description=nginx 
After=network.target 

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
eof

#刷新文件
systemctl daemon-reload

#修改配置文件,支持php
cp /usr/local/nginx/conf/nginx.conf{,.bak}
sed -i "45s/index.htm;/index.htm index.php;/" /usr/local/nginx/conf/nginx.conf
sed -i "65,71s/#//" /usr/local/nginx/conf/nginx.conf
sed -i "s#\/scripts$fastcgi_script_name#\$document_root$fastcgi_script_name#" /usr/local/nginx/conf/nginx.conf
nginx -t
#启动并开机自启
systemctl start nginx
systemctl enable nginx
echo "nginx 安装完毕"

#安装php
echo "开始安装php"
cd /opt/tools  
#安装oniguruma-6.9.4
ls -l oniguruma*.gz 2>1&>/dev/null
if [ $? ! -eq "0" ] ; then
    echo "没有准备oniguruma-6.9.4 软件包!"
    exit 1
fi 
tar -xvf  oniguruma-6.9.4.tar.gz 2>1&>/dev/null
cd oniguruma-6.9.4/
./autogen.sh && ./configure --prefix=/usr
make && make install

#解压php
cd /opt/tools
ls -l php*.gz 2>1&>/dev/null
if [ $? ! -eq "0" ] ; then
    echo "没有准备php-7.4.16 软件包!"
    exit 1 
fi

tar -xvf php-7.4.16.tar.gz 2>1&>/dev/null
cd php-7.4.16
#编辑PHP的配置项
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx \
--enable-inline-optimization --disable-debug --disable-rpath \
--enable-shared --enable-soap  --with-xmlrpc \
--with-openssl --with-mhash  --with-sqlite3 \
--with-zlib --enable-bcmath --with-iconv --with-bz2 \
--enable-calendar --with-curl --with-cdb --enable-dom \
--enable-exif --enable-fileinfo --enable-filter \
--enable-ftp  --with-openssl-dir --with-zlib-dir \
--enable-gd-jis-conv --with-gettext --with-gmp --with-mhash \
--enable-json --enable-mbstring --enable-mbregex  \
--enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-zlib-dir --with-pdo-sqlite --with-readline --enable-session \
--enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg \
--enable-sysvsem --enable-sysvshm --with-xsl  \
--enable-mysqlnd-compression-support --with-pear \
--enable-opcache --disable-fileinfo

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

#添加环境变量
echo "export PATH=$PATH:/usr/local/php/bin" >> /etc/profile
source  /etc/profile
#准备配置文件
cp php.ini-production /usr/local/php/etc/php.ini
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
ln -s /usr/local/php/etc/ /etc/php

#修改 /usr/local/php/etc/php-fpm.conf 运行用户和组改为nginx
chown nginx.nginx /usr/local/php/etc/php-fpm.conf
chown -R nginx.nginx /etc/php

#禁用PHP功能
sed -i "s#disable_functions =#disable_functions =\"passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,wnam,posix_getpwuid, posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname\"#" /etc/php/php.ini 

#设置支持mysql
sed -i "s#pdo_mysql.default_socket=#pdo_mysql.default_socket=/tmp/mysql.sock#" /etc/php/php.ini
sed -i "s#mysqli.default_socket =#mysqli.default_socket =/tmp/mysql.sock#" /etc/php/php.ini 

#设置开机自启,并启动
cp /opt/tools/php-7.4.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
chkconfig php-fpm on
systemctl start php-fpm
echo "php 安装完毕"

#安装mysql
echo "开始安装mysql"
cd /opt/tools
useradd -M -s /sbin/nologin mysql
mkdir -p /data/mysql 
chown -R mysql:mysql /data/mysql 
mkdir -p /usr/local/mysql
#安装依赖插件
ls -l rpcsvc*.gz 2>1&>/dev/null
if [ $? ! -eq "0" ] ; then
    echo "没有准备rpcsvc-proto-1.4 软件包!"
    exit 1
fi 
tar -zxvf rpcsvc-proto-1.4.tar.gz 2>1&>/dev/null
cd rpcsvc-proto-1.4/ && ./configure && make && make install
ls -l mysql*.gz 2>1&>/dev/null
if [ $? ! -eq "0" ] ; then
    echo "没有准备mysql-boost-8.0.19 软件包!"
    exit 1
fi
cd /opt/tools
tar -xvf mysql-boost-8.0.19.tar.gz 2>1&>/dev/null
cd mysql-8.0.19
#配置参数
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DINSTALL_DATADIR=/data/mysql \
-DMYSQL_USER=mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DFORCE_INSOURCE_BUILD=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DWITH_BOOST=/opt/tools/mysql-8.0.19/boost

#编译安装,时间会等待较久
make -j 2 && make install
#修改mysql目录权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql

#初始化mysql,无密码
# --initialize-insecure 无密码登录
cd /usr/local/mysql/
./bin/mysqld --initialize-insecure  --user=mysql --datadir=/data/mysql --character-set-server=utf8

#mysql 8.0.x默认没有配置文件,我们自己创建一个
cat >/usr/local/mysql/my.cnf <<EOF
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
user = mysql
socket=/tmp/mysql.sock
tmpdir = /tmp
basedir=/usr/local/mysql
datadir=/data/mysql
key_buffer_size=16M
max_allowed_packet=128M
default_authentication_plugin=mysql_native_password
open_files_limit = 60000
explicit_defaults_for_timestamp
server-id = 1
character-set-server = utf8
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
federated
max_connections = 1000
max_connect_errors = 100000
interactive_timeout = 86400
wait_timeout = 86400
sync_binlog=0
back_log=100
default-storage-engine = InnoDB
log_slave_updates = 1
[mysqldump]
quick
[mysqld-8.0]
sql_mode=TRADITIONAL
[mysqladmin]
force
[mysqld]
key_buffer_size=16M
EOF
#制作软链
ln -s /usr/local/mysql/my.cnf /etc/my.cnf

#把Mysql加入系统启动
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
#增加执行权限
chmod 755 /etc/init.d/mysqld
#启动,并开机自启
chkconfig mysqld on
systemctl start mysqld

#编辑文件
sed -i "46s#basedir=#basedir=/usr/local/mysql#" /etc/init.d/mysqld 
sed -i "47s#datadir=#datadir=/data/mysql#" /etc/init.d/mysqld 

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source  /etc/profile
#以下为把myslq的库文件链接到系统默认的位置
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
mkdir /var/lib/mysql  2>1&>/dev/null
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
echo "mysql安装完毕"

#测试
cat > /usr/local/nginx/html/index.php <<eof
<?php
phpinfo();
?>
eof
IP=`ifconfig | grep inet | awk NR==1'{print $2}'`
echo "浏览器访问 http://$IP/index.php"

echo " "
echo "LNMP搭建完成"
echo " "
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值