配置安装转载网上的 Apache2 + Gitweb+CGI配置

最近刚接触ubuntu和apache,第一次配置就被apahce搞到完全崩溃,跟着网上的配置修改apache的charset和apache2.conf以后,开始出现访问http://localhost然后直接让下载的提示:"you have chosen to open",怎么找都没找到问题到底载哪儿,不过,还是解决了一部分问题的,总算是找到了apache2的完全卸载方法

网上的教程错综复杂真的害死人,亲测好用,记录如下:


1. 删除apache

代码:
$ 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

3. 重装apache2
$ sudo apt-get install apache2

$ sudo /etc/init.d/apache2 restart


Apache2 + Gitweb+CGI配置

 

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配置完成


猜你喜欢

转载自blog.csdn.net/dongfengxueli/article/details/76261892