在京东云轻量应用服务器(Lightweight Application Server)上安装 Nginx 或 Apache,通常推荐使用系统自带的包管理器进行安装,这样更稳定且易于维护。以下是基于 CentOS/Rocky Linux 和 Ubuntu/Debian 两大主流系统的详细步骤:
✅ 一、通用准备步骤
-
登录服务器
- 通过 SSH 工具(如 PuTTY、Xshell、终端)登录您的轻量主机。
- 使用 root 用户或具有 sudo 权限的用户账号。
-
更新系统软件包
# CentOS / Rocky / AlmaLinux sudo yum update -y # Ubuntu / Debian sudo apt update && sudo apt upgrade -y -
确认防火墙状态
-
京东云轻量主机默认可能开启防火墙,需放行 HTTP(80) 和 HTTPS(443) 端口:
# CentOS (firewalld) sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload # Ubuntu (ufw) sudo ufw allow http sudo ufw allow https sudo ufw enable
-
🌐 二、安装 Nginx
▶️ CentOS / Rocky Linux
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
▶️ Ubuntu / Debian
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
✅ 验证安装:
- 浏览器访问
http://<你的公网IP>,应看到 "Welcome to nginx!" 页面。 - 检查监听端口:
netstat -tlnp | grep nginx或ss -tlnp | grep nginx
🌐 三、安装 Apache
▶️ CentOS / Rocky Linux
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
⚠️ 注意:Apache 在 CentOS 中服务名是
httpd,不是apache2。
▶️ Ubuntu / Debian
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
✅ 验证安装:
- 浏览器访问
http://<你的公网IP>,应看到 Apache 默认测试页。 - 检查监听端口:
netstat -tlnp | grep apache或ss -tlnp | grep apache
🔧 四、配置 Web 根目录与自定义网站
Nginx 默认站点目录:
/usr/share/nginx/html- 主配置文件:
/etc/nginx/nginx.conf - 虚拟主机配置:
/etc/nginx/conf.d/
Apache 默认站点目录:
- CentOS:
/var/www/html - Ubuntu:
/var/www/html(部分版本为/var/www/apache2-default) - 主配置文件:
/etc/httpd/conf/httpd.conf(CentOS)或/etc/apache2/apache2.conf(Ubuntu)
📌 示例:创建自定义网站
# 创建网站目录
sudo mkdir -p /var/www/mywebsite
# 写入测试页面
echo "<h1>Hello from My Website!</h1>" | sudo tee /var/www/mywebsite/index.html
# 设置权限
sudo chown -R www-data:www-data /var/www/mywebsite # Ubuntu
# 或
sudo chown -R apache:apache /var/www/mywebsite # CentOS
# 修改 Nginx 配置(编辑 /etc/nginx/sites-available/default 或新建 conf.d/mywebsite.conf)
server {
listen 80;
server_name example.com; # 替换为你的域名或IP
root /var/www/mywebsite;
index index.html;
}
然后重启服务:
sudo systemctl restart nginx
# 或
sudo systemctl restart apache2
🛡️ 五、安全建议
- 禁用 root 远程登录(推荐改用普通用户 + sudo)
- 安装 Fail2Ban 防止暴力破解
- 启用 HTTPS(Let’s Encrypt)
sudo apt install certbot python3-certbot-nginx # Ubuntu+Nginx sudo certbot --nginx -d yourdomain.com - 定期更新系统和安全补丁
❓ 常见问题
-
无法访问网页?
- 检查京东云控制台是否开放了 80/443 端口(安全组规则)。
- 检查本地防火墙和 SELinux(CentOS 默认开启,可能阻止访问):
setenforce 0 # 临时关闭测试(生产环境请配置策略而非关闭)
-
服务启动失败?
- 查看日志:
journalctl -u nginx -f # 或 tail -f /var/log/nginx/error.log
- 查看日志:
如需图形化管理(如宝塔面板),也可在轻量主机上一键部署,但原生安装更轻量、可控性更强。
需要我为你提供自动化脚本或 Docker 部署方案吗?
CLOUD技术博