安装lnmp 的shell脚本
时间: 2025-06-12 18:12:13 浏览: 33
<think>我们被要求提供一个用于安装LNMP(Linux,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 脚本
以下是一个完整的 LNMP(Linux + 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]
阅读全文
相关推荐
















