Fork me on GitHub

Centos7之WEB服务器

1、安装httpd服务

  输入命令:yum -y install httpd

 1 [root@N37012 ~]# yum -y install httpc
 2 Loaded plugins: fastestmirror, langpacks
 3 Loading mirror speeds from cached hostfile
 4 No package httpc available.
 5 Error: Nothing to do
 6 [root@N37012 ~]# yum -y install httpd
 7 Loaded plugins: fastestmirror, langpacks
 8 Loading mirror speeds from cached hostfile
 9 Resolving Dependencies
10 --> Running transaction check
11 ---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed
12 --> Processing Dependency: httpd-tools = 2.4.6-89.el7.centos for package: httpd-2.4.6-89.el7.centos.x86_64
13 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-89.el7.centos.x86_64
14 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64
15 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-89.el7.centos.x86_64
16 --> Running transaction check
17 ---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed
18 ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
19 ---> Package httpd-tools.x86_64 0:2.4.6-89.el7.centos will be installed
20 ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
21 --> Finished Dependency Resolution
22 
23 Dependencies Resolved
24 
25 ========================================================================================================================================================
26  Package                             Arch                           Version                                       Repository                       Size
27 ========================================================================================================================================================
28 Installing:
29  httpd                               x86_64                         2.4.6-89.el7.centos                           updates                         2.7 M
30 Installing for dependencies:
31  apr                                 x86_64                         1.4.8-3.el7_4.1                               os                              103 k
32  apr-util                            x86_64                         1.5.2-6.el7                                   os                               92 k
33  httpd-tools                         x86_64                         2.4.6-89.el7.centos                           updates                          90 k
34  mailcap                             noarch                         2.1.41-2.el7                                  os                               31 k
35 
36 Transaction Summary
37 ========================================================================================================================================================
38 Install  1 Package (+4 Dependent packages)
39 
40 Total download size: 3.0 M
41 Installed size: 10 M
42 Downloading packages:
43 (1/5): httpd-tools-2.4.6-89.el7.centos.x86_64.rpm                                                                                |  90 kB  00:00:00     
44 (2/5): apr-1.4.8-3.el7_4.1.x86_64.rpm                                                                                            | 103 kB  00:00:00     
45 (3/5): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                           |  92 kB  00:00:00     
46 (4/5): mailcap-2.1.41-2.el7.noarch.rpm                                                                                           |  31 kB  00:00:00     
47 (5/5): httpd-2.4.6-89.el7.centos.x86_64.rpm                                                                                      | 2.7 MB  00:00:00     
48 --------------------------------------------------------------------------------------------------------------------------------------------------------
49 Total                                                                                                                   3.0 MB/s | 3.0 MB  00:00:01     
50 Running transaction check
51 Running transaction test
52 Transaction test succeeded
53 Running transaction
54   Installing : apr-1.4.8-3.el7_4.1.x86_64                                                                                                           1/5 
55   Installing : apr-util-1.5.2-6.el7.x86_64                                                                                                          2/5 
56   Installing : httpd-tools-2.4.6-89.el7.centos.x86_64                                                                                               3/5 
57   Installing : mailcap-2.1.41-2.el7.noarch                                                                                                          4/5 
58   Installing : httpd-2.4.6-89.el7.centos.x86_64                                                                                                     5/5 
59   Verifying  : httpd-tools-2.4.6-89.el7.centos.x86_64                                                                                               1/5 
60   Verifying  : mailcap-2.1.41-2.el7.noarch                                                                                                          2/5 
61   Verifying  : httpd-2.4.6-89.el7.centos.x86_64                                                                                                     3/5 
62   Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                                                                                           4/5 
63   Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                                          5/5 
64 
65 Installed:
66   httpd.x86_64 0:2.4.6-89.el7.centos                                                                                                                    
67 
68 Dependency Installed:
69   apr.x86_64 0:1.4.8-3.el7_4.1      apr-util.x86_64 0:1.5.2-6.el7      httpd-tools.x86_64 0:2.4.6-89.el7.centos      mailcap.noarch 0:2.1.41-2.el7     
70 
71 Complete!
72 [root@N37012 ~]# 

2、配置

Apache 主要的配置文件是 /etc/httpd/conf/httpd.conf 。 它包含许多在基本安装中不需要更改的配置。 实际上,只需对此文件进行一些更改即可启动并运行一个简单的网站。

3、监听端口

第一个要修改的是 Listen 配置项,它定义了 Apache 要监听页面请求的 IP 地址和端口。 现在,你只需要使这个网站可以从本地访问,所以使用 localhost 地址。 完成后,该行应该看起来像这样(位置大概在42行)

#Listen 12.34.56.78:80
Listen 127.0.0.1:80

通过将此配置项设置为 localhost 的 IP 地址,Apache 将只侦听来自本地主机的连接。 如果您希望 Web 服务器侦听来自远程主机的连接,则可以使用主机的外部 IP 地址。

4、网页页面html文件位置

DocumentRoot 配置项指定组成网站页面的 HTML 文件的位置。 该配置项不需要更改,因为它已经指向标准位置。 该行应该看起来像这样(位置大概在119行)

DocumentRoot "/var/www/html"

如果想更改存储网站文件的位置,可修改此配置项完成。例如改成/var/myhtml/html

DocumentRoot "/var/myhtml/html"

5、防火墙设置

  查看防火墙状态

1 [root@N37012 ~]# systemctl status firewalld 
2 ● firewalld.service - firewalld - dynamic firewall daemon
3    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
4    Active: inactive (dead)
5      Docs: man:firewalld(1)
6 [root@N37012 ~]#

  启动防火墙

 1 [root@N37012 ~]# systemctl start firewalld.service
 2 [root@N37012 ~]# systemctl status firewalld 
 3 ● firewalld.service - firewalld - dynamic firewall daemon
 4    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
 5    Active: active (running) since Sun 2019-05-19 16:53:09 CST; 1s ago
 6      Docs: man:firewalld(1)
 7  Main PID: 4192 (firewalld)
 8    CGroup: /system.slice/firewalld.service
 9            └─4192 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
10 
11 May 19 16:53:08 N37012 systemd[1]: Starting firewalld - dynamic firewall daemon...
12 May 19 16:53:09 N37012 systemd[1]: Started firewalld - dynamic firewall daemon.
13 [root@N37012 ~]# 

  开放TCP的80端口,并重载防火墙

1 [root@N37012 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
2 success
3 [root@N37012 ~]# firewall-cmd --reload
4 success
5 [root@N37012 ~]# 

  查看防火墙开放端口

1 [root@N37012 ~]# firewall-cmd --zone=public --list-ports
2 80/tcp
3 [root@N37012 ~]# 

6、创建index.html文件

  index.html文件是使用域名访问网站而不是访问特点网页时的默认文件。在/var/www/html中。

  创建index.html文件并输入文字hello word~

  输入命令:chown apache index.html

7、查看Apache状态,如未启动。则启动

 1 [root@N37012 html]# systemctl status httpd
 2 ● httpd.service - The Apache HTTP Server
 3    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
 4    Active: inactive (dead)
 5      Docs: man:httpd(8)
 6            man:apachectl(8)
 7 [root@N37012 html]# systemctl start httpd
 8 [root@N37012 html]# systemctl status httpd
 9 ● httpd.service - The Apache HTTP Server
10    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
11    Active: active (running) since Sun 2019-05-19 17:00:14 CST; 4s ago
12      Docs: man:httpd(8)
13            man:apachectl(8)
14  Main PID: 5405 (httpd)
15    Status: "Processing requests..."
16    CGroup: /system.slice/httpd.service
17            ├─5405 /usr/sbin/httpd -DFOREGROUND
18            ├─5406 /usr/sbin/httpd -DFOREGROUND
19            ├─5407 /usr/sbin/httpd -DFOREGROUND
20            ├─5408 /usr/sbin/httpd -DFOREGROUND
21            ├─5409 /usr/sbin/httpd -DFOREGROUND
22            └─5410 /usr/sbin/httpd -DFOREGROUND
23 
24 May 19 17:00:14 N37012 systemd[1]: Starting The Apache HTTP Server...
25 May 19 17:00:14 N37012 httpd[5405]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.30.0.3....s message
26 May 19 17:00:14 N37012 systemd[1]: Started The Apache HTTP Server.
27 Hint: Some lines were ellipsized, use -l to show in full.
28 [root@N37012 html]# 

8、访问web服务器

在Chrome或IE浏览器地址栏中输入Apache地址。即可访问到本机

 

posted @ 2019-06-08 12:08  苦逼运维-匿名  阅读(9462)  评论(3编辑  收藏  举报
1 2 3 4