Linux(Centos7)安裝apache服務(wù)器

Centos安裝apache服務(wù)器

概述

Apache HTTP Server ("httpd"),一款開源的HTTP服務(wù)器,官網(wǎng)地址:Apache HTTP Server Project。因需要提供文件的下載功能,簡(jiǎn)單搭建了這個(gè)服務(wù)器,基本滿足需求。

安裝前

系統(tǒng)信息:

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

檢測(cè)是否安裝過,我機(jī)器上已經(jīng)安裝過,如下:

[root@localhost /]# rpm -qa | grep httpd
httpd-tools-2.4.6-45.el7.centos.4.x86_64
httpd-2.4.6-45.el7.centos.4.x86_64
httpd-devel-2.4.6-45.el7.centos.4.x86_64

安裝過程:

yum 安裝 httpd

yum install httpd-devel.x86_64

提示完成后,修改配置文件:

修改配置

vim /etc/httpd/conf/httpd.conf

ServerName 192.168.52.131:8989

Listen 8989

DocumentRoot "/home/doc"

<Directory "/home/doc">
    AllowOverride None

    Require all granted
</Directory>

<Directory "/home/doc/com">

    Options Indexes FollowSymLinks

    AllowOverride None

    Require all granted
</Directory>

其中 ServerName 要寫成訪問的域名或IP地址,再加上端口; DocumentRoot 是訪問的根目錄,需要對(duì)文件做不同的分類放置的話,需要單獨(dú)配置,需要將 denied 改成 granted ;如果不需要 conf.d 中的配置,可以將 IncludeOptional conf.d/*.conf 注釋掉;還有就是端口不要沖突。

啟動(dòng)服務(wù)

systemctl restart httpd.service

[root@localhost conf]# systemctl restart httpd.service 
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

可能在啟動(dòng)時(shí)會(huì)有問題,根據(jù)提示 journalctl -xe 查看錯(cuò)誤信息,可能是配置的問題。

如果在啟動(dòng)后,依然無法訪問,那么就有可能是防火墻的問題。

防火墻配置

添加允許 http 訪問:

firewall-cmd --add-service=http

[root@localhost home]# firewall-cmd --add-service=http
FirewallD is not running

[root@localhost home]# systemctl start firewalld
Failed to start firewalld.service: Unit firewalld.service is masked.

[root@localhost home]# systemctl unmask firewalld.service 
Removed symlink /etc/systemd/system/firewalld.service.

[root@localhost home]# firewall-cmd --add-service=http
success

端口開放

將需要的端口開放:

firewall-cmd --permanent --add-port=8989/tcp

[root@localhost home]# firewall-cmd --permanent --add-port=8989/tcp
success

重啟服務(wù)

配置好之后,再重啟防火墻:

firewall-cmd --reload

成功之后,在重啟 httpd 服務(wù)并添加開機(jī)自啟動(dòng):

[root@localhost home]# systemctl restart httpd.service 
[root@localhost home]# systemctl enable httpd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

在瀏覽器地址欄輸入域名或IP地址加上端口號(hào),可以訪問配置的根目錄則表示成功。

總結(jié)

關(guān)鍵步驟

  1. 安裝:

yum install httpd-devel.x86_64

  1. 配置:

vim /etc/httpd/conf/httpd.conf

  1. 啟動(dòng)/重啟:

systemctl start/restart httpd.service

  1. 防火墻允許http訪問:

firewall-cmd --add-service=http

  1. 防火墻端口開放

firewall-cmd --permanent --add-port=8989/tcp

  1. 防火墻重啟

firewall-cmd --reload

問題

  1. 配置文件httpd.conf的配置項(xiàng)需要仔細(xì),包括端口、文件目錄等,配置錯(cuò)誤會(huì)導(dǎo)致無法成功;
  2. 防火墻的策略需要檢測(cè);
  3. 系統(tǒng)相關(guān),例如我用的Centos 7和6的配置就有所區(qū)別。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容