在CentOS虛擬機上搭建Web服務器,通常會選擇使用Apache或Nginx作為Web服務器軟件。以下是使用這兩種軟件的基本步驟:
更新系統
sudo yum update -y
安裝Apache
sudo yum install httpd -y
啟動Apache服務
sudo systemctl start httpd
設置Apache開機自啟
sudo systemctl enable httpd
配置防火墻 如果你使用的是firewalld,需要允許HTTP和HTTPS流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
訪問Web服務器 打開瀏覽器,訪問服務器的IP地址或域名,應該能看到Apache的默認頁面。
更新系統
sudo yum update -y
安裝Nginx
sudo yum install nginx -y
啟動Nginx服務
sudo systemctl start nginx
設置Nginx開機自啟
sudo systemctl enable nginx
配置防火墻 同樣,如果你使用的是firewalld,需要允許HTTP和HTTPS流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
訪問Web服務器 打開瀏覽器,訪問服務器的IP地址或域名,應該能看到Nginx的默認頁面。
無論是Apache還是Nginx,都可以配置多個虛擬主機來托管不同的網站。
編輯Apache配置文件:
sudo vi /etc/httpd/conf/httpd.conf
添加虛擬主機配置:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
DocumentRoot "/var/www/html/yourdomain"
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ErrorLog "/var/log/httpd/yourdomain-error.log"
CustomLog "/var/log/httpd/yourdomain-access.log" common
</VirtualHost>
重啟Apache服務:
sudo systemctl restart httpd
編輯Nginx配置文件:
sudo vi /etc/nginx/nginx.conf
添加虛擬主機配置:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html/yourdomain;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_log /var/log/nginx/yourdomain-error.log;
access_log /var/log/nginx/yourdomain-access.log;
}
重啟Nginx服務:
sudo systemctl restart nginx
通過以上步驟,你可以在CentOS虛擬機上成功搭建并配置一個Web服務器。