lnmp源码安装脚本

#!/bin/bash
cat <<END
+---------------------------------------------+
	This is a LNMP
	1.安装常用软件包
	2.安装nginx
	3.安装mysql
	4.安装php
	5.一键安装lnmp
+---------------------------------------------+
END
read -p "请输入一个数字:" num

#1.安装常用软件包

[ "$num" == "1" ] && {
echo -e "开始安装常用软件包,更新yum源……"
yum -y remove mariadb* && yum install -y vim lirzs gcc* wget bash-completion unzip && \
wget http://mirrors.aliyun.com/repo/Centos-7.repo -P /etc/yum.repos.d/ && yum -y install epel-release && yum clean all && yum makecache && echo "安装成功!"
}

#2.安装Nginx1.14.0
#安装部分

[ "$num" == "2" ] && {
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* && wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.zip --no-check-certificate && unzip pcre-8.42.zip -d /usr/local/src/ && wget http://nginx.org/download/nginx-1.14.0.tar.gz && tar zxf nginx-1.14.0.tar.gz -C /usr/local/src/ && cd /usr/local/src/nginx-1.14.0/ && ./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-http_ssl_module --user=nginx --group=nginx --with-pcre=/usr/local/src/pcre-8.42 && make && make install 
if [ -e /usr/local/nginx ]; then
echo "安装nginx成功!" && /usr/local/nginx/sbin/nginx
else 
echo "安装nginx失败!"
fi

#配置部分

useradd -M -s /sbin/nologin nginx && echo "开始配置Nginx解析PHP,请稍后.............." 
    if [ -f /usr/local/nginx/conf/nginx.conf ];then
       sed -i 's/#user  nobody;/user  nginx nginx;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    root           html;/    root           html;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    fastcgi_pass   127.0.0.1:9000;/    fastcgi_pass   127.0.0.1:9000;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    fastcgi_index  index.php;/    fastcgi_index  index.php;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's*#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    include        fastcgi_params;/    include        fastcgi.conf;/g' /usr/local/nginx/conf/nginx.conf
       sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf
       sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
     else
       action "Nginx解析PHP失败!!!" /bin/false
       exit 1
   fi

#添加环境变量,优化nginx服务

/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx && sed -i "/export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL/a\export PATH=$PATH:/usr/local/nginx/sbin" /etc/profile
source /etc/profile 
cat>/etc/init.d/nginx<<EOF
#!/bin/bash
# chkconfig: 2345 99 20
#description: nginx-server
nginx=/usr/local/nginx/sbin/nginx
case $1 in
        start)
                netstat -anptu | grep nginx
                if [ $? -eq 0 ]
                then
                    echo "nginx service is already running"
                else
                     echo "nginx Service started successfully "
                    $nginx
                fi
         ;;
        stop)
                $nginx -s stop
                if [ $? -eq 0 ]
                then
                    echo "nginx service closed successfully"
                else
                    echo "nginx server stop fail,try again"
                fi
        ;;
        status)
                netstat -anlpt | grep nginx
                if [ $? -eq 0 ]
                then
                    echo "nginx server is running"
                else
                    echo "nginx service not started "
                fi
        ;;
        restart)
                 $nginx -s reload
                 if [ $? -eq 0 ]
                 then
                     echo "nginx service restart successfully "
                 else
                     echo "nginx server restart failed"
                 fi
        ;;
        *)
                 echo "please enter {start restart status stop}"
        ;;
esac
EOF
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
}

#3.安装MYSQL5.7.20

[ "$num" == "3" ] && {
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel && wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.20.tar.gz && tar zxf mysql-boost-5.7.20.tar.gz -C /usr/local/src/ && cd /usr/local/src/mysql-5.7.20/ && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/mysql-5.7.20/boost/boost_1_59_0 -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql && make && make install 
if [ "$?" -eq 0 ];then
     echo "mysql安装成功!!!!"
     useradd -M -s /sbin/nologin -r mysql && mkdir -p /usr/local/mysql/data && chown -R mysql.mysql /usr/local/mysql/
   else
     echo "mysql安装失败"
     exit 1
fi 

#创建用户和数据目录

useradd -M -s /sbin/nologin -r mysql && mkdir -p /usr/local/mysql/data && chown -R mysql.mysql /usr/local/mysql/
cat>/etc/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
pid-file=/usr/local/mysql/mysqld.pid     
log-error=/var/log/mysqld.log
EOF
 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chkconfig --add mysqld && chkconfig mysqld on
sed -i 's*basedir=*basedir=/usr/local/mysql*g' /etc/init.d/mysqld
sed -i 's*datadir=*datadir=/usr/local/mysql/data*g' /etc/init.d/mysqld
sed -i "/export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL/a\export PATH=$PATH:/usr/local/mysql/bin" /etc/profile
source /etc/profile && chmod +x mysqld.service && systemctl enable mysqld.service && touch /var/log/mysqld.log && chown mysql:mysql /var/log/mysqld.log && chcon system_u:object_r:mysqld_log_t:s0 /var/log/mysqld.log
if [ "$?" -eq 0 ];then
     echo "mysql初始化成功!!"
   else
     echo "mysql安装失败"
     exit 1
fi 
}

#安装PHP-7.2.6

[ "$num" == "4" ] && {
echo "开始安装PHP依赖包……"
yum -y install php-mcrypt libmcrypt libmcrypt-devel  autoconf  freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel
if [ "$?" -eq 0 ];then
     echo "恭喜你,PHP依赖包安装成功!!!!"
   else
     echo "PHP依赖包安装失败"
     exit 1
fi

#下载解压php源码包

wget http://cn2.php.net/distributions/php-7.2.6.tar.gz && tar zxf php-7.2.6.tar.gz -C /usr/local/src/ && cd /usr/local/src/php-7.2.6/ && cp -frp /usr/lib64/libldap* /usr/lib/ && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql --with-mysql-sock=/usr/local/mysql/mysql.sock --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-curl --with-gd --with-gmp --with-zlib --with-xmlrpc --with-openssl --without-pear --with-snmp --with-gettext --with-mhash --with-libxml-dir=/usr --with-ldap --with-ldap-sasl --with-fpm-user=nginx --with-fpm-group=nginx --enable-xml --enable-fpm  --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-sockets --enable-inline-optimization --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-zip --disable-fileinfo --disable-rpath --enable-libxml --enable-opcache  --enable-mysqlnd && sed -i 's/XTRA_LIBS = -lcrypt -lz -lresolv -lcrypt -lrt -lldap -lgmp -lpng -lz -ljpeg -lz -lrt -lm -ldl -lnsl -lpthread -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lnetsnmp -lssl -lssl -lcrypto -lm -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt/& -llber/g' Makefile && make && make install
if [ "$?" -eq 0 ];then
     echo "恭喜你,PHP安装成功!!!!"
   else
     echo "PHP安装失败,请检查环境..."
     exit 1
fi

#配置php环境

echo "开始配置php环境……"
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf && cp /usr/local/src/php-7.2.6/php.ini-production /usr/local/php/etc/php.ini && cp /usr/local/src/php-7.2.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on && /etc/init.d/php-fpm start && service nginx restart
cat>/usr/local/nginx/html/index.php<<EOF
<?php

        phpinfo();

?>
EOF
}

#_______________________________________________
#一键安装lnmp

[ "$num" == "5" ] && {
echo -e "开始安装常用软件包,更新yum源……"
yum -y remove mariadb* && yum install -y vim lirzs gcc* wget bash-completion unzip && \
wget http://mirrors.aliyun.com/repo/Centos-7.repo -P /etc/yum.repos.d/ && yum -y install epel-release && yum clean all && yum makecache && echo "安装成功!"
#2.安装Nginx1.14.0
#安装部分
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* && wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.zip --no-check-certificate && unzip pcre-8.42.zip -d /usr/local/src/ && wget http://nginx.org/download/nginx-1.14.0.tar.gz && tar zxf nginx-1.14.0.tar.gz -C /usr/local/src/ && cd /usr/local/src/nginx-1.14.0/ && ./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-http_ssl_module --user=nginx --group=nginx --with-pcre=/usr/local/src/pcre-8.42 && make && make install 
if [ -e /usr/local/nginx ]; then
echo "安装nginx成功!" && /usr/local/nginx/sbin/nginx
else 
echo "安装nginx失败!"
fi
#配置部分
useradd -M -s /sbin/nologin nginx && echo "开始配置Nginx解析PHP,请稍后.............." 
    if [ -f /usr/local/nginx/conf/nginx.conf ];then
         sed -i 's/#user  nobody;/user  nginx nginx;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    root           html;/    root           html;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    fastcgi_pass   127.0.0.1:9000;/    fastcgi_pass   127.0.0.1:9000;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    fastcgi_index  index.php;/    fastcgi_index  index.php;/g' /usr/local/nginx/conf/nginx.conf
       sed -i 's*#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*g' /usr/local/nginx/conf/nginx.conf
       sed -i 's/#    include        fastcgi_params;/    include        fastcgi.conf;/g' /usr/local/nginx/conf/nginx.conf
       sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf
       sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
     else
       action "Nginx解析PHP失败!!!" /bin/false
       exit 1
   fi
#添加环境变量,优化nginx服务
/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx && sed -i "/export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL/a\export PATH=$PATH:/usr/local/nginx/sbin" /etc/profile
source /etc/profile 
cat>/etc/init.d/nginx<<EOF
#!/bin/bash
# chkconfig: 2345 99 20
#description: nginx-server
nginx=/usr/local/nginx/sbin/nginx
case $1 in
        start)
                netstat -anptu | grep nginx
                if [ $? -eq 0 ]
                then
                    echo "nginx service is already running"
                else
                     echo "nginx Service started successfully "
                    $nginx
                fi
         ;;
        stop)
                $nginx -s stop
                if [ $? -eq 0 ]
                then
                    echo "nginx service closed successfully"
                else
                    echo "nginx server stop fail,try again"
                fi
        ;;
        status)
                netstat -anlpt | grep nginx
                if [ $? -eq 0 ]
                then
                    echo "nginx server is running"
                else
                    echo "nginx service not started "
                fi
        ;;
        restart)
                 $nginx -s reload
                 if [ $? -eq 0 ]
                 then
                     echo "nginx service restart successfully "
                 else
                     echo "nginx server restart failed"
                 fi
        ;;
        *)
                 echo "please enter {start restart status stop}"
        ;;
esac
EOF
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

#3.安装MYSQL5.7.20
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel && wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.20.tar.gz && tar zxf mysql-boost-5.7.20.tar.gz -C /usr/local/src/ && cd /usr/local/src/mysql-5.7.20/ && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/mysql-5.7.20/boost/boost_1_59_0 -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql && make && make install 
if [ "$?" -eq 0 ];then
     echo "mysql安装成功!!!!"
     useradd -M -s /sbin/nologin -r mysql && mkdir -p /usr/local/mysql/data && chown -R mysql.mysql /usr/local/mysql/
   else
     echo "mysql安装失败"
     exit 1
fi 
#创建用户和数据目录
useradd -M -s /sbin/nologin -r mysql && mkdir -p /usr/local/mysql/data && chown -R mysql.mysql /usr/local/mysql/
cat>/etc/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
pid-file=/usr/local/mysql/mysqld.pid     
log-error=/var/log/mysqld.log
EOF
 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chkconfig --add mysqld && chkconfig mysqld on
sed -i 's*basedir=*basedir=/usr/local/mysql*g' /etc/init.d/mysqld
sed -i 's*datadir=*datadir=/usr/local/mysql/data*g' /etc/init.d/mysqld
sed -i "/export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL/a\export PATH=$PATH:/usr/local/mysql/bin" /etc/profile
source /etc/profile && chmod +x mysqld.service && systemctl enable mysqld.service 
4.#安装PHP-7.2.6
#安装依赖包
echo "开始安装PHP依赖包……"
yum -y install php-mcrypt libmcrypt libmcrypt-devel  autoconf  freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel
if [ "$?" -eq 0 ];then
     echo "恭喜你,PHP依赖包安装成功!!!!"
   else
     echo "PHP依赖包安装失败"
     exit 1
fi
#下载解压php源码包
wget http://cn2.php.net/distributions/php-7.2.6.tar.gz && tar zxf php-7.2.6.tar.gz -C /usr/local/src/ && cd /usr/local/src/php-7.2.6/ && cp -frp /usr/lib64/libldap* /usr/lib/ && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql --with-mysql-sock=/usr/local/mysql/mysql.sock --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-curl --with-gd --with-gmp --with-zlib --with-xmlrpc --with-openssl --without-pear --with-snmp --with-gettext --with-mhash --with-libxml-dir=/usr --with-ldap --with-ldap-sasl --with-fpm-user=nginx --with-fpm-group=nginx --enable-xml --enable-fpm  --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-sockets --enable-inline-optimization --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-zip --disable-fileinfo --disable-rpath --enable-libxml --enable-opcache  --enable-mysqlnd && sed -i 's/XTRA_LIBS = -lcrypt -lz -lresolv -lcrypt -lrt -lldap -lgmp -lpng -lz -ljpeg -lz -lrt -lm -ldl -lnsl -lpthread -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lnetsnmp -lssl -lssl -lcrypto -lm -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt/& -llber/g' Makefile && make && make install
if [ "$?" -eq 0 ];then
     echo "恭喜你,PHP安装成功!!!!"
   else
     echo "PHP安装失败,请检查环境..."
     exit 1
fi
#配置php环境
echo "开始配置php环境……"
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf && cp /usr/local/src/php-7.2.6/php.ini-production /usr/local/php/etc/php.ini && cp /usr/local/src/php-7.2.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on && /etc/init.d/php-fpm start && service nginx restart
cat>/usr/local/nginx/html/index.php<<EOF
<?php

        phpinfo();

?>
EOF
}
if [ "$?" -eq 0 ];then
     echo "lnmp安装配置成功!!
   else
     echo "lnmp安装失败"
     exit 1
fi 
<think>我们被要求提供一个用于安装LNMPLinux,Nginx,MySQL/MariaDB,PHP)环境的Shell脚本。注意:由于不同的Linux发行版包管理器和软件源不同,这里我们以CentOS7为例。脚本将包括以下步骤:1.安装Nginx2.安装MySQL(这里选择MariaDB作为替代,因为CentOS7默认支持MariaDB)3.安装PHP4.配置Nginx与PHP协同工作5.启动服务并设置开机自启注意:该脚本需要以root用户运行。我们将编写一个脚本,并添加必要的注释。由于安装过程中可能需要用户输入(如设置MySQLroot密码),我们将尽量自动化,但某些步骤(如设置密码)可能需要交互。我们可以使用预置的密码或者通过变量设置,但为了安全,建议在安装过程中由用户输入。不过,在脚本中自动设置密码通常使用`mysql_secure_installation`的自动化方式。但是,请注意,在生产环境中,自动设置密码可能会带来安全风险,因此本脚本仅用于测试环境。我们将按照以下步骤:1.更新系统2.安装Nginx3.安装MariaDB并设置root密码(这里我们将密码设置为变量,但实际使用中应避免在脚本中硬编码密码)4.安装PHP和常用扩展5.配置Nginx支持PHP(修改默认站点配置)6.测试PHP7.启动服务注意:在CentOS中,我们使用yum安装。由于是示例脚本,我们将尽量简化,并忽略一些错误处理。实际使用中应添加错误检查。我们使用以下变量:-MYSQL_ROOT_PASSWORD:设置MariaDB的root密码(示例中设置为一个默认密码,但用户应该修改)注意:这个脚本仅适用于测试环境,生产环境需要更安全的设置。下面是脚本内容:</think>### LNMP 环境一键安装 Shell 脚本 以下是一个完整的 LNMPLinux + Nginx + MySQL/MariaDB + PHP)环境自动化安装脚本,适用于 CentOS/RHEL 系统: ```bash #!/bin/bash # LNMP 环境一键安装脚本 # 支持 CentOS 7/8 和 RHEL 系统 # 安装选项配置 NGINX_VERSION="1.24.0" # Nginx 版本 PHP_VERSION="8.2" # PHP 版本 MYSQL_VERSION="8.0" # MySQL 版本 INSTALL_DIR="/opt/lnmp" # 安装目录 # 检查是否为 root 用户 if [ "$(id -u)" != "0" ]; then echo "错误:必须使用 root 用户运行此脚本!" exit 1 fi # 安装依赖库 function install_dependencies() { echo "正在安装系统依赖..." yum install -y epel-release yum install -y wget gcc make cmake openssl-devel pcre-devel zlib-devel \ libxml2-devel libjpeg-devel libpng-devel freetype-devel \ curl-devel libxslt-devel bzip2-devel sqlite-devel yum groupinstall -y "Development Tools" } # 安装 Nginx function install_nginx() { echo "正在安装 Nginx ${NGINX_VERSION}..." cd ${INSTALL_DIR} wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz tar zxvf nginx-${NGINX_VERSION}.tar.gz cd nginx-${NGINX_VERSION} ./configure --prefix=${INSTALL_DIR}/nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre \ --with-stream make && make install # 创建 Nginx 服务文件 cat > /lib/systemd/system/nginx.service <<EOF [Unit] Description=The nginx HTTP and reverse proxy server After=network.target [Service] Type=forking PIDFile=${INSTALL_DIR}/nginx/logs/nginx.pid ExecStartPre=${INSTALL_DIR}/nginx/sbin/nginx -t ExecStart=${INSTALL_DIR}/nginx/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID ExecStop=/bin/kill -s QUIT \$MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable nginx systemctl start nginx } # 安装 MySQL function install_mysql() { echo "正在安装 MySQL ${MYSQL_VERSION}..." cd ${INSTALL_DIR} wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm rpm -ivh mysql80-community-release-el7-7.noarch.rpm yum install -y mysql-community-server systemctl start mysqld systemctl enable mysqld # 获取临时密码 TEMP_PASSWORD=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}') echo "生成的 MySQL 临时密码: ${TEMP_PASSWORD}" # 安全设置 mysql_secure_installation <<EOF ${TEMP_PASSWORD} y y y y y EOF } # 安装 PHP function install_php() { echo "正在安装 PHP ${PHP_VERSION}..." yum install -y https://rpms.remirepo.net/enterprise/remi-release-$(rpm -E %rhel).rpm yum install -y yum-utils yum-config-manager --enable remi-php${PHP_VERSION//./} yum install -y php php-fpm php-mysqlnd php-opcache php-gd \ php-curl php-mbstring php-xml php-zip \ php-json php-bcmath php-pecl-redis # 配置 PHP-FPM sed -i 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf sed -i 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf sed -i 's/;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf sed -i 's/;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf systemctl start php-fpm systemctl enable php-fpm } # 配置 Nginx 支持 PHP function configure_nginx_php() { echo "正在配置 Nginx 支持 PHP..." # 创建测试 PHP 文件 mkdir -p ${INSTALL_DIR}/nginx/html echo "<?php phpinfo(); ?>" > ${INSTALL_DIR}/nginx/html/info.php # 配置 Nginx 站点 cat > ${INSTALL_DIR}/nginx/conf/nginx.conf <<EOF user nginx; worker_processes auto; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root html; index index.php index.html index.htm; location / { try_files \$uri \$uri/ /index.php?\$query_string; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; include fastcgi_params; } } } EOF # 创建 Nginx 用户 useradd -M -s /sbin/nologin nginx chown -R nginx:nginx ${INSTALL_DIR}/nginx systemctl restart nginx } # 主安装流程 function main() { mkdir -p ${INSTALL_DIR} install_dependencies install_nginx install_mysql install_php configure_nginx_php echo "==================================================" echo "LNMP 环境安装完成!" echo "访问 http://服务器IP/info.php 测试 PHP" echo "MySQL root 密码: 查看安装日志中的临时密码" echo "Nginx 安装目录: ${INSTALL_DIR}/nginx" echo "==================================================" } main ``` #### 脚本功能说明 1. **组件版本控制**: - 可自定义 Nginx、PHP 和 MySQL 版本 - 默认安装 PHP 8.2 + MySQL 8.0 + Nginx 1.24 2. **自动化安装流程**: - 自动安装系统依赖 - 源码编译安装 Nginx - YUM 安装 MySQL 和 PHP - 自动配置 PHP-FPM 和 Nginx 集成 3. **安全配置**: - MySQL 自动安全初始化 - PHP-FPM 使用独立用户运行 - 服务自动配置为 systemd 管理 #### 使用说明 1. 保存为 `install_lnmp.sh` 2. 添加执行权限:`chmod +x install_lnmp.sh` 3. 运行脚本:`./install_lnmp.sh` 4. 安装完成后访问 `http://服务器IP/info.php` 测试 #### 验证安装 ```bash # 检查服务状态 systemctl status nginx systemctl status mysqld systemctl status php-fpm # 测试 PHP 处理 curl http://localhost/info.php # 测试 MySQL 连接 mysql -u root -p ``` #### 注意事项 1. 脚本需要 2GB 以上内存运行 2. 首次安装 MySQL 后需修改 root 密码 3. 生产环境建议: - 修改默认安装目录 - 禁用 `info.php` 等测试文件 - 配置防火墙规则[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值