apache installation and service of virtual Web Host Configuration

1.apache Profile

Apache HTTP Server (referred to as Apache) is an Apache Software Foundation open source web server that can run on most computer operating systems, because of its multi-platform security and is widely used, is the most popular Web server software of One. It is fast, reliable, and can be extended through a simple API, the Perl / Python interpreter like compiled into the server.

Apachehttp server is using the world's number one Web server software. It can run on almost any computer platform widely used.

Here Insert Picture Description

2. Source install apache

rpm mode can not be customized using apache use the function, you need to use the source code installed

lab environment

一台cenots7主机,IP为192.168.1.11,配置好yum源

1) provides upload and unzip the package

Link: https://pan.baidu.com/s/12GrVLVy0_FDmN2NO1kKHuQ
extraction code: v4nf

Here Insert Picture Description
[Apache the root @ ~] # the unzip lamp.zip
[Apache the root @ ~] # CD LAMP / [the root @ Apache LAMP] # RM -rf Boost-5.7.19.tar.gz PHP-MySQL-5.6.36.tar.gz [@ apache the root LAMP] # LS the httpd-2.4.28.tar.gz apache as primary package, others are dependencies
有几个与本实验无关软件包,删除即可


Here Insert Picture Description

2) install compilation tools and development kits

[root@apache ~]# yum -y install make gcc gcc-c++ openssl openssl-devel expat-devel

3) install apache dependencies

安装顺序请严格按照我这里来,因为有依赖包也有依赖关系

1. First apr-1.5.2.tar.gz

[root@apache lamp]# tar zxvf apr-1.5.2.tar.gz
[root@apache lamp]# cd apr-1.5.2/
这里有个文件,必须更改参数,否则安装会报错
[root@apache apr-1.5.2]# vim configure
在29605行,可以直接在vim 使用 :29605 到此行
Here Insert Picture Description
[root@apache apr-1.5.2]# ./configure --prefix=/usr/local/apr
这里解释一下:--prefix=是安装位置
[root@apache apr-1.5.2]# echo $?

0

[root@apache apr-1.5.2]# make && make install
[root@apache apr-1.5.2]# echo $?

0
2. Then install apr-util-1.5.4.tar.gz

[root@apache apr-1.5.2]# cd /root/lamp/
[root@apache lamp]# tar -zxvf apr-util-1.5.4.tar.gz
[root@apache lamp]# cd apr-util-1.5.4/
[root@apache apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/bin/apr-1-config

--with就是指定刚刚上面安装的软件中的一个文件,所以要严格按照安装顺序
[apache, apr-util-1.5.4 to set a root @] # the echo $?

0

[root@apache apr-util-1.5.4]# make && make install
[root@apache apr-util-1.5.4]# echo $?

0
3. Installation apr-util-1.5.4.tar.gz

[root@apache lamp]# cd /root/lamp/
[root@apache lamp]# tar -zxvf pcre-8.41.tar.gz
[root@apache lamp]# cd pcre-8.41/
[root@apache pcre-8.41]# ./configure --prefix=/usr/local/pcre
[root@apache pcre-8.41]# make && make install
[root@apache pcre-8.41]# echo $?

0
4. Installation apache main package httpd-2.4.28.tar.gz

[root@apache pcre-8.41]# cd /root/lamp/
[root@apache lamp]# tar -zxvf httpd-2.4.38.tar.gz
[root@apache lamp]# cd httpd-2.4.38/
[root@apache httpd-2.4.38]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/ --libdir=/usr/lib64
Here Insert Picture Description
[root@apache httpd-2.4.38]# echo $?

0

Here to explain
-enable-SO: 启动动态加载模块,也就是apache可以动态加载模块而不需要从新编译
-enable-rewrite:支持网站地址重写,也就是网页跳转现在不须深究
支持ssl加密,也就是https方式
-enable-ssl: -libdir = / usr / lib64:关联库文件

然后下面三个意思是关联刚刚安装的依赖包
–with-apr=/usr/local/apr
–with-apr-util=/usr/local/apr-util
–with-pcre=/usr/local/pcre

[root@apache httpd-2.4.38]# make && make install
[root@apache httpd-2.4.38]# echo $?

0

2. apache configuration files and management services systemctl

1) explain profile

[root@apache httpd-2.4.38]# cd /usr/local/apache/
[root@apache apache]# ls

bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

bin:启动程序
build:一些构建信息
cgi-bin:开发人员使用
conf:配置文件位置
eror:错误信息
htdocs:网站根目录
include:文件包含目录,主配文件可以调用这里的一些文件
logs:日志文件存放位置
man:帮助文档
modules:模块

其余没写的作用不大,或现在用不到
[root@apache ~]# vim /usr/local/apache/conf/httpd.conf
除了ServerName 改一下,其他均不用改,这里只是介绍

Listen 80   #监听端口
ServerRoot "/usr/local/apache" #配置文件根目录,就是配置文件存放目录
ServerAdmin [email protected]  #管理员邮箱,这里不重要随意
ServerName apache:80  #这行原本是注释的,删除#号,最好写自己主机名,也可以随便写,不影响
DocumentRoot "/usr/local/apache/htdocs" #网站的根目录
DirectoryIndex index.html #默认首页打开的html文件名字,就是上面根目录中写的
ErrorLog "logs/error_log" #错误日志位置,这里是相对路径

Here then this is the page file, we can change
[root @ the Apache ~] # cd / usr / local / the Apache / htdocs /
[root @ the Apache htdocs] # echo "<h1>Index</h1>"> index.html

2) Copy the startup script and let systemctl can manage apache

[root@apache ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/
[root@apache ~]# chmod +x /etc/init.d/apachectl

下面是写一个apahce系统服务脚本,之后就可以使用systemctl管理apache
[root@apache ~]# touch /usr/lib/systemd/system/apache.service
[root@apache ~]# vim /usr/lib/systemd/system/apache.service
下面内容不必深入了解,跟着写就行

[Unit]
Description=apache  
After=network.target  
[Service]
Type=forking
ExecStart=/etc/init.d/apachectl start
ExecReload=/etc/init.d/apachectl restart
ExecStop=/etc/init.d/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@apache ~]# chmod 754 /usr/lib/systemd/system/apache.service
[root@apache ~]# ll -l /usr/lib/systemd/system/apache.service
-rwxr-xr-- 1 root root 238 2月 23 03:00 /usr/lib/systemd/system/apache.service
[root@apache ~]# systemctl start apache
[root@apache apache]# netstat -anpt | grep 80

tcp6       0      0 :::80                   :::*                    LISTEN      108963/httpd   

IP access through what should be our own to change the page [root @ the Apache the Apache] # systemctl STOP firewalld
如果不能访问,多半是没有关闭防火墙

Here Insert Picture Description

3. Configure Virtual Web Host

Build a virtual web of purpose:
正常情况下,一个apache服务器只能搭建一个web站点,造成了资源的浪费 如果想在一个apache服务器中搭建多个web站点,可以使用虚拟主机

首先先打开apache服务器的虚拟主机功能
[root@apache ~]# vim /usr/local/apache/conf/httpd.conf
Here Insert Picture Description
[root@apache ~]# systemctl restart apache

1) virtual Web host based on IP address:

不同域名,不同IP,相同端口

1 to add a temporary IP (NIC will not restart), as a test

[root@apache ~]# ifconfig ens33:0 192.168.1.125
[root@apache ~]# ifconfig
Here Insert Picture Description

2. Create a virtual host site directory

[root@apache ~]# mkdir /usr/local/apache/htdocs/taobao
[root@apache ~]# mkdir /usr/local/apache/htdocs/tianmao
[root@apache ~]# echo "<h1>this is taobao</h1>" > /usr/local/apache/htdocs/taobao/index.html
[root@apache ~]# echo "<h1>this is tianmao</h1>" > /usr/local/apache/htdocs/tianmao/index.html
Here Insert Picture Description

3. Virtual Host Configuration File

[root@apache ~]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf
滑倒下面有两个例子,修改为我这里,或者把原来的删了复制我这里的

<VirtualHost 192.168.1.11:80> #写访问时对应的IP,这是我本机IP
    ServerAdmin root@ajbn #这里随意
    DocumentRoot "/usr/local/apache/htdocs/taobao/" #网站根目录,上面创建的
    ServerName www.taobao.com  #这里名字写这个就行,也可以写别的,随意
    ErrorLog "logs/www.taobao.com-error_log" #错误日志
    CustomLog "logs/www.taobao.com-access_log" common #日志
</VirtualHost>

<VirtualHost 192.168.1.125:80> #这里应该写另一个IP
    ServerAdmin root@ajbn
    DocumentRoot "/usr/local/apache/htdocs/tianmao/" #网站根目录不同
    ServerName www.tianmao.com
    ErrorLog "logs/www.tianmao.com-error_log"
    CustomLog "logs/www.tianmao.com-access_log" common
</VirtualHost>

If the view is not convenient, the following is the picture
Here Insert Picture Description
[root @ the Apache ~] # systemctl restart the Apache

4. Client Access test

http://192.168.1.11/
Here Insert Picture Description
another page, http://192.168.1.125/
Here Insert Picture Description
当然实际情况肯定是IP访问对应的域名,这里不是本章要点,不去演示了

2) based on the domain name (host headers) virtual Web host:

相同IP,相同端口号,不同域名

1. First of all, or change the configuration file

[root@apache ~]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf
只需更改2个VirtualHost这行,改为对应的域名,其他不用动

VirtualHost这里的域名不可以随意写,一会要和客户端DNS那里一样,这里这样改就行

<VirtualHost www.taobao.com:80> #更改
    ServerAdmin root@ajbn
    DocumentRoot "/usr/local/apache/htdocs/taobao/"
    ServerName www.taobao.com
    ErrorLog "logs/www.taobao.com-error_log"
    CustomLog "logs/www.taobao.com-access_log" common
</VirtualHost>

<VirtualHost www.tianmao.com:80> #更改
    ServerAdmin root@ajbn
    DocumentRoot "/usr/local/apache/htdocs/tianmao/"
    ServerName www.tianmao.com
    ErrorLog "logs/www.tianmao.com-error_log"
    CustomLog "logs/www.tianmao.com-access_log" common
</VirtualHost>
2. Change at the local hosts file

因为本例,没有配置dns服务器,所以需要手动配置解析,以便客户端过来访问
[root@apache ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.11 www.taobao.com
192.168.1.11 www.tianmao.com

[root@apache ~]# systemctl restart apache
修改完hosts文件也一定要重启apache服务

2. Client Configuration hosts file

Enter to C:\Windows\System32\drivers\etcopen the hosts file, make the following changes
Here Insert Picture Description

3. Client Access test

注意我这个www.taobao.com和互联网上的域名冲突了,有的浏览器会出问题
IE browser to access the test is recommended to use the system, there is no problem, as there are problems, and more for a few browser

http://www.taobao.com/
Here Insert Picture Description
http://www.tianmao.com/
Here Insert Picture Description
here to explain the principle: Why can distinguish the web site through the domain name
因为通过浏览器访问域名的时候,这个域名就是一个主机头,请求到web服务器时, 客户端浏览器会带着这个主机头,去请求,这个主机头对应,虚拟主机配置文件 </VirtualHost>那里写的那个域名

3) based Virtual Host port number:

相同域名,相同IP,不同端口号

1. Change profile

[root@apache ~]# cd /usr/local/apache/conf/
[root@apache conf]# vim httpd.conf
在原本的监听端口下面,添加一个8080端口
Here Insert Picture Description
[root@apache conf]# vim extra/httpd-vhosts.conf
还是两个<VirtualHost>字段需要更改,其他不用动

<VirtualHost *:8080>  #更改为*:8080
    ServerAdmin root@ajbn
    DocumentRoot "/usr/local/apache/htdocs/taobao/"
    ServerName www.taobao.com
    ErrorLog "logs/www.taobao.com-error_log"
    CustomLog "logs/www.taobao.com-access_log" common
</VirtualHost>

<VirtualHost *:80> #更为*:80
    ServerAdmin root@ajbn
    DocumentRoot "/usr/local/apache/htdocs/tianmao/"
    ServerName www.tianmao.com
    ErrorLog "logs/www.tianmao.com-error_log"
    CustomLog "logs/www.tianmao.com-access_log" common
</VirtualHost>

[root@apache conf]# systemctl restart apache

2. Client Authentication

Access www.tiaobao.com and www.tiaomao.com both sites can visit here later this

首先不加端口,默认时80端口
http://www.tianmao.com/ http://www.tianmao.com:8080/ experiment is complete!
Here Insert Picture Description
加端口8080,可以看到是另一个页面

Here Insert Picture Description

Published 54 original articles · won praise 57 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_45308292/article/details/104446120