温馨提示×

centos lnmp怎样部署网站

小樊
44
2026-10-06 04:50:36
栏目: 云计算

在 CentOS 上使用 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 部署网站,一般流程如下(以 CentOS 7/8 为例,适用于大多数场景):


一、准备环境

1. 更新系统

yum update -y

2. 关闭 SELinux(可选,生产可配置而不是关闭)

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

3. 开放防火墙端口

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

二、安装 Nginx

yum install -y nginx
systemctl start nginx
systemctl enable nginx

测试:浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。


三、安装 MySQL / MariaDB

使用 MariaDB(推荐)

yum install -y mariadb-server mari mariadb
systemctl start mariadb
systemctl enable mariadb

初始化数据库

mysql_secure_installation

创建网站数据库:

mysql -u root -p
CREATE DATABASE webdb DEFAULT CHARSET utf8mb4;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON webdb.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

四、安装 PHP

CentOS 7 示例(PHP 7.4)

yum install -y epel-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils
yum-config-manager --enable remi-php74
yum install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml

启动 PHP-FPM

systemctl start php-fpm
systemctl enable php-fpm

五、配置 Nginx 支持 PHP

修改站点配置

vi /etc/nginx/conf.d/web.conf

示例配置:

server {
    listen 80;
    server_name www.example.com;

    root /usr/share/nginx/html/web;
    index index.php index.html;

    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;
    }
}

重载 Nginx

nginx -t
systemctl reload nginx

六、部署网站代码

mkdir -p /usr/share/nginx/html/web

上传代码(示例):

cd /usr/share/nginx/html/web
echo "<?php phpinfo(); ?>" > index.php

浏览器访问:

http://服务器IP

七、(可选)使用 LNMP 一键包(新手推荐)

wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh lnmp

安装完成后:

lnmp vhost add

按提示添加网站即可。


八、常见问题

  • 403 错误:检查目录权限
chown -R nginx:nginx /usr/share/nginx/html/web
chmod -R 755 /usr/share/nginx/html/web
  • PHP 不解析:确认 php-fpm 已启动,Nginx 配置正确

如果你告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 网站类型(WordPress / 自写 PHP / 框架)

我可以给你 更精确的部署方案。

0 踩