温馨提示×

centos lnmp怎么部署网站

小樊
43
2026-10-12 02:13:19
栏目: 云计算

下面给你一个在 CentOS 上部署 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)并上线网站的实用流程,适合 CentOS 7 / 8(示例以 CentOS 7 为主)。


一、准备工作

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(CentOS 默认)

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

方式二:MySQL(如需)

yum install -y https://dev.mysql.com/get/mysql80-community-release-el7.rpm
yum install -y mysql-community-server
systemctl start mysqld

四、安装 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/default.conf

示例配置:

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

    root /usr/share/nginx/html;
    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 restart nginx

六、部署网站代码

1. 上传网站文件

cd /usr/share/nginx/html

可使用:

  • FTP
  • scp
  • git clone

2. 测试 PHP

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

访问:http://IP/info.php


七、配置数据库

mysql -u root -p
CREATE DATABASE site_db;
CREATE USER 'site_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON site_db.* TO 'site_user'@'localhost';
FLUSH PRIVILEGES;

网站配置文件中填写对应数据库信息。


八、常见网站类型示例

  • WordPress

    • 下载:wget https://wordpress.org/latest.tar.gz
    • 解压到网站目录
    • 浏览器访问安装
  • ThinkPHP / Laravel

    • root 指向 public
    • 配置 rewrite

九、常见问题

✅ 403 错误
→ 检查目录权限

chown -R nginx:nginx /usr/share/nginx/html
chmod -R 755 /usr/share/nginx/html

✅ 502 错误
→ php-fpm 未启动或端口错误

✅ 网站打不开
→ 检查防火墙 / 安全组 / Nginx 是否运行


如果你愿意,可以告诉我:

  • CentOS 版本
  • 网站类型(WordPress / 自研 PHP / Java)
  • 是否要 HTTPS(SSL)

我可以直接给你完整可复制的配置方案。

0 踩