阅读量:2
Ubuntu虚拟机安装与配置Web服务器(以Apache/Nginx为例)
一、前期准备
- 安装Ubuntu虚拟机
使用VMware Workstation、VirtualBox等虚拟机软件,下载Ubuntu Server或Desktop镜像(建议选择LTS版本,如22.04/24.04),创建虚拟机时分配至少2核CPU、4GB内存、20GB硬盘空间,网络模式选择NAT(便于虚拟机访问外网)或桥接(虚拟机与主机同局域网)。安装过程中设置root密码和普通用户账户(需加入sudo组)。 - 更新系统软件包
安装完成后,打开终端(Ctrl+Alt+T),运行以下命令更新软件包列表并升级已安装的软件:sudo apt update && sudo apt upgrade -y
二、安装Web服务器(Apache/Nginx)
1. 安装Apache
Apache是Ubuntu默认的Web服务器,安装步骤如下:
sudo apt install apache2 -y
安装完成后,Apache会自动启动,可通过以下命令验证状态:
sudo systemctl status apache2
若显示“active (running)”,则表示服务已启动。
2. 安装Nginx
Nginx以高性能著称,适合高并发场景,安装命令如下:
sudo apt install nginx -y
同样,安装完成后启动Nginx并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证状态:
sudo systemctl status nginx
```。
### **三、验证Web服务器运行**
安装完成后,在浏览器中输入虚拟机的**IP地址**(可通过`ip addr show`命令查看,如`192.168.92.129`),若看到以下页面则说明安装成功:
- Apache:显示“Apache2 Ubuntu Default Page”;
- Nginx:显示“Welcome to nginx!”。
### **四、配置Web服务器**
#### **1. Apache配置(虚拟主机)**
若需绑定域名或创建多个站点,需配置虚拟主机:
- 创建配置文件(以`example.com`为例):
```bash
sudo nano /etc/apache2/sites-available/example.com.conf
- 添加以下内容(替换
example.com为你的域名,/var/www/example.com为网站根目录)::80 > ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.comOptions Indexes FollowSymLinks AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/example.com_error.log CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined - 启用虚拟主机并禁用默认配置:
sudo a2ensite example.com.conf sudo a2dissite 000-default.conf - 重启Apache使配置生效:
sudo systemctl reload apache2 ```。
2. Nginx配置(服务器块)
Nginx使用“服务器块”(类似Apache的虚拟主机)处理不同域名:
- 创建服务器块文件:
sudo nano /etc/nginx/sites-available/example.com - 添加以下内容(替换
example.com和网站根目录):server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html index.htm; location / { try_files $uri $uri/ =404; } } - 创建符号链接启用服务器块:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ - 测试配置语法并重新加载Nginx:
sudo nginx -t # 检查语法 sudo systemctl reload nginx # 重新加载配置 ```。
五、部署网站文件
默认网站根目录:
- Apache:
/var/www/html; - Nginx:
/var/www/html(或自定义的/var/www/example.com)。
将网站文件(如index.html)上传至根目录,可通过以下命令创建测试页面:
sudo nano /var/www/html/index.html
输入以下内容并保存:
html>
<html>
<head>
<title>My Web Servertitle>
head>
<body>
<h1>Welcome to My Web Server!h1>
body>
html>
刷新浏览器即可看到自定义页面。
六、安全配置
- 配置防火墙
使用UFW(Uncomplicated Firewall)允许HTTP(80)和HTTPS(443)流量:sudo apt install ufw -y sudo ufw allow 'Apache Full' # Apache # 或 sudo ufw allow 'Nginx Full' # Nginx sudo ufw enable # 启用防火墙 sudo ufw status # 查看状态 ```。 - 配置SSL(可选)
为网站配置HTTPS加密,使用Let’s Encrypt免费获取SSL证书:按提示完成配置,证书会自动续期。sudo apt install certbot python3-certbot-apache # Apache # 或 sudo apt install certbot python3-certbot-nginx # Nginx sudo certbot --apache -d example.com -d www.example.com # Apache # 或 sudo certbot --nginx -d example.com -d www.example.com # Nginx
常见问题排查
- 无法访问网站:检查Web服务是否启动(
systemctl status apache2/nginx)、防火墙是否拦截(ufw status)、IP地址是否正确(ip addr show); - 权限问题:确保网站根目录的所属用户为
www-data(Apache)或www-data(Nginx),可通过sudo chown -R www-data:www-data /var/www/html修改。
以上就是关于“Ubuntu虚拟机如何安装和配置Web服务器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm