服务器篇--之 LAMP安装手记
Server OS: Cent OS
Apache: http-2.2.11
PHP:PHP-5.2.9
Mysql: 5.1.32
GD: gd-2.0.35
JPEG:jpeg-6b
libpng:libpng-1.2.35
libxml2:libxml2-2.6.19
freetype:freetype-2.3.9
ZendOptimizer:ZendOptimizer-3.3.0a-linux-glibc21-i386
记得安装Linux的时候不要安装系统默认的apache,mysql和php以及相关的软件。已经安装的请用rpm -e * 删除已经安装的包。
一,安装apache2
(这里我把软件包都放/root下了)
# cd /root
进入目录/root
# tar -zvxf httpd-2.2.11.tar.gz
解压httpd-2.2.11.tar.gz
# cd httpd-2.2.11
进入httpd-2.2.11目录
# mkdir -p /usr/local/apache2
生成/usr/local/apache2目录
# ./configure --prefix=/usr/local/apache2
或者
# ./configure --prefix=/usr/local/apache --enable-modules=so --enable-rewrite
配置是否支持重写
# make
编译
# make install
安装apache
# vi /usr/local/apache2/conf/httpd.conf
设置DocumentRoot 的路径
# /usr/local/apache2/bin/apachectl -k stop
停止apache
# /usr/local/apache/bin/apachectl restart
重启Apache
把httpd 添加到系统的启动项里
# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
# chmod 700 /etc/rc.d/init.d/httpd
# vi /etc/rc.d/init.d/httpd
在第三行后增加
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 90
# description:http server
注意:没有这几行,在使用chkconfig时会提示你:service httpd does not support chkconfig。
chkconfig –add httpd
chkconfig –level 345 httpd on
这样,在运行级别345上httpd就可以自动启动了。
二,Mysql的安装
# cd /root
# tar -zvxf mysql-5.1.32-linux-i686-glibc23.tar.gz
# mkdir -p /usr/local/mysql (省略也可)
# cp -r mysql-5.1.32-linux-i686-glibc23/* /usr/local/mysql
# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
添加mysql用户及用户组
# groupadd mysql
# useradd -g mysql mysql
修改mysql目录权限
# chown -R root /usr/local/mysql
# chgrp -R mysql /usr/local/mysql
# chown -R mysql /usr/local/mysql/data
生成mysql系统数据库
# /usr/local/mysql/scripts/mysql_install_db --user=mysql&
启动mysql服务
# /usr/local/mysql/bin/mysqld_safe --user=mysql&
如出现 Starting mysqld daemon with databases from /usr/local/mysql/data
代表正常启动mysql服务了, 按Ctrl + C 跳出
修改 mysql 的 root 密码
# /usr/local/mysql/bin/mysqladmin -u root -p password "wengdo"
wengdo 这里可以换为你自己的密码
把你编译目录的一个脚本COPY过去
# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
用ntsysv设置使mysql每次启动都能自动运行。
好了,至此mysql安装完毕,你可以这样起动你的mysql服务
# /etc/rc.d/init.d/mysqld start
下面这步比较关键,
# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
大家可以不做这步,大可以在编译其他软件的时候自定义myslq的库文件路径,但我还是喜欢把库文件链接到默认的位置,这样你在编译类似PHP,Vpopmail等软件时可以不用指定mysql的库文件地址。
三,安装GD库 (如果你用系统自带的GD库,可以省略这一步)
建立目录:
# mkdir -p /usr/local/jpeg6
# mkdir -p /usr/local/jpeg6/bin
# mkdir -p /usr/local/jpeg6/lib
# mkdir -p /usr/local/jpeg6/include
# mkdir -p /usr/local/jpeg6/man
# mkdir -p /usr/local/jpeg6/man1
# mkdir -p /usr/local/jpeg6/man/man1
# cd /root
# tar -zvxf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static
# make
# make install
- 安装libpng
# cd /root
# tar -zvxf libpng-1.2.35.tar.gz
# cd libpng-1.2.35
# cp scripts/makefile.std makefile
# make
# make install
- 安装 freetype
# cd /root
# tar -zvxf freetype-2.3.9.tar.gz
# cd freetype-2.3.9
# mkdir -p /usr/local/freetype
# ./configure --prefix=/usr/local/freetype
# make
# make install
# cd /root
# tar -zvxf gd-2.0.35.tar.gz
# mkdir -p /usr/local/gd2
# cd gd-2.0.35
# ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png=/usr/local/lib/ --with-zlib=/usr/local/lib/ --with-freetype=/usr/local/freetype/ --enable-m4_pattern_allow
# make
# make install
四,安装PHP5
php5必须有libxml2支持!
安装libxml2
# cd /root
# tar -zxvf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/local/libxml2
# ./configure --prefix=/usr/local/libxml2
# make
# make install
安装php5
# cd /root
# tar -zvxf php-5.2.9.tar.gz
# mkdir -p /usr/local/php
# cd php-5.2.9
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
--with-mysql=/usr/local/mysql/
--with-curl=/usr/local/curl --enable-ftp
--with-libxml-dir=/usr/local/libxml2
--with-expat-dir=/usr/lib --enable-soap
--with-xsl=/usr/local/libxslt --enable-xslt
--with-gd=/usr/local/gd2/
--with-jpeg-dir=/usr/local/jpeg6/
--with-zlib-dir=/usr/local/lib/
--with-png=/usr/local/lib/
--with-freetype-dir=/usr/local/freetype/
# make
# make install
# cp php.ini-dist /usr/local/php/lib/php.ini (别忘记了)
其中./configure 后的
--prefix=/usr/local/php
--with-apxs2=/usr/local/apache2/bin/apxs
--with-mysql=/usr/local/mysql/
--with-libxml-dir=/usr/local/libxml2
是必要的选项
--with-gd=/usr/local/gd2/
--with-jpeg-dir=/usr/local/jpeg6/
--with-png=/usr/local/lib
--with-zlib-dir=/usr/lib
--with-freetype-dir=/usr/local/freetype
这是让PHP支持GD库的配置选项
--with-curl=/usr/local/curl 支持CURL库
--enable-ftp 打开FTP库函数支持
--enable-soap --with-xsl=/usr/local/libxslt --enable-xslt
让PHP支持SOAP, 上面这些一般用得少, 可以去掉
如果服务器自带 GD库及其相关的库,可以用下面这个方式去配置PHP
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-freetype-dir=/usr --with-png-dir=/usr --with-gd --enable-gd-native-ttf --with-ttf --with-gdbm --with-gettext --with-iconv --with-jpeg-dir=/usr --with-png --with-zlib --with-xml --enable-calendar --with-apxs=/usr/local/apache2/bin/apxs
五,安装 ZendOptimizer
# cd /root
# tar -zxvf ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
# cd ZendOptimizer-3.3.0a-linux-glibc21-i386
# ./install.sh
然后出现图形界面,按提示Next
六,配置一些信息
配置 httpd.conf 让apache支持PHP
# vi /usr/local/apache2/conf/httpd.conf
找到 AddType application/x-gzip .gz .tgz 在其下添加如下内容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
重启apache
# /usr/local/apache2/bin/apachectl restart
在你设置的目录里建一内容为 <? phpinfo(); ?> PHP文件, 输入URL地址查看PHP配置是否正确
安装时可能会出现的一些问题
编译最新的GD库2.0.35版本,用默认的 ./configure编译
当make的时候,出现以下错误
configure.ac:64: error: possibly undefined macro: AM_ICONV
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
make: *** [configure] Error 1
解决方法:
编译加m4_pattern_allow参数,上面已添加
# ./configure --enable-m4_pattern_allow
数据库可能会出现这样的错误
"Can't connect to local MySQL server through socket '/tmp/mysql.sock'"
检查是否存在那个socket文件,可以在/etc/my.cnf 中指定 socket = /tmp/mysql.sock
使用.mysqld_safe --socket=/tmp/mysql.sock & 启动mysql进程,
有时pam_mysql使用/var/lib/mysql/mysql.sock
所以做个连接 ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
或者是修改/etc/my.cnf
有可能是权限问题,看看mysql的错误日志,一步一步调整
重启Apache出错
重新启动apache报如下错误:
httpd: Syntax error on line 53 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
原因:是Linux有一个SELinux保护模式引起的。
解决办法:
1关闭SELINUX的方法:
# vi /etc/selinux/config
将SELINUX=enforcing 改成SELINUX=disabled 需要重启
这个方法可能会对服务器带来风险。
2不关闭SELINUX的方法:
# setenforce 0
# chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache2/modules/libphp5.so
# service httpd restart
# setenforce 1
PHP 在Make 或者Make install的时候出错,报错信息:
Installing PEAR environment: /usr/local/lib/php/
/usr/local/php-5.2.5/sapi/cli/php: error while loading shared libraries: /usr/local/gd2/lib/libgd.so.2: cannot restore segment prot after reloc: Permission denied
make[1]: *** [install-pear-installer] Error 127
make: *** [install-pear] Error 2
安装出错,这时候输入 tail /var/log/messages 查看错误日志
# tail /var/log/messages
...
Mar 3 06:40:46 localhost setroubleshoot: SELinux is preventing /usr/local/php-5.2.5/sapi/cli/php from loading /usr/local/gd2/lib/libgd.so.2.0.0 which requires text relocation. For complete SELinux messages. run sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846
你可以找到 run sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846,执行它
# sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846
...
The following command will allow this access:
chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
找到 following command chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
# chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
再次运行make install 或make
忘了mysql的root口令怎么办
# /usr/local/mysql/support-files/mysql.server stop
# mysqld_safe --skip-grant-tables &
# mysqladmin -u user password 'newpassword'
# mysqladmin flush-privileges