安装lnmp的shell脚本
时间: 2025-06-12 09:48:18 浏览: 23
### LNMP 环境安装 Shell 脚本
以下是一个用于安装 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)环境的完整 Shell 脚本,适用于 CentOS/RHEL 系统[^1]。
```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
```
#### 功能说明
- **组件版本控制**:脚本支持自定义 Nginx、PHP 和 MySQL 的版本[^2]。
- **自动化安装流程**:自动安装系统依赖、编译 Nginx、通过 YUM 安装 MySQL 和 PHP,并配置 PHP-FPM 与 Nginx 的集成[^3]。
- **安全配置**:MySQL 自动进行安全初始化,PHP-FPM 使用独立用户运行,服务由 systemd 管理[^4]。
#### 使用方法
1. 将脚本保存为 `install_lnmp.sh`。
2. 添加执行权限:`chmod +x install_lnmp.sh`。
3. 运行脚本:`./install_lnmp.sh`。
4. 安装完成后访问 `http://服务器IP/info.php` 测试 PHP。
#### 验证安装
```bash
# 检查服务状态
systemctl status nginx
systemctl status mysqld
systemctl status php-fpm
# 测试 PHP 处理
curl http://localhost/info.php
# 测试 MySQL 连接
mysql -u root -p
```
#### 注意事项
- 脚本需要至少 2GB 内存运行。
- 首次安装 MySQL 后需修改 root 密码。
- 生产环境建议禁用 `info.php` 等测试文件,并配置防火墙规则。
阅读全文
相关推荐
















