Linux——搭建Apache(httpd)服务器
<h1 id="一什么是apache">一、什么是Apache?</h1><p>Apache(或httpd)服务,是Internet上使用最多的Web服务器技术之一,通俗来讲就是一个用于搭建网站的服务。</p>
<p>有两个版本:</p>
<ul>
<li>http:超文本传输协议,通过线路以明文形式发送,默认使用80端口/TCP</li>
<li>https:经TLS/SSL安全加密的超文本传输协议,默认使用443端口/TCP</li>
</ul>
<h1 id="二apache的配置文件">二、Apache的配置文件</h1>
<h2 id="1配置文件的位置">1、配置文件的位置</h2>
<table>
<thead>
<tr>
<th>配置文件</th>
<th>存放位置</th>
</tr>
</thead>
<tbody>
<tr>
<td>服务目录</td>
<td>/etc/httpd</td>
</tr>
<tr>
<td>主配置文件</td>
<td>/etc/httpd/conf/httpd.conf</td>
</tr>
<tr>
<td>虚拟主机的配置文件目录</td>
<td>/etc/httpd/conf.d</td>
</tr>
<tr>
<td>基于用户的配置文件</td>
<td>/etc/httpd/conf.d/userdir.conf</td>
</tr>
<tr>
<td>日志文件目录</td>
<td>/etc/httpd/logs</td>
</tr>
<tr>
<td>默认的网站数据目录</td>
<td>/var/www/html</td>
</tr>
</tbody>
</table>
<h2 id="2主配置文件的重要参数">2、主配置文件的重要参数</h2>
<p>主配置文件:/etc/httpd/conf/httpd.conf</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>作用</th>
<th>参数</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>ServerRoot</td>
<td>服务目录</td>
<td>ServerName</td>
<td>网站服务器的域名</td>
</tr>
<tr>
<td>Listen</td>
<td>监听的IP地址与端口号</td>
<td>DocumentRoot</td>
<td>默认网站数据目录</td>
</tr>
<tr>
<td>User</td>
<td>运行服务的用户</td>
<td>Directory</td>
<td>文件目录的权限</td>
</tr>
<tr>
<td>Group</td>
<td>运行服务的用户组</td>
<td>DirectoryIndex</td>
<td>默认的索引页页面</td>
</tr>
<tr>
<td>ServerAdmin</td>
<td>管理员邮箱</td>
<td>ErrorLog</td>
<td>错误日志文件</td>
</tr>
</tbody>
</table>
<h1 id="三如何搭建apache服务器">三、如何搭建Apache服务器</h1>
<h2 id="准备主机名网络yum源">准备:主机名、网络、yum源</h2>
<pre><code># 1、更改主机名:
# hostnamectl set-hostname server
# bash
#
# 2、配置网络
# (1)虚拟交换机配置为192.168.100.0网段,网络适配器选择仅主机模式;
# (2)编辑网络配置文件:
# cd /etc/sysconfig/network-scripts/
# vim ifcfg-ens33
#需要修改的参数为:
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.10
PREFIX=24
# (3)重启网络服务:
# systemctl restart network
# 3、配置yum源
# (1)先进入虚拟机设置,把系统镜像连接到虚拟机的光驱上;
# (2)挂载光驱里的镜像:
# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only
# (3)修改yum源配置文件:
# cd /etc/yum.repos.d/
# ls
# vim local.repo
name=rhel
baseurl=file:///media
enabled=1
gpgcheck=0
# (4)清空yum源缓存并检索yum源
# yum clean all
# yum repolist
</code></pre>
<h2 id="1搭建简单的httpd服务">1、搭建简单的httpd服务</h2>
<p><strong>Server端</strong>配置:</p>
<pre><code class="language-bash"># 1、安装httpd服务
# yum -y install httpd
# 2、配置防火墙
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# 3、开启服务
# systemctl restart httpd
# systemctl enable httpd
</code></pre>
<p><strong>Client端</strong>访问:</p>
<pre><code class="language-bash"># 有图形化
# firefox http://192.168.100.10
# 没有图形化
# curl http://192.168.100.10
</code></pre>
<h2 id="2搭建基于用户的个人网站">2、搭建基于用户的个人网站</h2>
<p><strong>Server端</strong>配置:</p>
<pre><code class="language-bash"># 首先已经安装了httpd服务
# 1、新建用户(网站基于该用户)
# useradd wzg
# 2、创建个人的网页文件
# mkdir /home/wzg/public_html
# cd /home/wzg/public_html
# echo "hello,欢迎访问王智刚的个人网站">>index.html
# 3、修改用户网页文件的访问权限
# chmod -R 705 /home/wzg #使其他用户具有读取和执行的权限
# 4、修改基于用户的配置文件
# vim /etc/httpd/conf.d/userdir.conf
UserDir enabled #第17行,改为开启,表示开启个人用户主页功能
UserDir public_html #第24行,去注释,表示网站数据在用户家目录中的名称
# 5、修改selinux权限
# getsebool -a | grep home
# setsebool httpd_enable_homedirs on
# 6、配置防火墙(同上)
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# 7、重启服务
# systemctl restart httpd
</code></pre>
<p><strong>Client端</strong>访问:</p>
<pre><code class="language-bash"># 有图形化
# firefox http://192.168.100.10/~wzg/
# 没有图形化
# curl http://192.168.100.10/~wzg/
</code></pre>
<h2 id="3搭建基于域名访问的虚拟主机">3、搭建基于域名访问的虚拟主机</h2>
<p>以"www.hubtvu.edu.cn" 为域名来创建一个虚拟网站</p>
<p>1)网页数据存放在/www/hubstc/下;<br>
2)网站主页内容为:"welcome to Hubei Open University,our domain name is www.hubtvu.edu.cn";<br>
3)网站对所有客户端开放。</p>
<p><strong>Server端</strong>配置:</p>
<pre><code class="language-bash"># 首先已经安装了httpd服务
# 1、创建虚拟主机的网页文件
# mkdir -p /www/hubstc
# cd /www/hubstc/
# echo "welcome to Hubei Open University,our domain name is www.hubtvu.edu.cn" >> index.html
# 2、修改文件的访问权限(使其他用户具有可执行权限)
# chmod o+x /www
# chmod o+x index.html
# 3、配置虚拟主机的网页文件
# cd /etc/httpd/conf.d
# vim hubstc.conf
<Directory /www/hubstc>
Require all granted #所有客户端都可访问
</Directory>
<VirtualHost 192.168.100.10>
ServerName www.hubtvu.edu.cn #定义域名
DocumentRoot /www/hubstc #网站主页文件的目录
</VirtualHost>
# 4、做域名解析文件(注意:client端也要配置)
# vim /etc/hosts
加入:192.168.100.10 www.hubtvu.edu.cn
# 5、修改虚拟主机网页文件的selinux上下文类型
# ll -dZ /www #查看/www的上下文类型
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www
# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?' #修改文件的上下文类型,包括/www下面的子文件
# restorecon -RFv /www #以可视化模式强制递归刷新
# 6、配置防火墙(同上)
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# 7、重启服务
# systemctl restart httpd
</code></pre>
<p><strong>Client端</strong>配置及访问:</p>
<pre><code class="language-bash"># 做域名解析
# vim /etc/hosts
加入:192.168.100.10 www.hubtvu.edu.cn
# 访问:
# 有图形化
# firefox http://www.hubtvu.edu.cn
# 没有图形化
# curl http://www.hubtvu.edu.cn
</code></pre>
<h2 id="4搭建基于端口访问的虚拟主机">4、搭建基于端口访问的虚拟主机</h2>
<p>配置两个新的访问端口,分别为8088和8089</p>
<p>1)网站域名为:www.hubtuv.com;<br>
2)网页数据分别存放在/www/8088/和/www/8089/下;<br>
3)每个端口主页内容分别为:"this is new port 8088或8089 for website www.hubtuv.com"。</p>
<p><strong>Server端</strong>配置:</p>
<pre><code class="language-bash"># 首先已经安装了httpd服务
# 1、新建虚拟主机的网页文件
# mkdir -p /www/8088 /www/8089
# cd /www/8088
# echo "this is new port 8088 for website www.hubtuv.com">>index.html
# cd /www/8089
# echo "this is new port 8089 for website www.hubtuv.com">>index.html
# 2、修改文件的访问权限
# chmod o+x /www
# chmod o+x /www/8088/index.html
# chmod o+x /www/8089/index.html
# 3、配置虚拟主机的文件
# cd /etc/httpd/conf.d
# vim 8088.conf
<Directory /www/8088/>
Require all granted
</Directory>
<virtualHost 192.168.100.10:8088> #注意:IP地址后面要加新端口号
DocumentRoot /www/8088/
ServerName www.hubtuv.com #也可以不添加域名
</virtualHost>
# vim 8089.conf
<Directory /www/8089/>
Require all granted
</Directory>
<virtualHost 192.168.100.10:8089>
DocumentRoot /www/8089/
ServerName www.hubtuv.com
</virtualHost>
# 4、添加监听端口
# cd /etc/httpd/conf/
# vim httpd.conf
#在42行 Listen 80的下一行添加:
Listen 8088
Listen 8089
# 5、修改端口的上下文类型
# semanage port -l | grep http #查看httpd服务端口的类型
# semanage port -a 8088 -t http_port_t -p tcp #将8088端口加入httpd服务端口类型
# semanage port -a 8089 -t http_port_t -p tcp #将8089端口加入httpd服务端口类型
# netstat -pant #查看端口号
# 6、修改网页文件的上下文类型(前面已经做过)
# ll -dZ /www #查看/www的上下文类型
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www
# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?' #修改文件的上下文类型,包括/www下面的子文件
# restorecon -RFv /www #以可视化模式强制递归刷新
# 7、添加新端口到防火墙(注意:前面只添加了服务,并没有添加新的端口)
# firewall-cmd --list-all
# firewall-cmd --permanent --add-port=8088/tcp
# firewall-cmd --permanent --add-port=8089/tcp
# firewall-cmd --reload
# 8、重启服务
# systemctl restart httpd
</code></pre>
<p><strong>Client端</strong>访问:</p>
<pre><code class="language-bash"># 有图形化
# firefox www.hubtuv.com:8088
# firefox www.hubtuv.com:8089
# 没有图形化
# curl www.hubtuv.com:8088
# curl www.hubtuv.com:8089
</code></pre>
<h2 id="五搭建基于tls加密的虚拟主机">(五)搭建基于TLS加密的虚拟主机</h2>
<p><strong>注意:</strong>经TLS/SSL安全加密的超文本传输协议,默认情况下使用端口443/TCP</p>
<p><strong>Server端</strong>配置:</p>
<pre><code class="language-bash">注意:前面做的的配置要全部删除,否则配置文件会起冲突
# 1、安装httpd服务
# yum -y install httpd
# 2、安装TLS加密软件,网站内容不用明文传输
# yum -y install mod_ssl
# 3、生成密钥文件
# openssl genrsa > tlsweb.key
# 4、生成证书的请求文件
# openssl req -new -key tlsweb.key > tlsweb.csr
# 5、生成证书文件
# openssl req -x509 -days 365 -key tlsweb.key -in tlsweb.csr > tlsweb.crt
# 6、修改ssl.conf配置文件(第100行、107行)
# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/tlsweb.crt
SSLCertificateKeyFile /etc/pki/tls/private/tlsweb.key
# 7、把证书文件和秘钥文件分别拷贝到ssl.conf配置文件里的对应路径下面
# cp tlsweb.crt /etc/pki/tls/certs/
# cp tlsweb.key /etc/pki/tls/private/
# 8、配置防火墙(注意:添加的是https)
# firewall-cmd --permanent --add-service=https
# firewall-cmd --reload
# 9、重启服务
# systemctl restart httpd
</code></pre>
<p><strong>Client端</strong>访问:</p>
<pre><code class="language-bash"># 使用浏览器去访问
https://192.168.100.10
</code></pre>
<p>创建一个基于tls加密的虚拟主机,要求如下:</p>
<p>1)网站域名:www.hubtvu.edu.cn;<br>
2)网页文件:/var/www/html/index.html<br>
3)网页内容:this is www.hubtvu.edu.cn</p>
<pre><code class="language-bash">在以上基础上完成:
# 服务端创建网页文件
# cd /var/www/html
# echo "this is www.hubtvu.edu.cn" >> index.html
# 做域名解析文件
# vim /etc/hosts
加入:192.168.100.10 www.hubtvu.edu.cn
# scp /etc/hosts 192.168.100.20:/etc
# 客户端使用浏览器去访问
https://www.hubtvu.edu.cn
</code></pre>
<p>查看报错日志文件:journalctl -xe</p>
<p><strong>声明:未经许可,不得转载</strong></p><br><br>
来源:https://www.cnblogs.com/wzgwzg/p/15587199.html
頁:
[1]