Linux平台下Web环境配置
Linux 平台下Web 环境搭建【前言】此文档是Linux 平台下Web 环境搭建的参考手册,基于Red Hat Enterprise Linux 5(32位) 环境,因此在64位环境下编译时若遇到
Linux 平台下Web 环境搭建
【前言】
此文档是Linux 平台下Web 环境搭建的参考手册,基于Red Hat Enterprise Linux 5(32位) 环境,因此在64位环境下编译时若遇到错误,请先参照附录一。各模块的说明如下: Apache 网站运行的平台。
PHP 服务器脚本
GD2 支持验证码的显示
OpenSSL 提供加密通道,即https 方式访问。
编译前请先熟悉Linux 下的基本操作,文件编辑操作可以参照附录三。由于Linux 下的Apache 与PHP 的配置文件与Windows 下的配置文件有很大差异,所以不要将Windows 下的配置文件直接拷贝到Linux 下。
一、准备工作
以下所有操作都以vcen 用户登录为示例,但命令的执行是以root 帐户身份,因此在打开终端后先切换用户方式。
分别创建以下目录,daemon 为编译apache 后自动创建的用户。 mkdir /home/vcen/download mkdir /home/vcen/download/apache2_2 mkdir /home/vcen/download/php5
mkdir /var/ins-g mkdir /var/sessionpath chown daemon:daemon /var/ins-g chmod o rw /var/ins-g
chown daemon:daemon /var/sessionpath
二、编译GD2包
2.1、下载相关编译包
在Linux 平台下可以直接在终端中输入以下命令进行下载。
cd /home/vcen/download



2.2、编译GD2包与GD2包所需的环境
一定要先编译过前面的四个包之后再去编译GD2包,不然的话会编译失败。所有的编译最好要普通用户下进行,只有当make install时才切换至root 用户。总之,只有当非用root 用户不可时才切换至root 用户,不要轻易的用root 用户来操作系统。
2.2.1、编译zlib 包 tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure make make install clean
64位下编译方法:
tar -zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure
vi Makefile
找到 CFLAGS=-O3 -DUSE_MMAP
在后面加入-fPIC ,即变成CFLAGS=-O3 -DUSE_MMAP -fPIC
接下面步骤
make
make install
如果编译的时候出现
/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libz.a: could not read symbols: Bad value
错误,解决办法参见附录一
2.2.2、编译libpng 包 tar zxvf libpng-1.2.34.tar.gz
cd libpng-1.2.34
./configure
make
make install clean
,2.2.3、编译freetype 包 tar zxvf freetype-2.3.5.tar.gz cd freetype-2.3.5
./configure make
make install clean
2.2.4、编译jpeg 包
tar zxvf jpegsrc.v6b.tar.gz cd jpeg-6b ./configure make
make install-lib
64位下编译: ./configure CFLAGS="-O3 -fPIC" make
make install-lib
如果在编译的时候出现
/usr/bin/ld: /usr/local/lib/libjpeg.a(jcapimin.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
错误,解决办法参见附一
2.2.5、编译GD2包
如果以上软件包都编译成功了,就可以开始编译GD2包了 tar zxvf gd-2.0.35.tar.gz cd gd-2.0.35 ./configure --with-png --with-freetype --with-jpeg make
make install
如果编译失败的话,先make clean一下,再make 。一般就可以编译过去了。
编译的时候,最后结果那里可以看到
** Configuration summary for gd 2.0.35:
Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
,Support for Fontconfig library: no
Support for Xpm library: no
Support for pthreads: yes
可以看到png 、 jpeg 、 freetype 都已经编译上了
三、编译Apache2.2
3.1、编译OpenSSL
3.1.1、下载OpenSSL
下载地址:http://openssl.org/source/openssl-0.9.8h.tar.gz cd /home//vcen/download
wget http://openssl.org/source/openssl-0.9.8h.tar.gz
3.1.2、编译OpenSSL tar zxvf openssl-0.9.8h.tar.gz
cd openssl-0.9.8h ./config
make make test
make install
3.1.3、创建证书
对于OpenSSL 的编译目标目录,在默认情况下为:/usr/local/ssl。 cd /usr/local/ssl/bin
生成server.key ,并要求输入key 的密码。1024表示长度: ./openssl genrsa -des3 -out server.key 1024
在出现: Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
输入两次口令,类似于密码。记清楚,一会要用的。
生成server.csr ,这时需要你输入在上一步时生成的密码 ./openssl req -new -key server.key -out server.csr
再次输入密码后,填写以下内容: For some fields there will be a default value,
If you enter '.', the field will be left blank.
{输入“. ”, 表示为空}
,-----
Country Name (2 letter code) [AU]:(国家) State or Province Name (full name) [Some-State]:(洲/省)
Locality Name (eg, city) []:(城/镇)
Organization Name (eg, company) [Internet Widgits Pty Ltd]:(组织名)
Organizational Unit Name (eg, section) []:(单位名)
Common Name (eg, YOUR name) []:(httpd-ssl.conf 中的ServerName 普通名称) (注意:当提示输入 Common Name (eg, YOUR name) []: ,请输入你需要SSL 支持的域名, 如localhost,blog.yening.cn ,否则浏览器提示证书错误.)
Email Address []:(邮箱)
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(csr 密码)
An optional company name []:(公司)
生成server.crt ,再次输入server.key 口令
./openssl x509 -req -days 730 -in server.csr -signkey server.key -out server.crt 至此,证书文件便完成了,默认在/usr/local/ssl/bin下会有server.csr,server.key,server.crt 三个文件。
3.2、编译Apache2.2
3.2.1、下载Apache2.2
最新版本2.2.11,放到/home/vcen/download/apache2_2目录下,download 目录为自己创建的目录。
下载地址:http://dev.xiaonei.com/apache-mirror/httpd/httpd-2.2.11.tar.gz
cd /home/vcen/download/apache2_2
wget http://dev.xiaonei.com/apache-mirror/httpd/httpd-2.2.11.tar.gz
3.2.2、编译Apache2.2
cd /home/vcen/download/apache2_2
tar zxvf httpd-2.2.11.tar.gz
cd httpd-2.2.11
./configure --prefix=/usr/local/apache2 --enable-so --enable-auth-digest --enable-rewrite --enable-setenvif --enable-mime --enable-ssl --with-ssl=/usr/local/ssl --enable-headers
make
make install clean
,四、编译PHP5
4.1、下载PHP5
最新版本php5.2.8,放到/home/vcen/download/php5目录下。
下载地址:http://cn2.php.net/distributions/php-5.2.8.tar.gz
cd /home/vcen/download/php5

4.2、编译PHP5
cd /home/vcen/download/php5 tar zxvf php-5.2.8.tar.gz
cd php-5.2.8
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-jpeg --with-freetype --enable-trace-vars --with-zlib --with-gd --enable-sockets make
make install clean
五、配置Apache(/usr/local/apache2/conf/httpd.conf)
5.1、端口侦听
Listen 80 监听端口,即URL 访问时的端口。
5.2、设置网站目录
DocumentRoot “/var/ins-g” 页面所存放的目录,一定要使用/。
修改
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Order deny,allow为Order all,deny,
,Options Indexes FllowSymLinks为Options None。
注:一定要设置
5.3、关闭访问日志写。
找到以下几行,在前面添加#
LogFormat "h l u t "r" >s b "{Referer}i" "{User-Agent}i"" combined
LogFormat "h l u t "r" >s b" common
LogFormat "h l u t "r" >s b "{Referer}i" "{User-Agent}i" I O" combinedio
CustomLog "logs/access_log" common
5.4、设置默认访问页
在DirectoryIndex 后面添加要默认访问的页面,如:login.php index.php ,多文件用空格隔开。
5.5、支持PHP 解析
找到AddType application/x-gzip .gz .tgz在下面添加如下两行:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html
5.6、配置SSL
5.6.1、配置SSL 基本信息
对于SSL 的支持,在httpd.conf 中无须多配,只需要将httpd-conf 中的
Include conf/extra/httpd-ssl.conf
这一行的注释去掉。
然后将OpenSSL 生成的证书文件: server.crt, server.key 拷贝到/usr/local/apache2/conf下。
此时如果启动apache server,会要求你输入SSL 的密码,并且每次Apache 启动的时候,都会要求你输入SSL 的密码.
如果想每次启动时不需要输入SSL 密码,则必须创建SSL 密码自动应答文件。 创建SSL 密码自动应答文件
创建 /usr/local/apache2/conf/sendsslpwd ,内容如下:
#!/bin/bash
SSLpasswd=”你的证书文件密码“
echo $SSLpasswd
,修改文件属性:chmod 755 /usr/local/apache2/conf/sendsslpwd
5.6.2、配置httpd-ssl.conf 文件 cd /usr/local/apache2/conf/extra
vi httpd-ssl.conf
找到 SSLPassPhraseDialog builtin 这一行,将其注释掉
加入 SSLPassPhraseDialog exec:/usr/local/apache2/conf/sendsslpwd 这样,每次启动将不会提示输入SSL 密码了
另外,请修改
按照apache 配置文件中的配置修改如下信息:
DocumentRoot "/usr/local/apache2/htdocs"
ServerName www.example.com:443
ServerAdmin you@example.com
六、配置PHP(/etc/php.ini)
6.1、上传文件相关的配置
表单提交时的最大数据:post_max_size=8M改成200M
上传文件的最大值:upload_max_filesize=20M修改为200M
PHP 文件的最大执行时间:max_execution_time=30修改为300
PHP 处理提交参数最大时间 max_input_time=60修改为600
PHP 消耗内存限制memory_limit=128M
6.2、全局变量限制
register_globals = On 打开/关闭,应打开(On)
6.3、关闭自动转义
将magic_quotes_gpc=On修改为Off
6.4、PHP 默认编码.
default_charset = "iso-8859-1"
修改为gb18030. 若不存在则添加此行.
,6.5、session 的保存路径
去掉;session.save_path = "/tmp"前面的分号,如:将路径设置为/var/sessionpath
6.6、session 的最大超时时间
将session.use_cookies设置为1,打开cookie 储存sessionid ,不过默认就是1 将session.cookie_lifetime改为86400,即24小时
将session.cache_expire改为86400,与上面的相同.
6.7、关闭页面的错误输出
只有当产品发布时,服务器端的配置才更新这一条。
将display_errors设置为On 。
七、启动Apache
输入如下指令启动Apache :
/usr/local/apache2/bin/apachectl start
由于Apache 服务要在每次机器重机即运行。则需要配置一下启用项:
在文件:/et/rc.d/rc.local最后添加如下语句:
/usr/local/apache2/bin/apachectl start
八、测试
在/var/ins-g目录下新建一PHP 文件(index.php)。若正常会显示PHP 信息。代码如下:
phpinfo();
?>
附录一:
错误提示:
/usr/bin/ld: /usr/local/lib/libjpeg.a(jcapimin.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
进入Jpeg 目录
,./configure CFLAGS="-O3 -fPIC"
make
make install-lib
编译前需指定为64位编译模式,否则会出现以下错误:
/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32against `a local symbol' can not be used when making a shared object;recompile with -fPIC
/usr/local/lib/libz.a: could not read symbols: Bad value
解决办法 : 重新安装 zlib-1.2.3.tar.gz
tar -zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure
vi Makefile
找到 CFLAGS=-O3 -DUSE_MMAP
在后面加入-fPIC ,即变成CFLAGS=-O3 -DUSE_MMAP -fPIC
接下面步骤
make
make install
附录二:
编译SSL 的参考:
附录三:
所有的包编译、文件编辑等等操作都是在root 用户下进行。
常用命令:
su 切换root 帐户命令
cd 切换目录
mkdir 新建文件夹
mv 移动文件或文件夹
cp 拷贝文件或文件夹
chown 修改文件或文件夹的所有者
chmod 修改文件或文件夹的权限