基于 Centos7/8 一键安装LNMP脚本
注:此脚本只适用于centos 7/8,具体需要根据实际情况进行测试调整。
[root@centos8 ~]# cat lnmp_centos7or8.sh
#!/bin/bash
#
#**************************************************
#Author: Xan_Yum
#QQ: 7993167
#Email: waluna@qq.com
#Version: 1.0
#Date: 2022-04-28
#FileName: lnmp_centos7or8.sh
#Description: The test script
#URL: https://waluna.top
#Copyroght (C): 2022 ALL rights reserved
#**************************************************
. /etc/init.d/functions
COLOR='echo -e \e[1;31m'
END='\e[0m'
SRC_DIR=`pwd`
CPU=`lscpu| awk '/^CPU\(s\):/{print $NF}'`
OS=`awk -F'"' '/PRETTY_NAME/{print $2}' /etc/os-release|tr ' ' '-'`
MYSQL='mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz'
MYSQL_ROOT_PASSWORD="waluna"
nginx='nginx-1.18.0.tar.gz'
php='php-7.4.22.tar.gz'
$COLOR"Begin install LNMP,Please wait..."$END
download_file(){
$COLOR"begin download file..."$END
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz &> /dev/null
wget http://nginx.org/download/nginx-1.18.0.tar.gz &> /dev/null
wget https://www.php.net/distributions/php-7.4.22.tar.gz &> /dev/null
}
mysql_check(){
cd $SRC_DIR
if [ $UID -ne 0 ];then
action "user isn't root,install false" false
exit 1
fi
if [ ! -e $MYSQL ];then
$COLOR"install file isn't exist"$END
$COLOR"please download install file"$END
exit
elif [ -e /usr/local/mysql ];then
$COLOR"mysql is exist,install false"$END
exit
else
return
fi
}
install_mysql(){
$COLOR"begin install mysql..."$END
yum install libaio perl-devel ncurses-compat-libs -y &> /dev/null
mkdir -p /usr/local/src &> /dev/null
tar xf $MYSQL -C /usr/local/src
MYSQL_DIR=`echo $MYSQL|sed -nr 's/(.*[0-9]).*/\1/p'`
ln -s /usr/local/src/$MYSQL_DIR /usr/local/mysql
id mysql &> /dev/null || { useradd -r -u 306 -s /bin/false mysql;action "create mysql user";}
chown -R mysql.mysql /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<EOF
[mysqld]
server-id=1
log-bin=/data/mysql/mysql-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
bind-address=127.0.0.1
[client]
socket=/data/mysql/mysql.sock
EOF
[ -d /data/mysql ] || mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql/
mysqld --initialize --user=mysql --datadir=/data/mysql
cat > /lib/systemd/system/mysql.service <<EOF
[Unit]
Description=MySQL 5.7.33 database server
After=syslog.target
After=network.target
[Service]
#Type=notify
#User=mysql
#Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
Execstop=/usr/local/mysql/bin/mysqladmin -S /data/mysql/mysql.sock shutdown
TimeoutSec=300
PrivateTmp=true
Restart=on-failure
RestartPreventExitStatus=1
LimitNOFILE = 10000
Environment=MYSQLD_PARENT_PID=1
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysql &> /dev/null
[ $? -ne 0 ] && { $COLOR"mysql start false!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p"$MYSQL_OLDPASSWORD" password "$MYSQL_ROOT_PASSWORD" &> /dev/null
action "mysql install complete"
}
nignx_check(){
cd $SRC_DIR
if [ ! -e $nginx ];then
$COLOR"install file isn't exist"$END
$COLOR"please download install file"$END
exit
elif [ -e /usr/local/nginx ];then
$COLOR"nginx is exist,install false"$END
exit
else
return
fi
}
install_nginx(){
$COLOR"begin install nginx..."$END
yum install gcc make pcre-devel openssl-devel zlib-devel -y &> /dev/null
tar xf nginx-1.18.0.tar.gz -C /usr/local/src
nginx_dir=`echo $nginx|sed -nr 's/(.*[0-9]).*/\1/p'`
id www &> /dev/null || { useradd -r -s /sbin/nologin www;action "create www user";}
cd /usr/local/src/$nginx_dir/
./configure --prefix=/usr/local/$nginx_dir \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module &> /dev/null
make -j $CPU &> /dev/null && make install &> /dev/null
ln -s /usr/local/$nginx_dir /usr/local/nginx
cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
chown -R www.www /usr/local/nginx/
sed -i.bak -e '45s/index.html/index.php &/' -e '65,71s/#//' -e 's#/scripts#$document_root#' /usr/local/nginx/conf/nginx.conf
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
[ $? -ne 0 ] && { $COLOR"nginx start false!"$END;exit; }
action "nginx install complete"
}
php_check(){
cd $SRC_DIR
if [ ! -e $php ];then
$COLOR"install file isn't exist"$END
$COLOR"please download install file"$END
exit
elif [ -e /usr/local/php ];then
$COLOR"php is exist,install false"$END
exit
else
return
fi
}
install_php(){
$COLOR"begin install php..."$END
yum install libxml2-devel curl-devel libpng-devel freetype-devel libjpeg-devel openldap-devel openssl-devel bzip2-devel libmcrypt-devel sqlite-devel -y &> /dev/null
if [[ $OS == CentOS-Linux-7* ]];then
yum install oniguruma-devel -y &> /dev/null
elif [[ $OS == CentOS-Linux-8* ]];then
dnf --enablerepo=PowerTools install oniguruma-devel -y &> /dev/null
fi
cp -frp /usr/lib64/libldap* /usr/lib/
tar xf $php -C /usr/local/src
php_dir=`echo $php|sed -nr 's/(.*[0-9]).*/\1/p'`
cd /usr/local/src/$php_dir
./configure --prefix=/usr/local/$php_dir \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--enable-sockets \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-libzip \
--enable-soap \
--without-pear \
--with-gettext \
--disable-fileinfo \
--enable-maintainer-zts \
--with-ldap \
--disable-fileinfo &> /dev/null
sed -ri.bak 's/(^EXTRA_LIBS.*)/\1 -llber/' Makefile
make -j $CPU &> /dev/null && make install &> /dev/null
echo "PATH=/usr/local/$php_dir/bin:\$PATH" > /etc/profile.d/php.sh
. /etc/profile.d/php.sh
cp php.ini-production /usr/local/$php_dir/etc/php.ini
cp /usr/local/$php_dir/etc/php-fpm.conf.default /usr/local/$php_dir/etc/php-fpm.conf
cat > /usr/local/$php_dir/etc/php-fpm.d/www.conf <<EOF
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /status
ping.path = /ping
access.log = log/\$pool.access.log
slowlog = log/\$pool.log.slow
EOF
ln -s /usr/local/$php_dir/ /usr/local/php
mkdir /usr/local/php/log
chown -R www.www /usr/local/php/
sed -i.bak 's/ProtectSystem=full/ProtectSystem=false/' /usr/local/src/$php_dir/sapi/fpm/php-fpm.service
cp /usr/local/src/$php_dir/sapi/fpm/php-fpm.service /usr/lib/systemd/system
systemctl daemon-reload
systemctl enable --now php-fpm.service &> /dev/null
[ $? -ne 0 ] && { $COLOR"php start false!"$END;exit; }
action "php install complete"
}
check(){
$COLOR"checking..."$END
ss -ntl|grep 3306 &> /dev/null
a=`echo $?`
ss -ntl|grep 80 &> /dev/null
b=`echo $?`
ss -ntl|grep 9000 &> /dev/null
c=`echo $?`
if [[ $a -eq 0 && $b -eq 0 && $c -eq 0 ]];then
action "LNMP insall complete!"
elif [ $a -eq 1 ];then
action "mysql is down" false
elif [ $b -eq 1 ];then
action "nginx is down" false
elif [ $c -eq 1 ];then
action "php is down" false
else
action "Please check for errors" false
fi
}
download_file
sleep 3
mysql_check
install_mysql
sleep 3
nignx_check
install_nginx
sleep 3
php_check
install_php
sleep 3
check







Comments | NOTHING