Linux 下安裝支援 JSP/PHP 的 WEB server
最後更新:2017-02-28
來源:互聯網
上載者:User
js|server|web 0. 綜述\r
0.1 目標\r
在 Linux 平台上安裝一個支援 SSL、URL 重寫、反向 Proxy、頁面壓縮、CGI、PHP、JSP 的 免費 WEB 伺服器\r
0.2 許可協議\r
GNU Free Documentation License http://www.gnu.org/licenses/fdl.html
允許任意轉載,但請保持本文檔的完整性
如有修改,務請通知作者
0.3 作者
鈍刀 webmaster@yufeng.net
1. 下載
1.1 Apache http://www.apache.org/dist/httpd/
1.2 PHP http://www.php.net/downloads.php
1.3 Tomcat http://www.apache.org/dist/jakarta/tomcat-4/
1.4 JTC(jakarta tomcat connectors) http://www.apache.org/dist/jakarta/tomcat-4/source/
2. 安裝
2.1 Apache
$tar vfxz httpd-2.0.46.tar.gz
$cd httpd-2.0.46
$./configure \
$--enable-so \
$--enable-ssl=shared \
$--enable-rewrite=shared \
$--enable-proxy=shared \
$--enable-deflate=shared \
$--with-mpm=worker \
$--prefix=/www/server/apache2
$make
$make install
#--enable-so 要使用 JK2 串連 Tomcat,必須讓 apache2 支援模組動態載入
#--enable-ssl=shared 支援 SSL
#--enable-rewrite=shared 支援 URL 重寫
#--enable-proxy=shared 支援代理\r
#--enable-deflate=shared 支援網頁壓縮\r
#--with-mpm=worker 採用worker的mpm方式運行
#--prefix=/www/server/apache2 安裝目錄
2.2 PHP
$tar vfxz php-4.3.2.tar.gz
$cd php-4.3.2
$./configure \
$--with-apxs2=/www/server/apache2/bin/apxs \
$--prefix=/www/server/php
$make
$make install
$cp php.ini-dist /usr/local/lib/php.ini
#--with-apxs2=/www/server/apache2/bin/apxs 作為 apache2 的模組編譯\r
#--prefix=/www/server/php 安裝目錄,並沒有太大作用
2.3 Tomcat
$tar vfxz jakarta-tomcat-4.1.24.tar.gz
$cp -R jakarta-tomcat-4.1.24 /www/server/
$ln -s /home/data/www/jakarta-tomcat-4.1.24 /home/data/www/tomcat
2.4 JTC
#編譯錯誤請參考下文處理\r
$lynx http://www.pubbitch.org/jboss/mod_jk2.html
$tar vfxz jakarta-tomcat-connectors-4.1.24-src.tar.gz
$cd jakarta-tomcat-connectors-4.1.24-src
#以下兩步如果沒有編譯錯誤不需要做\r
$cp /usr/java/j2sdk1.4.1_02/include/linux/jni_md.h /usr/java/j2sdk1.4.1_02/include/
$ln -s /www/server/apache2/lib/libapr-0.so /www/server/apache2/lib/libapr.so
#開始編譯準備
$cd util
$cp build.properties.sample build.properties
$cd ../jk
$cp build.properties.sample build.properties
$vi build.properties
catalina.home=/www/server/tomcat
tomcat40.home=${catalina.home}
apache2.home=/www/server/apache2
#修改以上內容
$cd ../coyote
$cp build.properties.sample build.properties
$vi build.properties
catalina.home=/www/server/tomcat
#修改以上內容
$cd ../jk
#編譯JK2
$ant native
$cp build/jk2/apache2/mod_jk2.so /www/server/apache2/modules
3. 配置
3.1 apache2/conf/httpd.conf
$vi /home/data/www/apache2/conf/httpd.conf
#禁用預設字元集
#AddDefaultCharset ISO-8859-1
#載入 JK2
LoadModule jk2_module modules/mod_jk2.so
#支援 PHP
AddType application/x-httpd-php .php
#支援頁面壓縮\r
SetOutputFilter DEFLATE
DeflateFilterNote ratio
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
#包含虛擬機器主機設定
Include conf/virtual_host.conf
3.2 apache2/conf/virtual_host.conf
$touch /www/server/apache2/conf/virtual_host.conf
$vi /www/server/apache2/conf/virtual_host.conf
NameVirtualHost 123.456.78.9:80
<VirtualHost 123.456.78.9:80>
DocumentRoot /www/host/www.domain.com/htdocs
ServerName www.domain.com
ServerAlias domain.com
ErrorLog /www/host/www.domain.com/logs/apache_error.log
CustomLog /www/host/www.domain.com/logs/apache_access.log common
</VirtualHost>
#反向 Proxy的虛擬機器主機\r
<VirtualHost 123.456.78.9:80>
ServerName www2.domain.com
RewriteEngine on
ProxyRequests off
UseCanonicalName off
RewriteRule ^/(.*)$ http://192.168.0.1/$1 [P,L]
</VirtualHost>
3.3 apache2/conf/ssl.conf
$vi /www/server/apache2/conf/ssl.conf
NameVirtualHost 123.456.78.9:443
<VirtualHost 123.456.78.9:443>
DocumentRoot "/www/host/www.domain.com/htdocs"
ServerName www.domain.com
ServerAdmin webmaster@domain.com
ErrorLog /www/host/www.domain.com/logs/ssl_error.log
TransferLog /www/host/www.domain.com/logs/ssl_access.log
SSLCertificateFile /www/host/www.domain.com/server.crt
SSLCertificateKeyFile /www/host/www.domain.com/server.key
SSLCertificateChainFile /www/ca.crt
CustomLog /www/host/www.domain.com/logs/ssl_request.log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
3.4 apache2/conf/workers2.properties
$touch /www/server/apache2/conf/workers2.properties
$vi /www/server/apache2/conf/workers2.properties
[shm]
file=${serverRoot}/logs/shm.file
size=1048576
# Example socket channel, override port and host.
[channel.socket:example]
port=8009
host=127.0.0.1
# define the worker
[ajp13:example]
channel=channel.socket:example
#[status:status]
# Uri mapping
#[uri:/jkstatus/*]
#worker=status:status
# Uri mapping
[uri:/*.jsp]
worker=ajp13:example
#[uri:/examples/*]
#worker=ajp13:example
3.5 tomcat/conf/jk2.properties
$touch /www/server/tomcat/conf/jk2.properties
$vi /www/server/tomcat/conf/jk2.properties
# The default port is 8009 but you can use another one
# channelSocket.port=8019
3.6 tomcat/conf/web.xml
$vi /www/server/tomcat/conf/web.xml
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
#此處重點,禁用目錄列表\r
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3.7 tomcat/conf/tomcat-users.xml
$vi /www/server/tomcat/conf/tomcat-users.xml
#增加tomcat的管理員
<user name="xxx" password="xxx" roles="manager" />
3.8 tomcat/conf/server.xml
$vi /www/server/tomcat/conf/server.xml
#設定虛擬機器主機\r
<Host name="www.domain.com"
debug="0"
appBase="/www/host/www.domain.com/webapps"
unpackWARs="true">
<Alias>crcchem.com</Alias>
<Logger className="org.apache.catalina.logger.FileLogger"
directory="/www/host/www.domain.com/logs"
prefix="catalina."
suffix=".log"
timestamp="true"/>
<Context path=""
docBase="/www/host/www.domain.com/htdocs"
debug="0"/>
</Host>
4. 測試
4.1 apache2
$touch /www/host/www.domain.com/htdocs/test.html
$vi /www/host/www.domain.com/htdocs/test.html
<h1>hello</h1>
$/www/server/apache2/bin/apachectl configtest
$/www/server/apache2/bin/apachectl startssl
$lynx http://www.domain.com/test.html
4.2 SSL
$lynx https://www.domain.com/test.html
4.3 反向 Proxy\r
$lynx http://www2.domain.com/
4.4 PHP
$touch /www/host/www.domain.com/htdocs/test.php
$vi /www/host/www.domain.com/htdocs/test.php
<?phpinfo();?>
$lynx http://www.domain.com/test.php
4.5 JSP
touch /www/host/www.domain.com/htdocs/test.jsp
$vi /www/host/www.domain.com/htdocs/test.jsp
<%=new java.util.Date();%>
$lynx http://www.domain.com/test.jsp