温馨提示×

centos lnmp怎么部署

小樊
39
2026-10-15 00:56:52
栏目: 智能运维

下面给你一份**在 CentOS 上部署 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)**的常用实操流程,适用于 CentOS 7 / 8(Stream)。


一、系统准备

# 更新系统
yum update -y

# 关闭 SELinux(生产建议配置规则,而不是关闭)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 防火墙放行端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

二、安装 Nginx

CentOS 7

yum install epel-release -y
yum install nginx -y

CentOS 8

dnf install nginx -y
systemctl start nginx
systemctl enable nginx

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


三、安装 MySQL / MariaDB

方案 A:MariaDB(推荐 CentOS 默认)

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

方案 B:MySQL 8(CentOS 7/8 通用)

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

# 查看初始密码
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation

四、安装 PHP

CentOS 7(PHP 7.4 示例)

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

CentOS 8

dnf install php php-fpm php-mysqlnd php-json php-gd php-mbstring -y
systemctl start php-fpm
systemctl enable php-fpm

五、配置 Nginx 支持 PHP

编辑站点配置:

vi /etc/nginx/conf.d/default.conf

示例配置:

server {
    listen 80;
    server_name your_domain_or_ip;
    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 -t
systemctl restart nginx

六、测试 PHP

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

访问:

http://服务器IP/info.php

七、常见补充

1. 文件权限

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

2. PHP 上传大小

vi /etc/php.ini
upload_max_filesize = 50M
post_max_size = 50M
systemctl restart php-fpm

3. 一键脚本(新手可选)

wget http://soft.vpser.net/lnmp/lnmp1.9.tar.gz
tar zxf lnmp1.9.tar.gz
cd lnmp1.9
./install.sh

如果你告诉我:

  • CentOS 具体版本
  • 是否用 MySQL 还是 MariaDB
  • 是否要 HTTPS / 多站点 / 生产环境

我可以给你更精准的配置方案。

0 踩