在centos7.9上部署LNMP
时间: 2025-06-18 12:35:03 浏览: 9
### CentOS 7.9 部署 LNMP 环境并解决 Nginx 显示 PHP 默认网页错误
#### 一、环境准备
在部署 LNMP (Linux, Nginx, MySQL/MariaDB, PHP) 环境之前,确保服务器已更新至最新状态,并关闭防火墙和服务冲突项。可以执行以下命令来完成基础设置:
```bash
yum update -y
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
```
---
#### 二、安装依赖库
为了支持后续的编译过程,需先安装必要的开发工具和依赖包:
```bash
yum groupinstall "Development Tools" -y
yum install epel-release gcc pcre-devel zlib-devel openssl-devel libxml2-devel bzip2-devel curl-devel gd-devel oniguruma-devel libjpeg-turbo-devel freetype-devel libpng-devel -y
```
---
#### 三、创建用户组与用户
为了避免权限问题,在安装前建议为 Nginx 创建专用用户和组:
```bash
groupadd -g 8080 nginx
useradd -g 8080 -s /sbin/nologin -u 8080 -M nginx
```
---
#### 四、Nginx 编译安装
按照标准流程下载、解压、配置并安装 Nginx:
1. 下载源码包:
```bash
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxf nginx-1.26.2.tar.gz
```
2. 进入目录并配置选项:
```bash
cd nginx-1.26.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_realip_module \
--with-http_ssl_module \
--with-pcre \
--with-stream \
--with-http_stub_status_module
```
3. 编译并安装:
```bash
make && make install
```
4. 设置环境变量以便全局调用 `nginx` 命令:
```bash
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
source /etc/profile
```
5. 启动 Nginx 并验证端口监听情况:
```bash
nginx
ss -ntulp | grep nginx
```
如果看到类似如下输出,则表示启动成功:
```
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=xxxx,fd=6))
```
---
#### 五、MySQL 或 MariaDB 安装
可以选择官方 Yum 源快速安装 MySQL 或 MariaDB 数据库。
1. 添加 MariaDB 的 Yum 源:
```bash
cat <<EOF >/etc/yum.repos.d/mariadb.repo
[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
enabled=1
EOF
```
2. 安装 MariaDB 及其服务管理工具:
```bash
yum install mariadb-server mariadb-client -y
systemctl start mariadb && systemctl enable mariadb
```
3. 修改初始密码(假设当前无密码):
```sql
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
FLUSH PRIVILEGES;
```
---
#### 六、PHP 编译安装
以下是基于源码方式安装 PHP 的具体步骤。
1. **安装所需依赖**
如果未提前安装某些扩展库,可能会导致 PHP 功能受限。例如:
```bash
yum install libmcrypt-devel mhash mcrypt aspell-devel recode-devel enchant-devel tidyhtml-devel re2c bison autoconf automake cmake -y
```
2. **下载 PHP 源码**
```bash
cd /usr/local/src/
wget https://www.php.net/distributions/php-8.2.x.tar.gz
tar zxf php-8.2.x.tar.gz
cd php-8.2.x
```
3. **配置编译参数**
使用以下常用模块组合进行配置:
```bash
./configure \
--prefix=/usr/local/php \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype \
--with-jpeg \
--with-zlib \
--with-libxml \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-soap \
--with-gettext \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-pear \
--enable-session \
--with-onig \
--with-xsl \
--without-imap \
--without-ldap \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/conf.d
```
4. **编译并安装**
```bash
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
```
5. **调整 PHP 参数**
根据需求修改 `/usr/local/php/etc/php.ini` 文件中的关键参数:
```ini
post_max_size = 32M
upload_max_filesize = 32M
memory_limit = 128M
max_execution_time = 600
max_input_time = 600
date.timezone = Asia/Shanghai
short_open_tag = On
```
6. **启动 PHP-FPM**
复制默认的服务脚本并启用 FPM:
```bash
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
sed -i 's#php_fpm_CONF#/usr/local/php/etc/php-fpm.conf#g' /etc/init.d/php-fpm
sed -i 's#php_fpm_PID#/usr/local/php/var/run/php-fpm.pid#g' /etc/init.d/php-fpm
systemctl daemon-reload
systemctl start php-fpm && systemctl enable php-fpm
```
---
#### 七、Nginx 和 PHP 协同工作
为了让 Nginx 能够解析 PHP 页面,需要编辑 Nginx 配置文件 `/usr/local/nginx/conf/nginx.conf`,添加 FastCGI 支持部分:
```nginx
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
```
完成后重新加载 Nginx 配置:
```bash
nginx -t && nginx -s reload
```
---
#### 八、测试 PHP 是否生效
在网站根目录下新建一个简单的 PHP 测试页:
```bash
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php
```
通过浏览器访问该地址(如 `http://<IP>/index.php`),若能正常显示 PHP 版本信息则表明配置成功[^1]。
---
####
阅读全文
相关推荐











