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地址。即可訪問到本機

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM