您的位置:首页 > 理论基础 > 计算机网络

CentOS 7.2 配置Apache服务(httpd)--上篇

2018-02-02 13:37 597 查看
[乐意黎转载]Centos 7 Apache 服务安装及配置: http://blog.csdn.net/aerchi/article/details/72830535
CentOS 7.2 配置Apache服务(httpd)--上篇http://blog.csdn.net/wh211212/article/details/52982917



一、Apache简介

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器软件,可以在大多数电脑操作系统中运行,由于其跨平台和安全性(尽管不断有新的漏洞被发现,但由于其开放源代码的特点,漏洞总能被很快修补。因此总合来说,其安全性还是相当高的。)。被广泛使用,是最流行的Web服务器软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
软件图标 




二、安装Apache httpd

安装httpd以配置Web服务器, HTTP使用80 / TCP
[1] 安装 httpd.
[root@linuxprobe ~]# yum -y install httpd
# 删除默认欢迎页面
[root@linuxprobe ~]# rm -f /etc/httpd/conf.d/welcome.conf
[2] 配置httpd,将服务器名称替换为您自己的环境
[root@linuxprobe ~]# vi /etc/httpd/conf/httpd.conf
# line 86: 改变管理员的邮箱地址
ServerAdmin root@linuxprobe.org
# line 95: 改变域名信息
ServerName www.linuxprobe.org:80
# line 151: none变成All
AllowOverride All
# line 164: 添加只能使用目录名称访问的文件名
DirectoryIndex index.html index.cgi index.php
# add follows to the end
# server's response header(安全性)
ServerTokens Prod
# keepalive is ON
KeepAlive On
[root@linuxprobe ~]# systemctl start httpd
[root@linuxprobe ~]# systemctl enable httpd
[3] 如果Firewalld正在运行,请允许HTTP服务。,HTTP使用80 / TCP
[root@linuxprobe ~]# firewall-cmd --add-service=http --permanent
success
[root@linuxprobe ~]# firewall-cmd --reload
success
[4] 创建一个HTML测试页,并使用Web浏览器从客户端PC访问它。如果显示以下页面,是正确的
[root@linuxprobe ~]# vi /var/www/html/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Welcome access LinuxProbe.org,This is Test Page!
</div>
</body>
</html>


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35



版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。

本文已收录于以下专栏:
CentOS 7系统管理与运维实战

=====================================================================


前言

在上篇文章中,已经对 Centos 7 FTP(vsftp)服务安装及配置进行了详细的介绍,如果你对于FTP有什么不懂的可以去看看这篇文章。在这里我就不重复了。本文给大家介绍下在Centos
7 下如何安装及配置 Apache,Apache的安装及配置比FTP的安装及配置更为简单些。只需要几行代码就可以了。下面我们马上开始吧!


Apache 安装

Apache的安装非常地简单,你只需要输入一行代码回车就可以了
# yum install httpd


同样的在安装过程中会提示“is this ok [y/d/N]”,输入y,回车即可。

这样就完成了Apache 的安装。


Apache 配置

安装完之后,这个服务是还没开启的,所以接下来我们可以通过一段命令代码来开启 Apache 服务
# systemctl start httpd.service


你还可以通过下面这一行命令代码查看Apache 服务的运行状态
# systemctl status httpd.service


还有一行命令代码你需要运行下的,它可以让你在重启服务器时自动启动Apache 服务,这样就不怕重启服务后,手动开启Apache 服务了。
# systemctl enable httpd.service


如果有需要(如:更改站点根目录),你还可以打开Apache的配置文件进行编辑
# vim /etc/httpd/conf/httpd.conf


安装完这后,你在浏览器中输入IP时,你还不能访问站点。这是因为防火墙里没设置端口规则,下面我们可以对防火墙进行相应的配置,打开防火墙(默认时firewalld防火墙是关闭的,iptables是开启的,所以我们在这里对 iptables 进行修改)的配置文件
# vi /etc/sysconfig/iptables


按i键进入编辑模式,在文件里追加如下一行代码
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT


编辑完成后,按 Esc 键退出编辑模式,然后按
:wq
,保存退出。

重启下 iptables 服务就可以了
# systemctl restart iptables.service


Apache 服务到此已经配置完成,现在你就可以通过IP来访问站点了,此时你会看到一个 Apache 的测试页面,如果没有显示 Apache 的测试页面说明配置还没成功。重复上面的步骤即可,这个测试页面也不是没用的,至少它在页面的右下角告诉了你它的默认站点目录 /var/www/html。如果你不满意它给你设定好的站点根目录,你也可以自定义站点目录。

如果你觉得 Apache 的测试页面不能说明什么问题,那么你可以执行下面一行命令代码,在站点目录下新建一个 index.html 文件进行测试。
# echo "我是一个新站点!" >> /var/www/html/index.html


刷新下页面后,如果页面显示内容为:“我是一个新站点!”,则说明 Apache 配置成功了。

自定义站点目录

改变 Apache 默认的站点根目录,我们先通过下面的命令代码创建新站点目录,这例子中我们直接在 Apache 站点根目录下直接创建一个目录,至于这个站点目录其实放到哪都没问题。作为例子,就怎么简单怎么来就好,能把问题说清楚就够了。

输入下面命令行,在 Apache 默认的站点根目录新建一个目录 yunkus.com
# mkdir /var/www/html/yunkus.com


这一步做完之后,我们还得修改下 Apache 的配置文件

输入下面的命令行
# vi /etc/httpd/conf/httpd.conf


定位到下面这一行代码
DocumentRoot "/var/www/html"


修改成
DocumentRoot "/var/www/html/yunkus.com"


这样Apache 的站点根目录就更改成了/var/www/html/yunkus.com,当你在地址栏中输入服务器的IP时默认就会访问yunkus.com目录了。

但在这里不说说另一种情况是不行的,因为如果你完完全全地更改了站点的根目录后,你不仅要修改 DocumentRoot “/var/www/html” ,你还得修改其它地方,下面我再举一个例子。

如:我们想把站点的根目录改成/home/wwwroot/yunkus.com 目录下。

我们首先要做的就是创建这个目录
# mkdir -p /home/wwwroot/yunkus.com


进入配置文件
# vi /etc/httpd/conf/httpd.conf


定位到 DocumentRoot “/var/www/html/” 把
DocumentRoot "/var/www/html"


修改成
DocumentRoot "/home/wwwroot/yunkus.com"


除了修改这里外,我们还得多修改一个地方,定位到
# Further relax access to the default document root:

<Directory "/var/www/html">


按下i键进入编辑模式,把上面的代码改成
# Further relax access to the default document root:

<Directory "/home/wwwroot/yunkus.com">


为了方便你定位,我把它上一行注释的代码也复制过来了。你只需要关心第二行代码就行。改完后,按 Esc 键退出编辑模式,再按:wq退出保存。

 注意:记得重启 Apach 服务!
# systemctl restart httpd.service


现在你就可以访问站点试试了。如果你站点目录下没有任何文章,那么当你访问时就会出现 Apache 测试页面,这是再正常不过了,但如果你想访问这个站点目录下的其它文件如:index.html (默认的访问文件格式)时,不管你怎么刷新,浏览器还是显示 Apache 测试页面,这又是为什么呢?原来,我们还没给这个站点目录相应的权限(默认是750),所以给目录修改权限势在必行。

执行下下命令行
# chmod -R 755 /home/wwwroot/yunkus.com


 注意:R 要大写。

现在你再刷新试试,现在访问 index.html 应该没什么问题了。


相关命令

在这里为了方便你学习Linux的相关命令,我把本文中所涉及到的命令重新整理出来,这样可以让你对Linux的命令行有一个更加全面的了解。


Apache 服务安装

# yum install httpd


开启 Apache 服务
# systemctl start httpd.service


重启 Apache 服务
# systemctl restart httpd.service


停用Apache 服务
# systemctl stop httpd.service


查看Apache 服务状态
# systemctl status httpd.service


重启服务器自动启动Apache 服务
# systemctl enable httpd.service


打开Apache 配置文件
# vim /etc/httpd/conf/httpd.conf



iptables 防火墙

系统默认情况下iptables 防火墙服务是开启的。

编辑 iptables 防火墙配置文件
# vi /etc/sysconfig/iptables


查看 iptables 防火墙服务
# systemctl status iptables.service


重启服务器自动启动 iptables 服务
# systemctl enable iptables.service


开启 iptables 防火墙服务
# systemctl start iptables.service


重启 iptables 防火墙服务
# systemctl restart iptables.service


关闭 iptables 防火墙服务
# systemctl stop iptables.service



firewalld 防火墙

系统默认情况下firewalld防火墙是关闭的。

编辑防火墙配置文件
# vi /etc/sysconfig/iptables


查看 firewalld 防火墙服务
# systemctl status firewalld.service


重启服务器自动启动 firewalld 服务
# systemctl enable firewalld.service


开启 firewalld 防火墙服务
# systemctl start firewalld.service


重启 firewalld 防火墙服务
# systemctl restart firewalld.service


关闭 firewalld 防火墙服务
# systemctl stop firewalld.service
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux apache