安装apache
$ yum install httpd -y
启动apache
$ systemctl start httpd.service
查看是否开启成功
[root@centos7-1 ~] $ ps -ef|grep httpd
root 1739 1 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1740 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1741 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1742 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1743 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1744 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 1749 1112 0 18:37 pts/0 00:00:00 grep --color=auto httpd
查看apache端口
$ netstat -lntup|grep httpd
修改hosts解析
$ vi /etc/hosts
改成如下内容
192.168.56.101 centos7.com www.centos7.com bbs.centos7.com blog.centos7.com
测试访问
$ curl www.centos7-1.com
配置apache
备份文件
$ cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.back
配置httpd文件
因为在apache2.4中变化挺大,和nginx一样,可以自定义.conf文件。
在主配置文件中启用虚拟主机
$ mkdir /etc/httpd/vhost.d/
$ echo "include vhost.d/*.conf"
$ tail -1 /etc/httpd/conf/httpd.conf
配置多站点目录
$ vi /etc/httpd/vhost.d/name.conf
写入下面的内容
<VirtualHost *:80>
ServerAdmin admin@amsilence.com
DocumentRoot "/var/html/www"
ServerName www.centos7.com
ErrorLog "/var/httpd/logs/www-error_log"
CustomLog "/var/httpd/logs/www-access_log" common
</VirtualHost>
<Directory /var/html/www/>
Require all granted
</Directory>
<VirtualHost *:80>
ServerAdmin admin@amsilence.com
DocumentRoot "/var/html/bbs"
ServerName bbs.centos7.com
ErrorLog "/var/httpd/logs/bbs-error_log"
CustomLog "/var/httpd/logs/bbs-access_log" common
</VirtualHost>
<Directory /var/html/bbs/>
Require all granted
</Directory>
<VirtualHost *:80>
ServerAdmin admin@amsilence.com
DocumentRoot "/var/html/blog"
ServerName blog.centos7.com
ErrorLog "/var/httpd/logs/blog-error_log"
CustomLog "/var/httpd/logs/blog-access_log" common
</VirtualHost>
<Directory /var/html/blog/>
Require all granted
</Directory>
重启Apache服务
$ systemctl restart httpd.service
测试web访问
[root@centos7-1 httpd] $ for name in www bbs blog;do curl $name.centos7.com;done;
http://www.centos7.com
http://bbs.centos7.com
http://blog.centos7.com