在 CentOS 系统上配置服务器同时托管网站和应用服务,需要合理规划系统资源、安装必要的软件组件,并进行安全与性能优化。以下是一个完整的配置指南,适用于常见的 LAMP 或 LNMP 架构(以 Nginx + PHP-FPM + MySQL/PostgreSQL + Node.js 应用为例)。
一、环境准备
1. 更新系统
sudo yum update -y
2. 安装常用工具
sudo yum install -y epel-release vim wget curl net-tools htop
二、安装 Web 服务器(Nginx)
1. 安装 Nginx
sudo yum install -y nginx
2. 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
3. 防火墙放行 HTTP/HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
三、安装数据库(MySQL / MariaDB)
1. 安装 MariaDB(CentOS 默认源)
sudo yum install -y mariadb-server mariadb
2. 启动并启用
sudo systemctl start mariadb
sudo systemctl enable mariadb
3. 安全初始化
sudo mysql_secure_installation
按提示设置 root 密码、移除匿名用户等。
四、安装 PHP(用于网站)
1. 添加 EPEL 和 Remi 源(推荐)
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
注:CentOS 8 使用
remi-release-8,版本号需对应。
2. 启用 PHP 7.4 或 8.x 模块(以 8.1 为例)
sudo yum-config-manager --enable remi-php81
sudo yum install -y php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json php-opcache
3. 启动 PHP-FPM
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
五、配置 Nginx 托管静态网站或 PHP 网站
示例:配置 PHP 网站(如 WordPress)
1. 创建网站目录
sudo mkdir -p /var/www/html/mywebsite
sudo chown -R nginx:nginx /var/www/html/mywebsite
2. 创建 Nginx 虚拟主机配置
sudo vim /etc/nginx/conf.d/mywebsite.conf
内容示例:
server {
listen 80;
server_name www.mywebsite.com mywebsite.com;
root /var/www/html/mywebsite;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
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;
}
location ~ /.ht {
deny all;
}
}
3. 测试配置并重启 Nginx
sudo nginx -t
sudo systemctl reload nginx
六、部署应用服务(例如 Node.js 应用)
1. 安装 Node.js(使用 NodeSource 源)
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
2. 创建应用目录
sudo mkdir /var/www/nodeapp
cd /var/www/nodeapp
3. 初始化项目(示例)
npm init -y
npm install express
创建 app.js:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello from Node.js App!');
});
app.listen(port, '127.0.0.1', () => {
console.log(`App running at http://127.0.0.1:${port}`);
});
4. 使用 PM2 管理进程
sudo npm install -g pm2
pm2 start app.js --name "my-node-app"
pm2 startup
pm2 save
七、通过 Nginx 反向X_X访问 Node.js 应用
配置子域名或路径X_X
编辑 /etc/nginx/conf.d/nodeapp.conf:
server {
listen 80;
server_name api.mywebsite.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
测试并重载:
sudo nginx -t
sudo systemctl reload nginx
八、安全与优化建议
1. 使用 Let’s Encrypt 配置 HTTPS(可选)
sudo yum install -y certbot python3-certbot-nginx
sudo certbot --nginx -d mywebsite.com -d www.mywebsite.com
2. 设置 SELinux(如启用)
sudo setsebool -P httpd_can_network_connect 1
3. 日志监控
- Nginx 日志:
/var/log/nginx/access.log,error.log - PHP-FPM 日志:
/var/log/php-fpm.log - Node.js 日志:
pm2 logs
4. 定期备份
- 数据库备份:
mysqldump - 网站文件:
rsync或tar
九、总结:架构图
公网请求
↓
Nginx (80/443)
├──→ PHP 网站 → PHP-FPM (9000)
└──→ Node.js 应用 → 本地 3000 端口 (反向X_X)
↑
PM2 进程守护
数据库:MariaDB (3306)
通过以上步骤,你可以在一台 CentOS 服务器上同时运行多个服务:
✅ 静态/动态网站(PHP)
✅ 后端 API 或 Web 应用(Node.js)
✅ 数据库支持
✅ 安全、反向X_X、HTTPS 支持
如有更多需求(如 Docker 容器化、负载均衡),可进一步扩展。
PHPWP博客