Apache service (based on IP address, host name, port number)

Apache installation service program

 

Note apache package service program name is called httpd , thus directly execute yum install apache is wrong.

[root@liuxuanke-hbza ~]# yum install httpd*

Create a Yum repository configuration file using Vim Editor:

[Root @ liuxuanke-hbza ~] # vim /etc/yum.repos.d/rhel7.repo

[Rhel7]

name=rhel7

baseurl = file: /// media / cdrom

enabled=1

gpgcheck=0

Running Apache service program and is set to boot

Start Apache service program:

[root@liuxuanke-hbza ~]# systemctl start httpd

Is set to boot from the start:

[root@liuxuanke-hbza ~]# systemctl enable httpd

Based on IP address

A server with multiple IP addresses, display different web pages when the user accesses different IP address.

1, nmtui command to add multiple IP addresses (192.168.10.100/110/120) for the network card:

After restarting the network card device using the ping command to check the correct ( this is very important, be sure to test good and then the next step ! )

2, respectively, to create a site directory data

In the Create a data directory are three sites under / home / wwwroot directory:

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/100

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/110

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/120

Home files are written in the data directory three sites, the content for the site's IP address:

[root@liuxuanke-hbza ~]# echo "IP:192.168.10.100" > /home/wwwroot/100/index.html

[root@liuxuanke-hbza ~]# echo "IP:192.168.10.110" > /home/wwwroot/110/index.html

[root@liuxuanke-hbza ~]# echo "IP:192.168.10.120" > /home/wwwroot/120/index.html

3, in the configuration file ( /etc/httpd/conf/httpd.conf virtual host based on the host name of the description) in

 

 4, modify the data directory website SELinux security context

 

 Immediate resumption of SELinux security context:

[root@liuxuanke-hbza ~]# restorecon -Rv /home/wwwroot

Restart Service

[root@liuxuanke-hbza ~]# systemctl restart httpd

Were about access, validation results

 

 

After completing this lab restore the virtual machine performing the following experiment

 

Based on a host name

 When the server can not be assigned to a separate site for each IP address, you can try the service for Apache automatically identify the origin host name or domain name and then jump to the specified site.

1、配置网卡IP地址与hosts文件

hosts文件作用是定义IP地址与主机名的映射关系,即强制将某个主机名地址解析到指定的IP地址

[root@liuxuanke-hbza ~]# vim /etc/hosts

//每行只能写一条,格式为IP地址+空格+主机名(域名)。

192.168.10.100 www.centos.com

192.168.10.100 bbs.centos.com

192.168.10.100 tech.centos.com

2、分别创建网站数据目录

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/www

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/bbs

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/tech

分别在网站目录中写入不同的首页文件:

[root@liuxuanke-hbza ~]# echo "www.centos.com" > /home/wwwroot/www/index.html

[root@liuxuanke-hbza ~]# echo "bbs.centos.com" > /home/wwwroot/bbs/index.html

[root@liuxuanke-hbza ~]# echo "tech.centos.com" > /home/wwwroot/tech/index.html

3、在配置文件中描述基于主机名称的虚拟主机

<VirtualHost 192.168.10.100>

DocumentRoot "/home/wwwroot/www"

ServerName "www.centos.com"

<Directory "/home/wwwroot/www">

AllowOverride None

Require all granted

</directory>

</VirtualHost>

 

<VirtualHost 192.168.10.100>

DocumentRoot "/home/wwwroot/bbs"

ServerName "bbs.centos.com"

<Directory "/home/wwwroot/bbs">

AllowOverride None

Require all granted

</Directory>

</VirtualHost>

 

<VirtualHost 192.168.10.100>

DocumentRoot "/home/wwwroot/tech"

ServerName "tech.centos.com"

<Directory "/home/wwwroot/tech">

AllowOverride None

Require all granted

</directory>

</VirtualHost>

 

 

重启服务

[root@liuxuanke-hbza ~]# systemctl restart httpd

设置为开机自启动:

[root@liuxuanke-hbza ~]# systemctl enable httpd

5、分别访问网站验证结果

 

请注意还原虚拟机快照再进行下一个实验,否则可能导致配置文件冲突而报错。

 

基于端口号

我们可以让服务器开启多个服务端口后,然后让用户能够通过访问服务器的指定端口来找到想要的网站。

1、配置服务器的IP地址

2、分别创建网站数据目录

分别创建端口为6111,6222的网站数据目录:

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/6111

[root@liuxuanke-hbza ~]# mkdir -p /home/wwwroot/6222

分别在网站数据目录中写入不同内容的主页文件:

[root@liuxuanke-hbza ~]# echo "port:6111" > /home/wwwroot/6111/index.html

[root@liuxuanke-hbza ~]# echo "port:6222" > /home/wwwroot/6222/index.html

 

 

 

 

 3、在配置文件中描述基于端口号的虚拟主机

编辑主配置文件(/etc/httpd/conf/httpd.conf),找到约在42行的Listen 80,并在下面追加:

Listen 6111
Listen 6222

然后在主配置文件的末尾按下面格式定义虚拟主机信息:

<VirtualHost 192.168.10.10:6111>
DocumentRoot ''/home/wwwroot/6111"
ServerName www.linuxprobe.com
<Directory "/home/wwwroot/6111''>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.10.10:6222>
DocumentRoot "/home/wwwroot/6222"
ServerName bbs.linuxprobe.com
<Directory "/home/wwwroot/6222">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

修改完后,记得重启服务

出现了错误这是因为SELinux服务检测到6111与6222端口原本并不属于Apache服务端口,但现在却被以Apache的名义监听了

4、修改网站数据目录的SELinux安全上下文并允许端口监听

让新的SElinux安全上下文立即生效:

[root@liuxuanke-hbza ~]# restorecon -Rv /home/wwwroot/

使用semanage命令搜索在SELinux系统中有关http服务的端口号:

[root@liuxuanke-hbza ~]# semanage port -l| grep http

默认的端口号中没有6111 、6222,所以需要添加

[root@liuxuanke-hbza ~]# semanage port -a -t http_port_t -p tcp 6111

[root@liuxuanke-hbza ~]# semanage port -a -t http_port_t -p tcp 6222

再来看下SELinux的端口规则(已经添加成功了):

 再尝试启动服务程序就没有问题了 systemctl restart httpd

5、分别访问网站查看验证结果

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/heiwa-0924/p/11816932.html