天天看點

Apache2的安裝與介紹Apache2的安裝與介紹

Apache2的安裝與介紹

1、Apache2的安裝

1、更新apt源
sudo apt-get update
2、安裝apache2
sudo apt-get install apache2
           

預設apache2安裝在了/etc/apache2檔案夾下

目錄結構如下:

Apache2的安裝與介紹Apache2的安裝與介紹

apache2 的幾個簡單指令:

啟動、停止、重新開機、狀态:

sudo /etc/init.d/apache2 [ start | stop | restart | status ]
service apache2 [ start | stop | restart | status ]
           

例如:啟動與檢視狀态

sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 status
           

2、Apache2的配置介紹

可以檢視官網:http://httpd.apache.org/

1、apache2.conf

​ 當apache2伺服器啟動時,就将零散的配置檔案以Including方式組合在一起。這個檔案不是真正的具體配置檔案,它隻是把各個零散的配置檔案以inluceding方式包含進來

這個檔案大概配置了這些東西:

1、apaceh2.conf

2、端口配置檔案port.conf

3、mods-enable檔案夾,這個檔案夾下都是*.load和*.conf字尾的檔案

4、site-enable、conf-enabled檔案夾,這兩個檔案夾下都是*.conf字尾的檔案
           

代碼如下:

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

           

2、envvars

這個檔案是apache程式的參數配置檔案,包括log路徑,程式使用的使用者名等。

預設的使用者與組為:www-data

預設的log位置:/var/log/apache2/下有錯誤日志 error.log和通路日志access.log。

這些配置都會export出來, service apache2在運作前會source envvars檔案。如果手動運作apache2,就應該自己先source,否則會報一些參數未設定。

代碼如下:

unset HOME
# for supporting multiple apache2 instances
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
        SUFFIX="-${APACHE_CONFDIR##/etc/apache2-}"
else
        SUFFIX=
fi
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
# temporary state file location. This might be changed to /run in Wheezy+1
export APACHE_PID_FILE=/var/run/apache2$SUFFIX/apache2.pid
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
export APACHE_LOCK_DIR=/var/lock/apache2$SUFFIX
# Only /var/log/apache2 is handled by /etc/logrotate.d/apache2.
export APACHE_LOG_DIR=/var/log/apache2$SUFFIX
## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:

#. /etc/default/locale
export LANG
           

3、port.conf

這個檔案始終包含在主配置檔案(apache2.conf)中。它用于确定傳入連接配接的偵聽端口,預設為80,我們一般都會重新配置新的端口。

預設監聽的就是80端口,代碼如下:

Listen 80
<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>
           

4、mods-available、mods-enabled

這兩個檔案夾裡面存放的都是一些apache2的讀寫操作等子產品,mods-enabled是apache2伺服器的啟動配置檔案,mods-available是apache2伺服器可以使用的配置檔案

mods-enabled檔案:

Apache2的安裝與介紹Apache2的安裝與介紹

mods-available檔案:

Apache2的安裝與介紹Apache2的安裝與介紹

​ 可以看到:mods-enable裡面的檔案都是mods-available中的快捷方式(或則稱為軟連結)

​ 是以,如果想給apache2添加什麼功能子產品,直接建立一個從avalible中建立一個快捷方式到enabled中就行了,删除也是一樣,删掉enabled中的某子產品的快捷方式就行了,實際的子產品代碼還在available中沒有消失。

5、sites-available、sites-enabled

1、sites-enabled檔案:

Apache2的安裝與介紹Apache2的安裝與介紹

2、sites-available檔案:

Apache2的安裝與介紹Apache2的安裝與介紹

可以看到:sites-enable裡面的檔案都是sites-available中的快捷方式(或則稱為軟連接配接)

裡面隻有兩個檔案,分别為000-default.con、default-ss.conf

  • 000-default.conf是使用http協定網站的預設網站配置檔案
  • default-ss.conf是https協定網站才使用的預設網站配置檔案

000-defaut.conf檔案:

這裡配置了是預設的80端口,如果在port.conf修改了這個預設端口,那麼這裡的這個80端口也要進行對應的修改

ServerAdmin: 設定一個郵件位址,如果伺服器有任何問題将發信到這個位址, 這個位址會在伺服器産生的某些頁面中出現。

**DocumentRoot:**是這個站點的根目錄,預設web目錄:/var/www/html。這樣 Apache2 啟動時會掃描 /etc/apache2/sites-enabled 中可用的 website 配置并加載。

<VirtualHost *:80
 
        #ServerName www.example.com

        ServerAdmin w[email protected]
        DocumentRoot /var/www/html
    
    	# 日志檔案,APACHE_LOG_DIR是在envvars 配置的
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    

</VirtualHost>
           

5、conf-available、conf-enabled

這兩個目錄關于一些伺服器的配置相關的東西

1、conf-available檔案:

Apache2的安裝與介紹Apache2的安裝與介紹

2、conf-enabled 檔案,裡面也是一些軟連結文檔

Apache2的安裝與介紹Apache2的安裝與介紹

繼續閱讀