在 CentOS 上使用 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 部署网站,一般流程如下(以 CentOS 7/8 为例,适用于大多数场景):
yum update -y
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
yum install -y nginx
systemctl start nginx
systemctl enable nginx
测试:浏览器访问服务器 IP,看到 Nginx 欢迎页即成功。
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;
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
systemctl start php-fpm
systemctl enable php-fpm
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 -t
systemctl reload nginx
mkdir -p /usr/share/nginx/html/web
上传代码(示例):
cd /usr/share/nginx/html/web
echo "<?php phpinfo(); ?>" > index.php
浏览器访问:
http://服务器IP
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
按提示添加网站即可。
chown -R nginx:nginx /usr/share/nginx/html/web
chmod -R 755 /usr/share/nginx/html/web
php-fpm 已启动,Nginx 配置正确如果你告诉我:
我可以给你 更精确的部署方案。