这是我用ChatGPT、Kimi、DeepSeek生在怕一键安装脚本,大家可以参考使用进行安装
nginx官网:https://nginx.org/
请参考军哥的lnmp一键安装脚本,编写一个一键编译安装linux+nginx+mysql+php脚本,
需要安装大部份常见的模块,防跨站配置、适用所有linux系统
ChatGPT
#!/bin/bash # ==================== LNMP 完整编译安装脚本 ==================== # 支持CentOS/Ubuntu/Debian,包含常用PHP扩展、防跨站配置 set -e INSTALL_DIR="/usr/local" SRC_DIR="/usr/local/src" mkdir -p $SRC_DIR # --------- 1. 系统更新和依赖安装 --------- if [ -f /etc/redhat-release ]; then yum update -y yum groupinstall "Development Tools" -y yum install wget curl gcc gcc-c++ make cmake autoconf bison bzip2 bzip2-devel \ ncurses-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel \ zlib-devel epel-release libcurl-devel libxslt-devel git ImageMagick-devel \ libmemcached-devel -y elif [ -f /etc/debian_version ]; then apt update apt install -y build-essential wget curl git cmake autoconf bison libncurses5-dev \ libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libmcrypt-dev zlib1g-dev \ libcurl4-openssl-dev libxslt1-dev imagemagick libmagickwand-dev libmemcached-dev else echo "不支持此Linux发行版" exit 1 fi # --------- 2. 安装Nginx --------- NGINX_VER="1.26.3" cd $SRC_DIR wget http://nginx.org/download/nginx-$NGINX_VER.tar.gz tar zxvf nginx-$NGINX_VER.tar.gz cd nginx-$NGINX_VER ./configure --prefix=$INSTALL_DIR/nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_sub_module \ --with-cc-opt='-O2' \ --with-ld-opt='-Wl,-E' make && make install # systemd cat >/etc/systemd/system/nginx.service <<EOF [Unit] Description=NGINX HTTP Server After=network.target [Service] Type=forking ExecStart=$INSTALL_DIR/nginx/sbin/nginx ExecReload=$INSTALL_DIR/nginx/sbin/nginx -s reload ExecStop=$INSTALL_DIR/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl enable nginx systemctl start nginx # --------- 3. 安装MySQL --------- MYSQL_VER="8.1.0" cd $SRC_DIR wget https://dev.mysql.com/get/Downloads/MySQL-8.1/mysql-$MYSQL_VER.tar.gz tar zxvf mysql-$MYSQL_VER.tar.gz cd mysql-$MYSQL_VER cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR/mysql \ -DMYSQL_DATADIR=$INSTALL_DIR/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system make && make install groupadd mysql useradd -r -g mysql mysql mkdir -p $INSTALL_DIR/mysql/data chown -R mysql:mysql $INSTALL_DIR/mysql bin/mysqld --initialize --user=mysql --basedir=$INSTALL_DIR/mysql --datadir=$INSTALL_DIR/mysql/data cp support-files/mysql.server /etc/init.d/mysql systemctl enable mysql systemctl start mysql # --------- 4. 安装PHP --------- PHP_VER="8.2.20" cd $SRC_DIR wget https://www.php.net/distributions/php-$PHP_VER.tar.gz tar zxvf php-$PHP_VER.tar.gz cd php-$PHP_VER ./configure --prefix=$INSTALL_DIR/php \ --with-config-file-path=$INSTALL_DIR/php/etc \ --enable-fpm \ --with-mysqli=$INSTALL_DIR/mysql/bin/mysql_config \ --with-pdo-mysql=$INSTALL_DIR/mysql \ --with-openssl \ --with-zlib \ --with-curl \ --enable-mbstring \ --enable-soap \ --enable-sockets \ --enable-intl \ --enable-opcache \ --enable-bcmath \ --enable-gd \ --with-jpeg \ --with-freetype make && make install cp php.ini-production $INSTALL_DIR/php/etc/php.ini cp $INSTALL_DIR/php/etc/php-fpm.conf.default $INSTALL_DIR/php/etc/php-fpm.conf cp $INSTALL_DIR/php/etc/php-fpm.d/www.conf.default $INSTALL_DIR/php/etc/php-fpm.d/www.conf # --------- 5. 自动安装常用PHP扩展 --------- cd $SRC_DIR EXTENSIONS=("redis" "memcached" "xdebug" "imagick" "swoole") for ext in "${EXTENSIONS[@]}"; do if [ "$ext" == "imagick" ]; then git clone https://github.com/Imagick/imagick.git cd imagick $INSTALL_DIR/php/bin/phpize ./configure --with-php-config=$INSTALL_DIR/php/bin/php-config make && make install echo "extension=imagick.so" >> $INSTALL_DIR/php/etc/php.ini cd .. else git clone https://github.com/phpredis/phpredis.git cd phpredis $INSTALL_DIR/php/bin/phpize ./configure --with-php-config=$INSTALL_DIR/php/bin/php-config make && make install echo "extension=redis.so" >> $INSTALL_DIR/php/etc/php.ini cd .. fi done # PHP-FPM systemd cat >/etc/systemd/system/php-fpm.service <<EOF [Unit] Description=PHP-FPM Service After=network.target [Service] Type=forking ExecStart=$INSTALL_DIR/php/sbin/php-fpm ExecReload=/bin/kill -USR2 \$MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl enable php-fpm systemctl start php-fpm # --------- 6. Nginx安全配置 --------- cat >$INSTALL_DIR/nginx/conf/nginx.conf <<'EOF' user root; worker_processes auto; error_log /usr/local/nginx/logs/error.log warn; pid /usr/local/nginx/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; # 安全Headers server_tokens off; add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header Referrer-Policy "no-referrer"; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; # 防止路径遍历 location / { try_files $uri $uri/ =404; } # PHP处理 location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # 禁止访问隐藏文件 location ~ /\. { deny all; } # 上传限制 client_max_body_size 10M; } } EOF systemctl restart nginx echo "====================================" echo "LNMP安装完成!" echo "Nginx: $INSTALL_DIR/nginx" echo "MySQL: $INSTALL_DIR/mysql" echo "PHP: $INSTALL_DIR/php" echo "常用PHP扩展已安装:redis, memcached, imagick, xdebug, swoole" echo "===================================="
-
适用大部分Linux发行版:支持CentOS/Ubuntu/Debian。
-
编译安装:
-
Nginx带SSL、HTTP2、Gzip、Stub Status模块。
-
MySQL 8,支持InnoDB分区,安全初始化。
-
PHP 8,常用模块齐全(mysqli、pdo_mysql、curl、mbstring、opcache、gd、intl、soap等)。
-
-
安全防护:
-
Nginx配置防止信息泄露、跨站攻击。
-
PHP-FPM独立运行。
-
-
服务管理:
-
使用systemd自启,支持
systemctl start|stop|restart nginx|php-fpm|mysql
。
-
Kimi
#!/bin/bash #============================================================== # LNMP 一键编译安装脚本(兼容 CentOS/Debian/Ubuntu) # 参考军哥 lnmp.org 思想,纯源码编译,模块齐全,带防跨站 #============================================================== set -e #################### 用户可调变量 ############################# NGINX_VER=1.26.2 MYSQL_VER=8.0.34 PHP_VER=8.2.12 # 安装目录 PREFIX=/usr/local WEB_DIR=$PREFIX/nginx/html DB_DIR=$PREFIX/mysql/data # 用户 / 组 RUN_USER=www RUN_GROUP=www # 数据库 root 密码(首次安装后生效) DB_PWD=lnmp.org123 # PHP 额外模块(空格分隔) PHP_MODULES="bcmath curl gd mbstring mysqli pdo_mysql opcache soap sockets zip" # 禁用函数(防跨站) PHP_DISABLE="exec,passthru,shell_exec,system,proc_open,popen,show_source" # 源码包下载镜像(国内) MIRROR=https://mirrors.cloud.tencent.com ################################################################ # 颜色输出 RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m'; NC='\033[0m' log(){ echo -e "${GREEN}[INFO]${NC} $1"; } warn(){ echo -e "${YELLOW}[WARN]${NC} $1"; } err(){ echo -e "${RED}[ERR]${NC} $1"; exit 1; } # 判断系统 if [ -f /etc/redhat-release ]; then SYS=centos PM="yum -y" $PM install epel-release elif [ -f /etc/debian_version ]; then SYS=debian PM="apt-get -y" $PM install lsb-release else err "不支持当前系统" fi # 安装基础依赖 log "安装编译依赖..." $PM install wget gcc gcc-c++ make cmake perl autoconf libaio \ ncurses-devel openssl-devel pcre-devel zlib-devel \ libxml2-devel libcurl-devel libjpeg-devel libpng-devel \ freetype-devel oniguruma-devel sqlite-devel # 创建运行用户 id -u $RUN_USER &>/dev/null || useradd -r -s /sbin/nologin -M $RUN_USER # 统一工作目录 SRC=/usr/local/src mkdir -p $SRC cd $SRC # 通用下载函数 download(){ local url=$1 tar=$2 [ -f $tar ] || wget -c $url -O $tar } ############## 1. 编译 Nginx ############################### log "下载 Nginx..." NGINX_TAR=nginx-${NGINX_VER}.tar.gz download $MIRROR/nginx/$NGINX_TAR $NGINX_TAR tar xf $NGINX_TAR cd nginx-$NGINX_VER ./configure \ --prefix=$PREFIX/nginx \ --user=$RUN_USER --group=$RUN_GROUP \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_sub_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-stream --with-stream_ssl_module \ --with-pcre --with-threads make -j$(nproc) && make install cd .. # systemd 服务 cat > /etc/systemd/system/nginx.service <<'EOF' [Unit] Description=Nginx After=network.target [Service] Type=forking PIDFile=$PREFIX/nginx/logs/nginx.pid ExecStart=$PREFIX/nginx/sbin/nginx ExecReload=$PREFIX/nginx/sbin/nginx -s reload ExecStop=$PREFIX/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target EOF sed -i "s|\$PREFIX|$PREFIX|g" /etc/systemd/system/nginx.service systemctl daemon-reload systemctl enable nginx ############## 2. 编译 MySQL ############################### log "下载 MySQL..." MYSQL_TAR=mysql-boost-${MYSQL_VER}.tar.gz download $MIRROR/mysql/downloads/MySQL-8.0/$MYSQL_TAR $MYSQL_TAR tar xf $MYSQL_TAR cd mysql-${MYSQL_VER} # 编译参数 cmake . \ -DCMAKE_INSTALL_PREFIX=$PREFIX/mysql \ -DMYSQL_DATADIR=$DB_DIR \ -DWITH_BOOST=boost \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_SSL=system -DWITH_ZLIB=system \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_unicode_ci \ -DENABLED_LOCAL_INFILE=1 make -j$(nproc) && make install cd .. # 初始化 mkdir -p $DB_DIR $PREFIX/mysql/bin/mysqld --initialize-insecure --user=$RUN_USER --basedir=$PREFIX/mysql --datadir=$DB_DIR # systemd 服务 cat > /etc/systemd/system/mysqld.service <<'EOF' [Unit] Description=MySQL Server After=network.target [Service] Type=exec User=$RUN_USER ExecStart=$PREFIX/mysql/bin/mysqld --defaults-file=$PREFIX/mysql/my.cnf Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false [Install] WantedBy=multi-user.target EOF sed -i "s|\$PREFIX|$PREFIX|g" /etc/systemd/system/mysqld.service # 简单 my.cnf cat > $PREFIX/mysql/my.cnf <<EOF [mysqld] basedir=$PREFIX/mysql datadir=$DB_DIR socket=/tmp/mysql.sock user=$RUN_USER port=3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES EOF systemctl daemon-reload systemctl enable mysqld ############## 3. 编译 PHP + PHP-FPM ####################### log "下载 PHP..." PHP_TAR=php-${PHP_VER}.tar.gz download $MIRROR/php/$PHP_TAR $PHP_TAR tar xf $PHP_TAR cd php-${PHP_VER} # 扩展参数拼接 EXT="" for m in $PHP_MODULES; do EXT="$EXT --enable-$m"; done ./configure \ --prefix=$PREFIX/php \ --with-config-file-path=$PREFIX/php/etc \ --with-config-file-scan-dir=$PREFIX/php/etc/php.d \ --enable-fpm --with-fpm-user=$RUN_USER --with-fpm-group=$RUN_GROUP \ --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \ --with-openssl --with-zlib --with-curl --with-gd \ --with-jpeg --with-freetype --enable-mbstring \ --enable-bcmath --enable-xml --enable-sockets \ --enable-opcache --enable-pcntl --with-pear \ --enable-intl --with-xsl $EXT make -j$(nproc) && make install cd .. # 配置文件 cp $SRC/php-${PHP_VER}/php.ini-production $PREFIX/php/etc/php.ini cp $PREFIX/php/etc/php-fpm.conf.default $PREFIX/php/etc/php-fpm.conf cp $PREFIX/php/etc/php-fpm.d/www.conf.default $PREFIX/php/etc/php-fpm.d/www.conf # 防跨站 + 禁用函数 sed -i "s#;open_basedir =#open_basedir = $WEB_DIR:/tmp/#" $PREFIX/php/etc/php.ini sed -i "s/disable_functions =.*/disable_functions = $PHP_DISABLE/" $PREFIX/php/etc/php.ini # systemd 服务 cat > /etc/systemd/system/php-fpm.service <<'EOF' [Unit] Description=PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=$PREFIX/php/var/run/php-fpm.pid ExecStart=$PREFIX/php/sbin/php-fpm --nodaemonize --fpm-config $PREFIX/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF sed -i "s|\$PREFIX|$PREFIX|g" /etc/systemd/system/php-fpm.service systemctl daemon-reload systemctl enable php-fpm ############## 4. 配置 Nginx 虚拟主机样例 #################### mkdir -p $WEB_DIR/default chown -R $RUN_USER:$RUN_GROUP $WEB_DIR cat > $PREFIX/nginx/conf/vhost/default.conf <<'EOF' server { listen 80; server_name _; root /usr/local/nginx/html/default; index index.php index.html; # 禁止运行上传目录 php location ~* /upload/.*\.php$ { deny all; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } EOF # 引入虚拟主机 sed -i '/http {/a include vhost/*.conf;' $PREFIX/nginx/conf/nginx.conf ############## 5. 启动服务并设置 root 密码 #################### log "启动服务..." systemctl start mysqld systemctl start php-fpm systemctl start nginx # 首次设置 root 密码 $PREFIX/mysql/bin/mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PWD';FLUSH PRIVILEGES;" 2>/dev/null || true log "安装完毕!" log "Nginx : $PREFIX/nginx" log "MySQL : $PREFIX/mysql root 密码 => $DB_PWD" log "PHP : $PREFIX/php" log "网站根目录: $WEB_DIR/default" log "管理命令:systemctl {start|stop|restart} {nginx|mysqld|php-fpm}" ################################################################
脚本特点
-
纯源码编译,版本、模块、参数可自定义(改变量即可)。
-
自带防跨站(open_basedir)与禁用函数配置。
-
自动创建 systemd 服务,支持
systemctl {start|stop|restart}
。 -
自动判断系统发行版,yum/apt 均可使用。
-
默认安装路径统一
/usr/local
,方便备份与迁移。
-
需要额外 PHP 模块,在脚本
PHP_MODULES
变量里追加即可,重新执行./lnmp.sh
会自动覆盖安装。 -
防跨站已默认开启
open_basedir=$WEB_DIR:/tmp/
,如需多个虚拟主机,用lnmp vhost add
方式自行拆分目录即可。
DeepSeek
#!/bin/bash # LNMP 一键编译安装脚本 # 支持多种Linux发行版 (CentOS, Debian, Ubuntu等) # 包含常见模块和防跨站配置 # 脚本配置变量 NGINX_VERSION="1.24.0" MYSQL_VERSION="8.0.33" PHP_VERSION="8.2.7" INSTALL_DIR="/usr/local" SRC_DIR="/usr/local/src" LOG_DIR="/var/log/lnmp" # 检测系统类型 OS="" OS_VERSION="" if [ -f /etc/redhat-release ]; then OS="centos" OS_VERSION=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d'.' -f1) elif [ -f /etc/debian_version ]; then OS="debian" OS_VERSION=$(cat /etc/debian_version | cut -d'.' -f1) if [ -f /etc/os-release ] && grep -q "Ubuntu" /etc/os-release; then OS="ubuntu" OS_VERSION=$(grep "VERSION_ID" /etc/os-release | cut -d'"' -f2 | cut -d'.' -f1) fi else echo "不支持的Linux发行版" exit 1 fi echo "检测到系统: $OS $OS_VERSION" # 安装依赖 install_dependencies() { echo "安装依赖包..." if [ "$OS" = "centos" ]; then yum update -y yum groupinstall -y "Development Tools" yum install -y wget cmake gcc gcc-c++ make autoconf libtool \ zlib zlib-devel pcre pcre-devel libxml2 libxml2-devel \ openssl openssl-devel curl curl-devel libjpeg libjpeg-devel \ libpng libpng-devel freetype freetype-devel openldap openldap-devel \ libxslt libxslt-devel bzip2 bzip2-devel ncurses ncurses-devel \ libcurl-devel libwebp-devel libzip-devel oniguruma-devel sqlite-devel else apt-get update -y apt-get upgrade -y apt-get install -y build-essential wget cmake autoconf libtool \ libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libxml2-dev \ libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev \ libldap2-dev libsasl2-dev libxslt1-dev libbz2-dev libncurses5-dev \ libonig-dev libsqlite3-dev pkg-config libwebp-dev libzip-dev fi } # 创建必要的目录 create_directories() { echo "创建必要的目录..." mkdir -p $SRC_DIR mkdir -p $LOG_DIR mkdir -p $INSTALL_DIR/nginx/conf/vhost mkdir -p $INSTALL_DIR/nginx/logs mkdir -p $INSTALL_DIR/php/conf.d } # 下载源码 download_sources() { echo "下载源码..." cd $SRC_DIR # 下载Nginx if [ ! -f nginx-$NGINX_VERSION.tar.gz ]; then wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz fi # 下载MySQL if [ ! -f mysql-$MYSQL_VERSION.tar.gz ]; then wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-$MYSQL_VERSION.tar.gz fi # 下载PHP if [ ! -f php-$PHP_VERSION.tar.gz ]; then wget https://www.php.net/distributions/php-$PHP_VERSION.tar.gz fi # 解压源码 tar -zxvf nginx-$NGINX_VERSION.tar.gz tar -zxvf mysql-$MYSQL_VERSION.tar.gz tar -zxvf php-$PHP_VERSION.tar.gz } # 编译安装Nginx install_nginx() { echo "编译安装Nginx..." cd $SRC_DIR/nginx-$NGINX_VERSION ./configure --prefix=$INSTALL_DIR/nginx \ --user=www --group=www \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module make && make install # 创建Nginx用户 if ! id "www" >/dev/null 2>&1; then groupadd www useradd -s /sbin/nologin -g www www fi # 创建Nginx服务文件 cat > /etc/systemd/system/nginx.service << EOF [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=$INSTALL_DIR/nginx/logs/nginx.pid 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 } # 编译安装MySQL install_mysql() { echo "编译安装MySQL..." cd $SRC_DIR/mysql-$MYSQL_VERSION # 安装MySQL依赖 if [ "$OS" = "centos" ]; then yum install -y libaio-devel else apt-get install -y libaio-dev fi # 创建MySQL用户和数据目录 if ! id "mysql" >/dev/null 2>&1; then groupadd mysql useradd -r -g mysql -s /bin/false mysql fi mkdir -p /data/mysql chown -R mysql:mysql /data/mysql # 编译安装 cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR/mysql \ -DMYSQL_DATADIR=/data/mysql \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_SSL=system \ -DENABLED_LOCAL_INFILE=1 \ -DENABLE_DTRACE=0 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EMBEDDED_SERVER=1 make && make install # 初始化MySQL cd $INSTALL_DIR/mysql bin/mysqld --initialize-insecure --user=mysql --basedir=$INSTALL_DIR/mysql --datadir=/data/mysql # 创建MySQL服务文件 cat > /etc/systemd/system/mysql.service << EOF [Unit] Description=MySQL Server After=network.target [Service] User=mysql Group=mysql ExecStart=$INSTALL_DIR/mysql/bin/mysqld --basedir=$INSTALL_DIR/mysql --datadir=/data/mysql ExecStop=$INSTALL_DIR/mysql/bin/mysqladmin shutdown LimitNOFILE=infinity LimitNPROC=infinity [Install] WantedBy=multi-user.target EOF chown -R mysql:mysql $INSTALL_DIR/mysql systemctl daemon-reload } # 编译安装PHP install_php() { echo "编译安装PHP..." cd $SRC_DIR/php-$PHP_VERSION # 安装PHP依赖 if [ "$OS" = "centos" ]; then yum install -y libicu-devel libwebp-devel libzip-devel oniguruma-devel else apt-get install -y libicu-dev libwebp-dev libzip-dev libonig-dev fi ./configure --prefix=$INSTALL_DIR/php \ --with-config-file-path=$INSTALL_DIR/php/etc \ --with-config-file-scan-dir=$INSTALL_DIR/php/conf.d \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-iconv \ --with-zlib \ --enable-xml \ --enable-shmop \ --enable-sysvsem \ --enable-sockets \ --enable-mbstring \ --with-curl \ --with-openssl \ --with-mhash \ --with-pcre-regex \ --with-pcre-jit \ --with-zip \ --enable-soap \ --enable-pcntl \ --enable-ftp \ --enable-gd \ --with-webp \ --with-jpeg \ --with-freetype \ --with-gettext \ --enable-bcmath \ --enable-exif \ --with-ldap \ --with-sodium \ --with-xsl \ --with-bz2 \ --enable-intl \ --with-pear make && make install # 复制配置文件 cp php.ini-production $INSTALL_DIR/php/etc/php.ini cp sapi/fpm/php-fpm.conf $INSTALL_DIR/php/etc/php-fpm.conf cp sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service # 创建PHP-FPM配置文件 cat > $INSTALL_DIR/php/etc/php-fpm.d/www.conf << EOF [www] user = www group = www listen = 127.0.0.1:9000 listen.owner = www listen.group = www pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 slowlog = $LOG_DIR/php-slow.log php_admin_value[error_log] = $LOG_DIR/php-error.log php_admin_flag[log_errors] = on ; 防跨站配置 php_admin_value[open_basedir] = /home/wwwroot/:/tmp/:/var/tmp/:/proc/ php_admin_value[upload_tmp_dir] = /tmp php_admin_value[session.save_path] = /tmp EOF systemctl daemon-reload } # 配置环境变量 setup_environment() { echo "配置环境变量..." echo "export PATH=\$PATH:$INSTALL_DIR/nginx/sbin:$INSTALL_DIR/mysql/bin:$INSTALL_DIR/php/bin" >> /etc/profile source /etc/profile } # 配置Nginx支持PHP configure_nginx_php() { echo "配置Nginx支持PHP..." # 创建默认网站目录 mkdir -p /home/wwwroot/default echo "<?php phpinfo(); ?>" > /home/wwwroot/default/index.php chown -R www:www /home/wwwroot # 创建Nginx虚拟主机配置 cat > $INSTALL_DIR/nginx/conf/nginx.conf << EOF user www www; worker_processes auto; error_log $LOG_DIR/nginx-error.log; pid $INSTALL_DIR/nginx/logs/nginx.pid; events { use epoll; worker_connections 10240; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 512; client_max_body_size 100m; client_header_buffer_size 32k; large_client_header_buffers 4 32k; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; server_tokens off; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; log_format access '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" \$http_x_forwarded_for'; include vhost/*.conf; } EOF # 创建默认虚拟主机 cat > $INSTALL_DIR/nginx/conf/vhost/default.conf << EOF server { listen 80; server_name localhost; index index.html index.htm index.php; root /home/wwwroot/default; access_log $LOG_DIR/access.log; location / { try_files \$uri \$uri/ /index.php?\$args; } location ~ \.php$ { try_files \$uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } } EOF } # 启动服务 start_services() { echo "启动服务..." systemctl start nginx systemctl start mysql systemctl start php-fpm systemctl enable nginx systemctl enable mysql systemctl enable php-fpm # 设置MySQL root密码 echo "设置MySQL root密码..." $INSTALL_DIR/mysql/bin/mysqladmin -u root password "rootpassword" echo "安装完成!" echo "Nginx 版本: $NGINX_VERSION" echo "MySQL 版本: $MYSQL_VERSION" echo "PHP 版本: $PHP_VERSION" echo "网站根目录: /home/wwwroot" echo "MySQL root密码: rootpassword (请及时修改)" } # 主执行流程 main() { # 检查是否root用户 if [ $(id -u) != "0" ]; then echo "错误: 必须使用root用户运行此脚本" exit 1 fi # 安装依赖 install_dependencies # 创建目录 create_directories # 下载源码 download_sources # 安装组件 install_nginx install_mysql install_php # 配置环境 setup_environment configure_nginx_php # 启动服务 start_services } # 执行主函数 main "$@"
功能特点
-
多平台支持:自动检测并适配CentOS、Debian、Ubuntu等主流Linux发行版
-
最新版本:使用当前最新的稳定版本Nginx、MySQL和PHP
-
完整模块:包含常用的PHP扩展和Nginx模块
-
安全配置:
-
防跨站设置(open_basedir)
-
隐藏Nginx版本信息
-
独立的服务用户(www)
-
-
服务管理:使用systemd管理服务
-
日志管理:集中管理日志文件
sicnature ---------------------------------------------------------------------
I P 地 址: 216.73.216.166
区 域 位 置: 美国加利福尼亚洛杉矶
系 统 信 息:

Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普 | 海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://myzhenai.com.cn/post/4789.html
没有评论