apache虚拟主机安装配置

一、apache服务安装

1、依赖安装

[root@test10 opt]# yum install -y wget apr apr-devel apr-util apr-util-devel bzip2 gcc gcc-c++ pcre pcre-devel

2、apache软件下载

[root@wxjtest195 opt]# wget https://mirrors.cnnic.cn/apache/httpd/httpd-2.4.38.tar.bz2

3、解压并预编译

[root@test10 opt]# tar xf httpd-2.4.38.tar.bz2 
[root@test10 opt]# cd httpd-2.4.38
[root@wxjtest195 httpd-2.4.38]# ./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so
--prefix:指定安装路径
--enable-rewrite:启动rewrite规则
--enable-so:启动动态加载库

没有报错表示预编译成功

4、编译并安装

[root@test10 httpd-2.4.38]# make ;make install

没有报错表示安装成功

5、服务启动

[root@test10 httpd-2.4.38]# /usr/local/apache2/bin/apachectl start
[root@test10 httpd-2.4.38]# netstat -ntlp|grep 80
 tcp6       0      0 :::80                   :::*                    LISTEN      21504/httpd 

6、防火墙和selinux配置

[root@test10 ~]# firewall-cmd --permanent --add-service=http
[root@test10 ~]# firewall-cmd --reload
[root@test10 ~]# setenforce 0
[root@test10 ~]# sed -i 's/SELINUX=enforcinf/SELINUX=disabled/g' /etc/selinux/config 

7、测试

二、虚拟主机配置

基于一个端口创建多个域名,方法如下

1、修改配置文件httpd.conf,去掉注释,打开虚拟机

[root@test10 ~]# vim /usr/local/apache2/conf/httpd.conf

2、修改配置文件httpd-vhost.conf

[root@test10 ~]# cd /usr/local/apache2/conf/extra/
[root@test10 extra]# cp httpd-vhosts.conf httpd-vhosts.conf.20191030
[root@test10 extra]# vim httpd-vhosts.conf
<VirtualHost *:80>        //虚拟主机配置起始
     ServerAdmin 717192502@qq.com     //管理员邮箱
     DocumentRoot "/usr/local/apache2/htdocs/test1"   //虚拟主机发布目录
     ServerName test1.wxj.com    //虚拟主机完整域名
     ServerAlias test1.wxj.com
     ErrorLog "logs/test1.wxj.com-error_log" //错误日志路径与文件名
     CustomLog "logs/test1.wxj.com-access_log" common  //访问日志路径与文件名
</VirtualHost>         //虚拟主机配置结束

httpd-vhosts.conf参数详解





3、创建虚拟目录,服务重启

[root@test10 ~]#  cd /usr/local/apache2/htdocs
[root@test10 htdocs]# mkdir test1
[root@test10 htdocs]# chown daemon:daemon -R *
[root@test10 htdocs]# vim test1/index.html
aabbccdd
[root@test10 htdocs]# /usr/local/apache2/bin/apachectl restart

4、做好域名解析,或者写入hosts文件

至此,虚拟主机创建完成

发表评论

后才能评论