前面有几篇文章是讲了在linux下面搭建lamp或者lnmp环境的,我们大都是使用借助repo使用yum或者使用rpm抑或者使用二进制包来安装,这样方便快捷,省时省力,但是有时候不够灵活,目前大多数的*unix和linx等都已经默认集成了php、mysql以及apache等,但是往往在版本和模块配置上不能满足我们的需求,编译安装会解决此问题。当然了,编译安装会遇到很多的软件包的冲突问题,不过不用怕,遇到问题一个一个解决,编译结束,你就又对linux理解深入一微步。此文章发出时,nginx最新版本为nginx1.5.2,php为php5.5.1(这里指的是使用版本,php6早就出了,但是普及使用还还需要一段时间),mysql为mysql5.6.0。
首先安装一下一些编译php以及mysql需要的依赖库文件[shell]
yum install wget
yum install pcre
yum install openssl*
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make
yum -y install gd gd2 gd-devel gd2-devel[/shell]
[shell]
ulimit -SHn 65535[/shell]
那么接下来我们开始了,首先是编译安装nginx,首先我们要下载一个nginx安装依赖的类库包pcre:[shell]
tar -zxv -f pcre-version.tar.gz
cd pcre-version/
./configure
make && make install
cd ../[/shell]
okay,接下来我们去下载nginx的最新版本了,guys[shell]
#创建用户组,以及虚拟主机目录
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www[/shell][shell]
#以下可等安装完nginx再做,或者将虚拟主机的目录换成其他的
mkdir -p /usr/local/nginx/html/test
chmod +w /usr/local/nginx/html/test
chown -R www:www /usr/local/nginx/html/test[/shell]
#编译安装[shell]
tar -zxv -f nginx-0.8.46.tar.gz
cd nginx-0.8.46/
./configure –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
cd ../[/shell]
okay,nginx就这么轻松的安装啦。
编译过程中可能会报错[shell]
HTTP SSL module requires OpenSSL library[/shell]
解决方法[shell]
yum install openssl-devel[/shell]
启动nginx如果报错[shell]
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory[/shell]
那么解决办法如下[shell]
64bit
ln -s /usr/local/lib/libpcre.so.1 /lib64
32bit
ln -s /usr/local/lib/libpcre.so.1 /lib[/shell]
接下来我们安装mysql(一般来说,我们都是先安装mysql,再安装php的),首先去下载mysql的新版本。mysql老版本都是使用make进行编译,新版本(从mysql5.5之后的版本)使用cmake。首先我们需要安装cmake编译器。[shell]
tar -zxv -f cmake-version.tar.gz
cd cmake-version
./bootstrap
gmake
gmake install
cd ../[/shell]
接下来进入mysql的安装[shell]
#建立帐户
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql[/shell]
[shell]tar -zxv -f mysql-5.6.13.tar.gz
cd mysql-5.6.13/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/var/mysql/data[/shell]
编译过程中可能遇到错误[shell]
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:118 (FIND_CURSES)
cmake/readline.cmake:214 (MYSQL_USE_BUNDLED_READLINE)
CMakeLists.txt:257 (MYSQL_CHECK_READLINE)[/shell]
解决方法[shell]
rm -f CMakeCache.txt
yum -y install ncurses-devel[/shell]
可能会报错[shell]
CMake Error at cmake/bison.cmake:78 (MESSAGE):
Bison (GNU parser generator) is required to build MySQL.Please install
bison.
Call Stack (most recent call first):
sql/CMakeLists.txt:189 (RUN_BISON)[/shell]
解决方法[shell]
yum install bison[/shell]
确认无误安装[shell]
make
make install
[/shell][shell]
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
mkdir -p /var/mysql/
mkdir -p /var/mysql/data/
mkdir -p /var/mysql/log/
chown -R mysql:mysql /var/mysql/
[/shell]
注意-DWITH_EXTRA_CHARSETS:STRING=all中all最好为小写,这是设置支持所有字符集,在编译Percona时,需要指定-DWITH_EXTRA_CHARSETS:STRING为小写的all,才能编译所有的字符集。
可以这样查看mysql支持的字符集:[shell]
SHOW VARIABLES LIKE ‘character_set_%’;
SHOW VARIABLES LIKE ‘collation_%’;[/shell]
如果不支持某个字符集,会报错如下:[shell]
ERROR 1115 (42000): Unknown character set: ‘gbk'[/shell]
然后:[shell]
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod u+x /etc/init.d/mysqld
[/shell]
接下来我们要初始化安装,当然你可以先修改一下配置文件,vi /var/mysql/my.cnf。[shell]
/usr/local/mysql/scripts/mysql_install_db \
–basedir=/usr/local/mysql \
–datadir=/var/mysql/data \
–user=mysql[/shell]
最后编译安装结束我们创建配置文件:[shell]
cp support-files/my.cnf /etc/my.cnf //copy配置文件 [/shell]
配置文件的位置还可以是其他的位置,例如/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ,你可以使用以下命令查看mysql查找配置文件的顺序,如果有多个配置文件,一最后一个的定义为准[shell]
/usr/local/mysql/bin/mysql –help | grep my.cnf[/shell]
当然你会看到如下的信息,这就是mysql查找配置文件的先后顺序:[shell]
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf [/shell]
mysql不像oracle,如果没有或者找不到配置文件,oracle会报错,mysql不会,他会按照你编译时候的默认参数加载。
okay,此刻你可以启动你的mysql了[shell]
/etc/init.d/mysqld start[/shell]
当然还可以这样启动mysql的实例:[shell]
/usr/local/mysql/bin/mysqld_safe&[/shell]
这个启动方式是在unix下推荐的启动方式,这样子会启动一些安全项相关的东西,并且在遇到错误时候自动重启,记录运行时错误到日志文件。
那么你可以将其加入开机启动项[shell]
chmod +x /etc/init.d/mysqld
vi /etc/init.d/mysqld[/shell]
编辑如下变量[shell]
basedir=/usr/local/mysql
datadir=/var/mysql/data
chkconfig –add mysqld
chkconfig –level 345 mysqld on[/shell]
自启动mysql还有如下方法(建议)[shell]
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql //添加到启动
cd /etc
ln -s rc.d/init.d .
chkconfig –add mysql //添加运行权限
chkconfig –level 345 mysql on
/bin/sh -c ‘cd /usr/local/mysql; ./bin/mysqld_safe –user=mysql &'[/shell]
将上面的写入[shell]
/etc/rc.d/rc.local[/shell]
对于mysql的登录也可以建立一个软连接,这样我们便可以任何地方使用mysql[shell]
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql[/shell]
接下来鸡冻人心的时刻到啦,我想你一直在使用php5.3.x甚至是php5.x.y(x <= 2),那么php5.5.1版本你应该只是听过吧,这个版本还是有很多改进的,其中就包括fpm的支持(这个fpm在php5.3.3就开始内置了),废话不多说,直接去官网php.net下载(话说这php官网这么多年以来首次改版新网站,可以去瞄一眼)。php编译需要依赖很多的库和文件,我们刚上来就安装了一些,如果不满足,遇到缺少的包继续安装即可,解压之后进入php文件目录,至于编译参数你可以使用这个./configure --help来查看
安装之前可以先来手动安装一下php需要的类库[shell]
mkdir -p /data/apps/libs/
wget http://www.ijg.org/files/jpegsrc.v9.tar.gz
tar zxvf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure --prefix=/data/apps/libs --enable-shared --enable-static --prefix=/data/apps/libs
make
make install
cd ../
wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz
tar zxvf libpng-1.6.2.tar.gz
cd libpng-1.6.2/
./configure --prefix=/data/apps/libs
make
make install
cd ../
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
tar zxvf freetype-2.4.12.tar.gz
cd freetype-2.4.12/
./configure --prefix=/data/apps/libs
make
make install
cd ../
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz"
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz"
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/data/apps/libs
make
make install
cd libltdl/
./configure --prefix=/data/apps/libs --enable-ltdl-install
make
make install
cd ../../
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/data/apps/libs
make
make install
cd ../[/shell]
[shell]
vi /etc/ld.so.conf[/shell]
添加[shell]
/data/apps/libs/lib[/shell]
然后[shell]
ldconfig[/shell]
[shell]
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
export LDFLAGS="-L/data/apps/libs/lib -L/usr/lib"
export CFLAGS="-I/data/apps/libs/include -I/usr/include"
touch malloc.h
./configure --prefix=/data/apps/libs --with-libmcrypt-prefix=/data/apps/libs
make
make install
cd ../[/shell]
[shell]
cd php.5.x
export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"[/shell]
[shell]
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/data/apps/libs --with-jpeg-dir=/data/apps/libs --with-png-dir=/data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts --enable-opcache=no --without-pear
[/shell]
这里可以结合另一篇文章apache和nginx做负载均衡的文章,使用apache运行php,将php作为module安装,这时候就要加一个编译参数[shell]
–with-apxs2=/usr/local/apache/bin/apxs[/shell]
这样的话在apache的配置文件中会自动写入对php支持[shell]
LoadModule php5_module modules/libphp5.so[/shell]
然后再配置文件中找到AddType application/x-gzip .gz .tgz,在下面添加:[shell]
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps[/shell]
这样前端nginx作为http反向代理,apache处理php。
编译过程可能报错[shell]
Cannot find ldap libraries in /usr/lib.[/shell]
解决方法(64位机器)[shell]
cp -frp /usr/lib64/libldap* /usr/lib/[/shell]
然后安装[shell]
make
make install[/shell]
这时候需要将php源码文件中php.ini文件复制到相应的目录[shell]
cp /php-5.5.1/php.ini-development /usr/local/php/lib/php.ini[/shell]
当然这个配置文件的目录你可以在编译的时候指定[shell]
–with-config-file-path=/usr/local/php/etc[/shell]
建议安装的时候指定,如果你不指定就要通过phpinfo或者/usr/local/php/bin/php -i | grep php.ini来查看配置文件的路径,并将这个配置文件复制进去。一般来说编译时候不配置,默认会配置为/usr/local/php/lib这个路径,当然你要手动复制php.ini到这里,否则这个文件夹是不会有这个文件的。修改php.ini文件最好重启nginx,pph-fpm或者两者。
安装结束之后配置一下fpm,首先建立其配置文件[shell]
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf[/shell]
修改[shell]
user = nobody
group = nobody[/shell]
为[shell]
user = www
group = www[/shell]
将;pid = run/php-fpm.pid前的;去掉并修改为[shell]
pid = /usr/local/php/var/run/php-fpm.pid[/shell]
启动php-fpm[shell]
/usr/local/php/sbin/php-fpm[/shell]
然后将nginx和php加入自启动
将Nginx与fpm加入自启动[shell]
vi /etc/rc.local
#输入
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm
/usr/local/webserver/nginx/sbin/nginx[/shell]
那么我们安装一个php的模块pdo_mysql,这个模块很有用。如果你已经编译进去了就不必这样外挂模块了。
首先去http://pecl.php.net下载pdo_mysql包,然后解压[shell]
tar -zxv -f PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2
#然后运行phpize,这个phpize我们后面有文章讲到,可以去看看,是个为php动态挂在模块的。
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql/
make
make install[/shell]
在make的时候可能会报错[shell]
make: *** [pdo_mysql.lo][/shell]
这是因为找不到mysql的头文件,可以将头文件做个软连接[shell]
ln -s /usr/local/mysql/include/mysql/* /usr/local/include/[/shell]
重启nginx,php-fpm,mysql
安装结束之后会提示一个路径到终端显示:[shell]
/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/[/shell]
这时候在php.ini中查找; extension_dir = “ext”这一行,在其后加上:[shell]
extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/pdo_mysql.so"[/shell]
或者[shell]
extension = "pdo_mysql.so"[/shell]
重启nginx,mysql,php-fpm.然后使用以下命令查看模块或者使用phpinfo页查看[shell]
/usr/local/php/bin/php -m[/shell]
至于怎么配置nginx解析php,以及nginx配置虚拟主机可以查看另一篇文章:centos下搭建nginx,php(fastcgi),mysql开发环境
至于安装zend optimizer和PHP accelerator可以自行决定,但是对于php5.3.3以后的版本zend optimizer已经变成了zen guard loader可以去官网下载安装,至于PHP accelerator,这家伙更新貌似已经死了,而且和新版php兼容不好,如果非得使用优化软件,可以转向其他的,例如xcache等。
最后有个补充问题,是关于percona的,percona也许有些人陌生,其实这是mysql的一个变种,这个percona数据库针对innodb引擎进行了优化,对mysql很多特性做了改良,性能优于mysql,该版本提升了在高负载情况下的 InnoDB 的性能、为 DBA 提供一些非常有用的性能诊断工具,我已经使用了pt,编译安装方式与mysql完全一样。如果你安装过mysql,这个时候你可以先kiil掉mysql进程,删除掉安装文件(linux下这些编译安装文件类似于win的绿色软件),然后开始编译安装percona,最后别忘了重新编译一下php即可。
安装zlib扩展,当然如果我们编译的时候已经安装了就不必了,如果没有的话就来安装一下:
首先进入源码的扩展目录,就是你编译安装的时候使用的源码包,如果没有重新下载一个相同版本的,建议装完之后不要删掉源码包[shell]
cd php-5.5.1/ext/zlib/[/shell]
然后运行[shell]
/usr/local/php/bin/phpize[/shell]
此时可能会报错类似“Cannot find config.m4”
此时正确修改一下配置文件:[shell]
mv config0.m4 config.m4[/shell]
再次运行[shell]
/usr/local/php/bin/phpize[/shell]
然后开始编译:[shell]
./configure –with-zlib –with-php-config=/usr/local/php/bin/php-config
make
make install[/shell]
安装结束会出现:[shell]
Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/[/shell]
这时候编辑php.ini文件,找到extension_dir = “ext”,在其他扩展行后面添加[shell]
extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/zlib.so"[/shell]
重启php-fpm即可。
在这里给出一些错误提示的通用解决办法
例如在编译php的时候如果报出这种错误
[shell]
error while loading shared libraries: xxx.so.x"
[/shell]
原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib*.so.*文件)或者共享库版本不对, 遇到这种情况那就去网上下载并安装上即可
另外一个原因就是已经安装了该共享库, 但执行需要调用该共享库的程序的时候, 程序按照默认共享库路径找不到该共享库文件
以安装共享库后要注意共享库路径设置问题, 如下:
1) 如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令
ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.
2) 如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它”非/lib或/usr/lib”目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:
[shell]
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig[/shell]
3) 如果共享库文件安装到了其它”非/lib或/usr/lib” 目录下, 但是又不想在/etc/ld.so.conf中加路径(或者是没有权限加路径). 那可以export一个全局变量LD_LIBRARY_PATH, 然后运行程序的时候就会去这个目录中找共享库.
LD_LIBRARY_PATH的意思是告诉loader在哪些目录中可以找到共享库. 可以设置多个搜索目录, 这些目录之间用冒号分隔开. 比如安装了一个mysql到/usr/local/mysql目录下, 其中有一大堆库文件在/usr/local/mysql/lib下面, 则可以在.bashrc或.bash_profile或shell里加入以下语句即可:
[shell]
export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
[/shell]
一般来讲这只是一种临时的解决方案, 在没有权限或临时需要的时候使用.
4)如果程序需要的库文件比系统目前存在的村文件版本低,可以做一个链接
比如:[shell]
error while loading shared libraries: libncurses.so.4: cannot open shared
object file: No such file or directory[/shell]
[shell]
ls /usr/lib/libncu*
/usr/lib/libncurses.a /usr/lib/libncurses.so.5
/usr/lib/libncurses.so /usr/lib/libncurses.so.5.3[/shell]
可见虽然没有libncurses.so.4,但有libncurses.so.5,是可以向下兼容的
建一个链接就好了[shell]
ln -s /usr/lib/libncurses.so.5.3 /usr/lib/libncurses.so.4[/shell]