在CentOS Stream 8上搭建Web服务器,你可以选择多种方式,包括使用Apache、Nginx等流行的Web服务器软件。以下是使用Apache和Nginx搭建Web服务器的基本步骤:
安装Apache: 打开终端,运行以下命令来安装Apache:
sudo dnf install httpd
启动Apache服务: 安装完成后,启动Apache服务并设置开机自启:
sudo systemctl start httpd
sudo systemctl enable httpd
配置防火墙: 如果你启用了防火墙,需要允许HTTP(端口80)和HTTPS(端口443)流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
测试Apache: 在浏览器中输入服务器的IP地址或域名,如果看到Apache的默认页面,说明安装成功。
安装Nginx: 打开终端,运行以下命令来安装Nginx:
sudo dnf install nginx
启动Nginx服务: 安装完成后,启动Nginx服务并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
配置防火墙: 同样,如果你启用了防火墙,需要允许HTTP(端口80)和HTTPS(端口443)流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
测试Nginx: 在浏览器中输入服务器的IP地址或域名,如果看到Nginx的默认页面,说明安装成功。
无论是Apache还是Nginx,你都可以配置虚拟主机来托管多个网站。
创建一个新的配置文件:
sudo vi /etc/httpd/conf.d/yourdomain.conf
添加虚拟主机配置:
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain.com/html
<Directory /var/www/yourdomain.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
重启Apache服务:
sudo systemctl restart httpd
创建一个新的配置文件:
sudo vi /etc/nginx/conf.d/yourdomain.conf
添加虚拟主机配置:
server {
listen 80;
server_name yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
重启Nginx服务:
sudo systemctl restart nginx
通过以上步骤,你可以在CentOS Stream 8上成功搭建一个基本的Web服务器,并配置虚拟主机来托管多个网站。