最近剛接觸ubuntu和apache,第一次配置就被apahce搞到完全崩潰,跟着網上的配置修改apache的charset和apache2.conf以後,開始出現訪問http://localhost然後直接讓下載的提示:"you have chosen to open",怎麼找都沒找到問題到底載哪兒,不過,還是解決了一部分問題的,總算是找到了apache2的完全卸載方法
網上的教程錯綜複雜真的害死人,親測好用,記錄如下:
1. 刪除apache
$ sudo apt-get --purge remove apache2.2-common
$ sudo apt-get autoremove
$ sudo rm -rf /var/www
3. 重裝apache2
$ sudo apt-get install apache2
$ sudo /etc/init.d/apache2 restart
1>安裝Apache2
sudo apt-get install apache2
修改apache2的配置文件
ubatem@ubuntu:~$ cd /etc/apache2
ubatem@ubuntu:/etc/apache2$ ls
apache2.conf conf-enabled mods-available sites-available
conf-available envvars mods-enabled sites-enabled
conf.d magic ports.conf
我們看到沒有想象中的httpd.conf配置文件,這裏要說明的是apache2的配置文件是apache2.conf,而不是http.conf
ubatem@ubuntu:/etc/apache2$ sudo viapache2.conf
寫入兩條語句
ServerName localhost
DirectoryIndex index.html index.htmindex.PHP
這裏的ServerName localhost是爲了防止最後開啓apache2服務的時候會提示DNS出錯。
DirectoryIndex index.html index.htmindex.php是默認目錄的寫法。
把下面的<Directory/var/www/html/>改成<Directory/var/www>
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
保存退出。
可以在apache2.conf中加入 AddDefaultCharset GB2312
設置默認字符集,定義服務器返回給客戶機默認字符集(由於西歐UTF-8是Apache默認字符集,因此當訪問有中文的網頁時會出現亂碼,這時只要將字符集改成GB2312,再重啓Apache服務即可)
下面這步(橫線裏面的)可以忽略
------------------------------------------------------------------------------------------------------------------------
需要說明的是,在apache2中,根設置(默認主目錄)在 /etc/apache2/sites-АVailable/default中,我們打開default,進行配置。如下
ubatem@ubuntu:/etc/apache2$ cdsites-available
ubatem@ubuntu:/etc/apache2/sites-available$ls
000-default.conf default-ssl.conf
ubatem@ubuntu:/etc/apache2/sites-available$sudo vi 000-default.conf
這裏我們的默認主目錄設置的路徑是/var/www,文檔最上方的VirtualHost後方的*代表通配符,即表示所有本機ip地址,監聽端口爲80,ServerName填寫你註冊的域名,沒有可以不填。保存退出。
------------------------------------------------------------------------------------------------------------------------
至此,基本配置已經全部完成,查看本機ip地址。ip爲 172.16.6.129
ubatem@ubuntu:~$ ifconfig
eth0 Link encap:以太網 硬件地址 00:0c:29:b4:97:17
inet 地址:172.16.6.129 廣播:172.16.6.255 掩碼:255.255.255.0
inet6 地址: fe80::20c:29ff:feb4:9717/64Scope:Link
輸入啓用apache2的命令:/etc/init.d/apache2 restart.並在瀏覽器中輸入本機ip地址。
若顯示 It Works!或者apache的頁面則安裝成功。
2>Gitweb安裝
sudo apt-get install gitweb
默認沒有 css 加載,把 gitweb 要用的靜態文件連接到DocumentRoot 下:
cd /var/www/
sudo ln -s /usr/share/gitweb/* .
修改gitweb.conf配置文件
sudo vi /etc/gitweb.conf
將 $projectroot 改爲Git倉庫存儲目錄(例如:/home/git/abc.git,abc.git爲git初始化的倉庫),保存退出。
如果沒有找到項目,你需要將$projectroot/*.git 的屬性改爲755(sudo chmod 755 xxx.git),讓apache用戶有可讀權限。http://localhost/cgi-bin/gitweb.cgi
修改/etc/gitweb.conf 內容
# path to git projects(<project>.git)
#$projectroot = "/var/cache/git";
$projectroot ="/home/git/repositories";
# directory to use for temp files
$git_temp = "/tmp";
# target of the home link on top of allpages
$home_link = $my_uri || "/";
# html text to include at home page
$home_text = "indextext.html";
# file with project list; by default,simply scan the projectroot dir.
$projects_list = $projectroot;
# stylesheet to use
@stylesheets =("/gitweb/static/gitweb.css");
# JavaScript code for gitweb
$javascript ="gitweb/static/gitweb.js";
# logo to use
$logo ="/gitweb/static/git-logo.png";
# the 'favicon'
$favicon = "/gitweb/static/git-favicon.png";
# git-diff-tree(1) options to use forgenerated patches
#@diff_opts = ("-M");
@diff_opts = ();
修改apache2.conf配置文件
添加如下內容
ScriptAlias /cgi-bin/"/var/www/cgi-bin/"
<Directory /var/www/cgi-bin/>
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
啓用更改,會出現以下信息
ubatem@ubuntu:~$ sudo /etc/init.d/apache2restart
* Restarting web server apache2
[WedMar 04 16:26:54.355891 2015] [alias:warn] [pid 12803:tid 3074894464] AH00671:The ScriptAlias directive in /etc/apache2/conf-enabled/serve-cgi-bin.conf atline 11 will probably never match because it overlaps an earlier ScriptAlias.
[ OK ]
ubatem@ubuntu:~$ cd /etc/apache2
,因爲/etc/apache2/conf-enabled/serve-cgi-bin.conf文件裏面已經有ScriptAlias /cgi-bin/"/var/www/cgi-bin/"這句語句,你可以註釋/etc/apache2/conf-enabled/serve-cgi-bin.conf裏面的ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"語句,也可以忽略,但是在apache2.conf配置文件裏面不能缺少ScriptAlias/cgi-bin/ "/var/www/cgi-bin/"語句,否則你會在瀏覽器訪問不了cgi文件,
403 錯誤 You don't have permissionto access /cgi-bin/XXXX.cgi on this server.
修改/etc/apache2/sites-available/000-000-default.conf配置文件
添加如下內容
<VirtualHost *:80>
ServerName gitserver
DocumentRoot /var/www/cgi-bin
<Directory /var/www/cgi-bin/>
Options +ExecCGI
Options +FollowSymLinks
Options+SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script .cgi .pl
DirectoryIndex gitweb.cgi
</Directory>
</VirtualHost>
保存退出.
這一步非常重要,有可能你的瀏覽器會顯示純文本的cgi
sudo a2enmod cgid,或者是 sudo a2enmod cgi
啓用配置
sudo /etc/init.d/apache2 restart
編寫hello.cgi程序
cd /var/www/cgi-bin
sudo vi hello.c
#include<stdio.h>
intmain()
{
printf("Hello,world\n");
return0;
}
保存退出。
編譯:
#sudo gcc hello.c -o hello.cgi
這裏可以先運行一下看看 hello.cgi是否有錯誤
#sudo ./hello.cgi
若輸出helloworld,則正確
然後可以在瀏覽器中鍵入http://你的apache服務器ip地址/cgi-bin/hello.cgi
若瀏覽器顯示helloworld,則cgi配置正確。
Gitweb配置完成