同一Linux VPS上通过独立域名部署Node.js与Apache应用
没问题,这个需求用Nginx反向代理就能完美解决,我帮很多人配置过类似的场景,跟着下面的步骤一步步来就行:
第一步:安装并启用Nginx
如果你的服务器还没装Nginx,根据发行版执行对应的安装命令:
- Debian/Ubuntu系:
sudo apt update && sudo apt install nginx - RHEL/CentOS系:
sudo dnf install nginx
安装完成后,启动并设置开机自启:
sudo systemctl enable --now nginx
第二步:确认两个应用的运行状态
- Node.js聊天应用:假设它默认监听
localhost:3000,先通过curl http://localhost:3000验证是否能正常返回内容,确保服务在运行。 - PHP应用:如果用的是Apache,为了避免和Nginx的80端口冲突,修改Apache的端口配置(比如
/etc/apache2/ports.conf),把Listen 80改成Listen 8080,然后重启Apache:sudo systemctl restart apache2;如果是Nginx+php-fpm组合,确保php-fpm服务处于运行状态。
第三步:配置Nginx虚拟主机(独立域名对应独立应用)
在Nginx的配置目录/etc/nginx/sites-available/下,为两个域名分别创建配置文件:
1. Node.js应用配置(nodeappexample.com)
创建文件/etc/nginx/sites-available/nodeappexample.com,内容如下:
server { listen 80; server_name nodeappexample.com www.nodeappexample.com; # 自动将HTTP请求重定向到HTTPS return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name nodeappexample.com www.nodeappexample.com; # SSL证书后续会用certbot自动生成,这里先预留路径 ssl_certificate /etc/letsencrypt/live/nodeappexample.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/nodeappexample.com/privkey.pem; # 反向代理到Node.js应用的端口 location / { proxy_pass http://localhost:3000; 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; } }
2. PHP应用配置(apacheappexample.com)
如果你的PHP应用跑在Apache的8080端口,创建文件/etc/nginx/sites-available/apacheappexample.com:
server { listen 80; server_name apacheappexample.com www.apacheappexample.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name apacheappexample.com www.apacheappexample.com; ssl_certificate /etc/letsencrypt/live/apacheappexample.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/apacheappexample.com/privkey.pem; # 反向代理到Apache的8080端口 location / { proxy_pass http://localhost:8080; 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; } }
如果是Nginx直接处理PHP(用php-fpm),把PHP应用的配置改成下面这样(假设PHP文件存放在/var/www/apacheappexample.com/public):
server { listen 80; server_name apacheappexample.com www.apacheappexample.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name apacheappexample.com www.apacheappexample.com; ssl_certificate /etc/letsencrypt/live/apacheappexample.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/apacheappexample.com/privkey.pem; root /var/www/apacheappexample.com/public; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整路径 } }
第四步:启用配置并验证Nginx语法
- 创建软链接将配置文件加入启用列表:
sudo ln -s /etc/nginx/sites-available/nodeappexample.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/apacheappexample.com /etc/nginx/sites-enabled/ - 测试配置是否有语法错误:
如果输出sudo nginx -ttest is successful就没问题,否则根据提示修正配置。 - 重启Nginx生效:
sudo systemctl restart nginx
第五步:配置免费SSL证书(Let's Encrypt)
安装certbot工具自动生成SSL证书:
- Debian/Ubuntu系:
sudo apt install certbot python3-certbot-nginx - RHEL/CentOS系:
sudo dnf install certbot python3-certbot-nginx
分别为两个域名申请证书:
sudo certbot --nginx -d nodeappexample.com -d www.nodeappexample.com sudo certbot --nginx -d apacheappexample.com -d www.apacheappexample.com
按照提示选择“重定向HTTP到HTTPS”即可,certbot会自动修改Nginx配置完成SSL设置。
最后验证
现在访问https://nodeappexample.com就能看到你的Node.js聊天应用,访问https://apacheappexample.com则会打开PHP应用。如果遇到问题,可以查看Nginx日志/var/log/nginx/error.log或者应用自身的日志排查。
内容的提问来源于stack exchange,提问作者Ghadeer R. Majeed
相关产品推荐
相关产品推荐

