天天看點

ubuntu下apache2 安裝 配置 解除安裝 CGI設定 SSL設定

一、安裝、解除安裝apache2

     apache2可直接用指令安裝

          sudo apt-get install apache2

     解除安裝比較麻煩,必須卸幹淨,否則會影響apache2再安裝

     (1) 

          sudo apt-get --purge remove apache2

          sudo apt-get --purge remove apache2.2-common

          sudo apt-get autoremove

     (2) (關鍵一步)找到沒有删除掉的配置檔案,一并删除

          sudo find  /etc -name "*apache*" -exec  rm -rf {} \;

          sudo rm -rf /var/www

     開啟/重新開機apache2服務

          service apache2 start/restart

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

二、配置檔案

     嚴格來說,apache2的配置檔案是/etc/apache2/apache2.conf apache先加載apache2.conf,然後根據apache2.conf裡的Include指令載入其他配置檔案。

     動态子產品的配置

          Include mods-enabled/*.load

          Include mods-enabled/*.conf

     使用者自己的配置

          Include httpd.conf

     端口監聽的配置

          Include ports.conf

     一般性的配置語句片斷

          Include conf.d/

     虛拟主機的配置指令

          Include sites-enabled/

     ubuntu下,web的根目錄是在/var/www,設定是在/etc/apache2/sites-enabled/000-default中

      DocumentRoot /var/www

      <Directory />

     Options FollowSymLinks +ExecCGI

     AllowOverride None

      </Directory>

     <Directory /var/www/>

     Options Indexes FollowSymLinks MultiViews

     Order allow,deny

     allow from all

     </Directory>

     web的預設首頁是在/etc/apache2/mods-enabled/dir.conf裡

          DirectoryIndex index.

html

index.cgi index.pl index.php index.xhtml index.htm

     XXX-enabled 和XXX-available目錄,XXX-enabled中放的是指向XXX-available中相應檔案的符号連結,不過雖然如此,隻有用ln指令把XXX-enabled中的檔案連結到XXX-enabled中才能起作用。

三、CGI 設定

     在/var/www下建立一個目錄 cgi-bin,作為cgi程式存放的地點,然後修改配置檔案/etc/apache2/sites-enabled/000-default

     将

          ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

          <Directory "/usr/lib/cgi-bin/">

          AllowOverride None

          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

          Order allow,deny

          Allow from all

          </Directory>

     修改為:

          ScriptAlias /cgi-bin/ /var/www/cgi-bin/

          <Directory "/var/www/cgi-bin">

          AddHandler cgi-script cgi

     重新開機apache服務

     cgi程式的權限必須是755(可運作)。

四、SSL設定

     建立SSL目錄

          sudo mkdir /etc/apache2/ssl

     建立自簽名憑證,内容随便填

          sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

     開啟SSL 子產品

          a2enmod ssl

     編輯/etc/apache2/sites-enabled/default-ssl.conf

          ServerAdmin webmaster@localhost

          ServerName www.unixmen.com:443

          SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.crt

          SSLCertificateKeyFile /etc/apache2/ssl/apache.key

     啟動虛拟主機

          a2ensite default-ssl

     重新開機apache

          sudo service apache2 restart