首先注明下面的操作都是在root下执行或者使用sudo命令。
1)去http://httpd.apache.org/download 下载apache源码
2)解压到/usr/src,在下载目录执行tar -zxvf httpd-x.x.x.tar.gz -C /usr/src (我用的是httpd-2.4.3.tar.gz)(root权限下)
3)进入目录/usr/src/httpd-2.4.3,执行:./configure --prefix=/usr/local/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl
4)出现configure: error: APR not found。解决办法:
4.1)去apr下载地址 http://apr.apache.org/ 下载源码
4.2)解压到/usr/src,在下载目录执行tar -zxvf apr-x.x.x.tar.gz -C /usr/src/ (我用的是apr-1.4.6.tar.gz)
4.3)进入目录/usr/src/apr-1.4.6,执行./configure --prefix=/usr/local/apr;make;make install
5)再次进入目录/usr/src/httpd-2.4.3,增加参数--with-apr=/usr/local/apr/,重新执行:./configure --prefix=/usr/local/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr/
6)出现configure: error: APR-util not found .解决办法
6.1)去apr-util下载地址 http://apr.apache.org/ 下载源码
6.2)解压到/usr/src,在下载目录执行tar -zxvf apr-util-x.x.x.tar.gz -C /usr/src/ (我用的是apr-util-1.5.1.tar.gz)
6.3)进入目录/usr/src/apr-util-1.5.1,执行./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/;make;make install
7)再次进入目录/usr/src/httpd-2.4.3,增加参数--with-apr-util=/usr/local/apr-util/,重新执行:./configure --prefix=/usr/local/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/
8)出现configure: error: pcre-config for libpcre not found.解决办法:
8.1)去pcre下载地址 http://pcre.org/ 下载源码
8.2)解压到/usr/src,在下载目录执行tar -zxvf pcre-x.x.tar.gz -C /usr/src/ (我用的是pcre-8.31.tar.gz)
8.3)进入目录/usr/src/pcre-8.31,执行./configure --prefix=/usr/local/pcre;make;make install
9)再次进入目录/usr/src/httpd-2.4.3,增加参数--with-pcre=/usr/local/pcre,重新执行:./configure --prefix=/usr/local/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre
10)出现configure: WARNING: OpenSSL version is too old;checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures;解决办法:
10.1)openssl源码下载地址http://www.openssl.org/source/ 下载源码
10.2)解压到/usr/src,在下载目录执行tar -zxvf openssl-x.x.x.tar.gz -C /usr/src/ (我用的是openssl-1.0.1c.tar.gz)
10.3)进入目录/usr/src/openssl-1.0.1c,执行./config --prefix=/usr/local/openssl;make;make install
11)再次进入目录/usr/src/httpd-2.4.3,增加参数--with-ssl=/usr/local/openssl,重新执行:./configure --prefix=/usr/local/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl=static --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre --with-ssl=/usr/local/openssl ;make ;make install;
成功执行。
http://blog.chinaunix.net/uid-18997108-id-273857.html
12)启动apache,进入/usr/local/apache2/bin目录,执行命令sudo ./httpd -k stop/start/restart 或者 sudo apachectl start/stop/restart(apachectl是执行脚本)
/usr/local/apache/conf/httpd.conf 中#ServerName www.example.com:80 改为ServerName localhost:8080
13)设置httpd在任何目录下都可以运行,打开vim /etc/environment,增加配置/usr/local/apache2/bin
14)设置apache开机自动启动,首先拷贝apachectl到目录/etc/init.d/,执行命令sudo cp apachectl /etc/init.d/ ;然后执行sudo update-rc.d apachectl defaults
15) 在root下执行apachectl start/stop失败,报错:httpd: Could not reliably determine the server's fully qualified domain name。
解决办法:(1)进入apache的安装目录:(视个人安装情况而不同) [root@server ~]# cd /usr/local/apache/conf
(2)编辑httpd.conf文件,搜索"#ServerName",添加ServerName localhost:8080
(3)重新启动apache 即可。执行apachectl restart
16)遇到报错:Permission denied: make_sock: could not bind to address 0.0.0.0:8080
网上搜到的解决办法如下:
httpd文件的权限必须是4755并且归属root用户…..简单的说,s能让普通用户作为文件属主/组运行,因为有些程序只有文件属主/组才能运行的,如 /usr/bin/ping,ping命令只能由 root运行,为了让普通用户也能运行,就必须加上s位,你可试验一下:
先去除s:chmod 755 ping
用普通用户运行ping,你会发现权限不足。
加上s:chmod 4755 ping
再运行就正常了。
ps:对shell脚本设置无效!只对应用程序有效!
17)去浏览器输入127.0.0.1,可以显示apache自带的网页It works。该网页的默认目录是在/usr/local/apache2/htdocs/。apache到此就安装OK了。
也可以设置index.php
<?php
phpinfo();
?>
在localhost:8080/index.php。
另外,需要配置Cgi,使得,apache能够执行cgi脚本。具体如下:
想实践下Apache是如何运行cgi程序的,却发现先前编译安装Apache的时候,没有安装Apache的cgi模块。
附:CentOS6.x编译安装LAMP(2):编译安装 Apache2.2.25
此时,从 httpd.conf 文件中可看到如下模块被动态加载(没有cgi模块):
1 | LoadModule deflate_module modules/mod_deflate.so |
2 | LoadModule expires_module modules/mod_expires.so |
3 | LoadModule headers_module modules/mod_headers.so |
4 | LoadModule ssl_module modules/mod_ssl.so |
5 | LoadModule rewrite_module modules/mod_rewrite.so |
6 | LoadModule php5_module modules/libphp5.so |
查了下网络资料,发现Apache自带的apxs工具,可以在不重新编译Apache的前提下,给Apache添加模块。
Apache运行cgi程序需要用到2个模块:mod_cgi.so 与 mod_cgid.so
操作如下:
添加的模块:
LoadModule cgi_module libexec/mod_cgi.so
LoadModule cgid_module libexec/mod_cgid.so
添加步骤:
如要额外安装cgi,先找到mod_cgi.c及mod_cgid.c。一般在apache安装包目录下,如 ./httpd-2.2.25/modules/generators 。
#编译安装 cgi模块
1 | cd /usr/local/src/Apache-2.2.25/httpd-2.2.25/modules/generators |
2 | /usr/local/apache/bin/apxs -i -a -c mod_cgi.c |
编译成功后会输出:
1 | cd /usr/local/src/Apache-2.2.25/httpd-2.2.25/modules/generators |
2 | /usr/local/apache/bin/apxs -i -a -c mod_cgi.c |
3 |
4 | .... #省略掉了前部分内容 |
5 | ---------------------------------------------------------------------- |
6 | chmod 755 /usr/local/apache/modules/mod_cgi.so |
7 | [activating module `cgi' in /usr/local/apache/conf/httpd.conf] #这行表示,在httpd.conf中已经加载了cgi module |
CGI动态页面
相关模块 | 相关指令 |
---|---|
CGI(公共网关接口)定义了web服务器与外部内容生成程序之间交互的方法,通常是指CGI程序或者CGI脚本,它是在网站上实现动态页面的最简单和常用的方法。本文将对如何在Apache web服务器上建立CGI以及如何编写CGI程序进行介绍。
配置Apache以允许CGI
要让CGI程序能正常运作,必须配置Apache以允许CGI的执行,
第一步:配置Apache(httpd.conf)加载cgi模块,详细参考 Apache不重新编译,利用apxs工具给Apache添加模块,如cgi模块
第二步:配置Apache(httpd.conf)设置cgi目录,如下方法之一:
方法一:ScriptAlias
ScriptAlias指令使Apache允许执行一个特定目录中的CGI程序。当客户端请求此特定目录中的资源时,Apache假定其中所有的文件都是CGI程序并试图运行它。
ScriptAlias指令形如:/cgi-bin/是这个目录的别名
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
完整配置参考如下:
01 | <IfModule alias_module> |
02 | ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/" |
03 | </IfModule> |
04 |
05 | <Directory "/usr/local/apache2/cgi-bin"> |
06 | AllowOverride None |
07 | Options None |
08 | Order allow,deny |
09 | Allow from all #千万别忘了这个配置 |
10 | </Directory> |
如果Apache被安装到默认位置,默认的配置文件httpd.conf中就会有上述配置。ScriptAlias与Alias指令非常相似,都是定义了映射到一个特定目录的URL前缀,两者一般都用于指定位于DocumentRoot以外的目录,其不同之处是ScriptAlias又多了一层含义,即URL前缀后面的任何文件都被视为CGI程序。所以,上述例子会指示Apache:任何以/cgi-bin/开头的资源都将映射到/usr/local/apache2/cgi-bin/目录中,且视之为CGI程序。
例如,如果有URL为http://www.example.com/cgi-bin/test.pl的请求,Apache会试图执行/usr/local/apache2/cgi-bin/test.pl文件并返回其输出。当然,这个文件必须存在而且可执行,并以特定的方法产生输出,否则Apache返回一个出错消息。
方法二:ScriptAlias目录以外的CGI
由于安全原因,CGI程序通常被限制在ScriptAlias指定的目录中,这样,管理员就可以严格控制谁可以使用CGI程序。但是,如果采取了恰当的安全措施,则没有理由不允许其他目录中的CGI程序运行。比如,你可能希望用户在UserDir指定的宿主目录中存放页面,而他们有自己的CGI程序,但无权访问cgi-bin目录,这样,就产生了运行其他目录中CGI程序的需求。
允许CGI在任意目录执行需要两个步骤:
#第一步:指定特定文件后缀为CGI文件(即,告诉服务器哪些文件是CGI文件)。
有2种方法:方法一是定义MIME类型为application/x-httpd-cgi,方法二是使用 AddHandler 或 SetHandler 指令
例如,下面的AddHandler指令告诉服务器所有带有cgi或pl后缀的文件是CGI程序。(添加到配置文件的最后面即可)
1 | AddType application/x-httpd-cgi .cgi .pl #方法一 |
2 | AddHandler cgi-script .cgi .pl #方法二 |
#第二步:设置CGI的目录属性,必须在Options指令中启用ExecCGI选项。
可以在主配置文件中,使用Options指令显式地允许特定目录中CGI的执行:
1 | <Directory "/usr/local/apache2/cgi-bin"> |
2 | Options ExecCGI |
3 | AllowOverride None |
4 | Order allow,deny |
5 | Allow from all |
6 | </Directory> |
方法三:.htaccess文件
.htaccess指南示范了怎样在没有权限修改httpd.conf文件的情况下激活CGI程序。
#用户目录
为了允许用户目录中所有以".cgi"结尾的文件作为CGI程序执行,你可以使用以下配置:
<Directory /home/*/public_html>
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
如果你想在用户目录中指定一个cgi-bin子目录,其中所有的文件都被当作CGI程序,你可以这样配置:
<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory>
#编写CGI程序
编写CGI程序和"常规"程序之间有两个主要的不同。
首先,在CGI程序的所有输出前面必须有一个HTTP的MIME类型的头,对客户端指明所接收内容的类型,大多数情况下,像这样:
Content-type: text/html
其次,输出要求是HTML形式的,或者是浏览器可以显示的其他某种形式。多数情况下,输出是HTML形式的,但偶然也会输出一个gif图片或者其他非HTML的内容。
除了这两点,编写CGI程序和编写其他程序大致相同。
#第一个CGI程序
这个CGI程序的例子在浏览器中打印一行文字。把下列存为first.pl文件,并放在你的cgi-bin目录中。
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";
即使不熟悉Perl语言,你也应该能看出它干了什么。第一行,告诉Apache这个文件可以用/usr/bin/perl(或者任何你正在使用的shell)解释并执行。第二行,打印上述要求的内容类型说明,并带有两个换行,在头后面留出空行,以示HTTP头的结束。第三行,打印文字"Hello, World."。程序到此结束。
打开你喜欢的浏览器并输入地址:
http://www.example.com/cgi-bin/first.pl
或者是你存放程序的其他位置,就可以在浏览器窗口中看到一行:Hello, World. 。虽然并不怎么激动人心,但是一旦这个程序能正常运行,那么就可能运行其他任何程序。
附:
(1)Shell脚本cgi程序
1 | #!/bin/bash |
2 | echo "Content-type: text/html" |
3 | echo # 注意,这行不能少 |
4 | echo "Hello, Shell.cgi" |
(2)Python脚本cgi程序
1 | #!/usr/bin/python |
2 | print( "Content-type: text/html\n" ) // 末尾的换行符不能少 |
3 | print( "Hello, Python.cgi" ) |
(3)使用任何语言编写都可以,只要程序可以正常执行,Apache就可以把输出原封不动的发给浏览器。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#include <iostream>
using namespace std;
int main ()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
命名为demo.cpp,使用g++ demo.cpp -o demo.cgi。然后放到cgi-bin目录即可。
http://www.cnblogs.com/moonlove/archive/2012/02/22/2509147.html
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
http://blog.chinaunix.net/uid-509190-id-3151423.html
server虚拟主机配置
下面介绍对虚拟主机的配置。建议将对虚拟主机进行配置的内容写进另外一个文件,然后通过include指令包含进来,这样更便于维护和管理。
server{
listen 80;
server_name 192.168.12.188 www.ixdba.net;
index index.html index.htm index.jsp;
root /web/wwwroot/www.ixdba.net
charset gb2312;
server标志定义虚拟主机开始,listen用于指定虚拟主机的服务端口,server_name用来指定IP地址或者域名,多个域名之间用空格分开。Index用于设定访问的默认首页地址,root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径。Charset用于设置网页的默认编码格式。
access_log logs/www.ixdba.net.access.log main;
access_log用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。
URL匹配配置
URL地址匹配是进行Nginx配置中最灵活的部分。 location支持正则表达式匹配,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。
以下这段设置是通过location指令来对网页URL进行分析处理,所有扩展名以.gif、.jpg、.jpeg、.png、.bmp、.swf结尾的静态文件都交给nginx处理,而expires用来指定静态文件的过期时间,这里是30天。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root /web/wwwroot/www.ixdba.net;
expires 30d;
}
以下这段设置是将upload和html下的所有文件都交给nginx来处理,当然,upload和html目录包含在/web/wwwroot/www.ixdba.net目录中。
location ~ ^/(upload|html)/ {
root /web/wwwroot/www.ixdba.net;
expires 30d;
}
在最后这段设置中,location是对此虚拟主机下动态网页的过滤处理,也就是将所有以.jsp为后缀的文件都交给本机的8080端口处理。
location ~ .*.jsp$ {
index index.jsp;
proxy_pass http://localhost:8080;
}
ubuntu环境下nginx源码编译安装
1、更新系统
sudo apt-get update && sudo apt-get upgrade
2、安装nginx的依赖包 zlib pcre openssl(可以源码安装也可以直接系统安装)
sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential
3、下载openssl源码包
wget http://www.openssl.org/source/openssl-1.0.2a.tar.gz
sudo tar -zxvf openssl-1.0.2a.tar.gz -C /usr/local/src/
cd /usr/local/src/openssl-1.0.2a/
sudo ./config
sudo make && sudo make install
4、下载nginx源码包
wget http://nginx.org/download/nginx-1.8.0.tar.gz
sudo tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.8.0
sudo ./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl
sudo make && sudo make install
5、配置nginx 开机服务。
默认这么安装好以后每次检查配置、重启之类的操作略麻烦,所以我们模仿 Ubuntu 14.04 官方源,给系统设置个 nginx 服务,方便我们检查配置、启动重启关闭 Nginx 以及开机自动启动 Nginx
sudo vim /etc/init.d/nginx
插入如下内容:
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides: nginx
5 # Required-Start: $local_fs $remote_fs $network $syslog
6 # Required-Stop: $local_fs $remote_fs $network $syslog
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: starts the nginx web server
10 # Description: starts nginx using start-stop-daemon
11 ### END INIT INFO
12
13 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
14 DAEMON=/usr/sbin/nginx
15 NAME=nginx
16 DESC=nginx
17
18 # Include nginx defaults if available
19 if [ -f /etc/default/nginx ]; then
20 . /etc/default/nginx
21 fi
22
23 test -x $DAEMON || exit 0
24
25 set -e
26
27 . /lib/lsb/init-functions
28
29 test_nginx_config() {
30 if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
31 return 0
32 else
33 $DAEMON -t $DAEMON_OPTS
34 return $?
35 fi
36 }
37
38 case "$1" in
39 start)
40 echo -n "Starting $DESC: "
41 test_nginx_config
42 # Check if the ULIMIT is set in /etc/default/nginx
43 if [ -n "$ULIMIT" ]; then
44 # Set the ulimits
45 ulimit $ULIMIT
46 fi
47 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
48 --exec $DAEMON -- $DAEMON_OPTS || true
49 echo "$NAME."
50 ;;
51
52 stop)
53 echo -n "Stopping $DESC: "
54 start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
55 --exec $DAEMON || true
56 echo "$NAME."
57 ;;
58
59 restart|force-reload)
60 echo -n "Restarting $DESC: "
61 start-stop-daemon --stop --quiet --pidfile \
62 /var/run/$NAME.pid --exec $DAEMON || true
63 sleep 1
64 test_nginx_config
65 # Check if the ULIMIT is set in /etc/default/nginx
66 if [ -n "$ULIMIT" ]; then
67 # Set the ulimits
68 ulimit $ULIMIT
69 fi
70 start-stop-daemon --start --quiet --pidfile \
71 /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
72 echo "$NAME."
73 ;;
74
75 reload)
76 echo -n "Reloading $DESC configuration: "
77 test_nginx_config
78 start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
79 --exec $DAEMON || true
80 echo "$NAME."
81 ;;
82
83 configtest|testconfig)
84 echo -n "Testing $DESC configuration: "
85 if test_nginx_config; then
86 echo "$NAME."
87 else
88 exit $?
89 fi
90 ;;
91
92 status)
93 status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
94 ;;
95 *)
96 echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
97 exit 1
98 ;;
99 esac
100
101 exit 0
注意要设置好nginx的启动路径 DAEMON=/usr/sbin/nginx
6、设置文件权限并增加到系统服务
sudo chmod +x ./nginx
sudo update-rc.d nginx defaults
7、启动nginx
sudo /etc/init.d/nginx
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nginx 源码安装:
/usr/local/nginx/conf/nginx.conf中添加:
server {
listen 80;
server_name localhost;
index index.html index.htm index.php
root /usr/local/nginx/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root /usr/local/nginx/html;
expires 30d;
}
//底下,是将动态请求转发到8080端口的apache服务器。
location ~ .*.php$ {
index index.php;
proxy_pass http://localhost:8080;
}
location ~ .*.cgi$ {
index index.cgi;
proxy_pass http://localhost:8080;
}