在CentOS上搭建Apache虚拟主机的步骤如下:
cat /etc/redhat-release
看到"CentOS Linux release 7.x"即为正确版本。sudo yum update -y && sudo yum upgrade -y
sudo yum install httpd -y
其中 yum
是 CentOS 的软件包管理器,自动解决依赖关系。/etc/httpd/conf/httpd.conf
。vim
进行编辑:sudo vim /etc/httpd/conf/httpd.conf
Listen 80
ServerName your_domain:80
DirectoryIndex index.html
ESC
后输入 :wq
。sudo systemctl start httpd
sudo systemctl enable httpd
systemctl status httpd
看到 “active (running)” 标识表示 httpd
运行成功。sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
firewall-cmd --list-all
curl 127.0.0.1
/var/www/html/
。echo "<h1>Hello World!</h1>" | sudo tee /var/www/html/index.html
www.company.com
→ 文件存储在 /var/www/company
blog.company.com
→ 文件存储在 /var/www/blog
sudo mkdir -p /var/www/{company,blog}
sudo chown -R apache:apache /var/www/
sudo vim /etc/httpd/conf.d/vhost.conf
输入以下内容:<VirtualHost *:80>
ServerName www.company.com
DocumentRoot "/var/www/company"
ErrorLog "/var/log/httpd/company_error.log"
CustomLog "/var/log/httpd/company_access.log" combined
<Directory "/var/www/company">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName blog.company.com
DocumentRoot "/var/www/blog"
ErrorLog "/var/log/httpd/blog_error.log"
CustomLog "/var/log/httpd/blog_access.log" combined
<Directory "/var/www/blog">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart httpd
systemctl status httpd
netstat -tunlp | grep 80
tail -f /var/log/httpd/error_log
sudo setenforce 0 # 临时关闭