CentOS Linux VPS编译安装LNMP

LNMP表示使用Linux+Nginx+MySQL+PHP搭建的一个web环境。

使用的系统及软件版本:
CentOS 5.6 32bit
mysql 5.1.56
php 5.2.17
nginx 0.8.55

安装相关组件:

yum install patch make gcc gcc-c++ gcc-g77 flex bison tar libtool libtool-libs kernel-devel autoconf213 libjpeg libjpeg-devel libpng libpng-devel libtiff libtiff-devel gettext gettext-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel file glib2 glib2-devel bzip2 diff* openldap-devel bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel zip unzip tcl-devel ghostscript-devel fontconfig-devel libwmf-devel libc-client-devel


没有安装相关组件的话,编译时会提示缺少函数库。

yum check-update
yum update

一、安装mysql:
mysql现在有5.1和5.5两个版本,在架构上有很大的不同,由于5.5较新,这里安装5.1版本。进入mysql官网http://dev.mysql.com/downloads/,点击Mirrors,选择一个美国地区的镜像下载点。

1、开始安装,选择目前5.1的最新版本:

cd /tmp
wget -c ftp://mysql.llarian.net/pub/mysql/Downloads/MySQL-5.1/mysql-5.1.56.tar.gz
tar -zxvf mysql-5.1.56.tar.gz
cd mysql-5.1.56
./configure –prefix=/usr/local/mysql –with-extra-charsets=all –enable-thread-safe-client –enable-assembler –with-charset=utf8 –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile
make
make install

2、建立mysql用户:

groupadd mysql
useradd -g mysql mysql

3、复制配置文件my.cnf:

cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

4、安装用户数据库和表文件:

/usr/local/mysql/bin/mysql_install_db –user=mysql

5、设置目录权限:

chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.

6、复制mysql的服务启动脚本到init.d目录下:

cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld

7、设置mysql开机自启动:

chmod 755 /etc/init.d/mysqld
chkconfig –level 345 mysqld on

8、启动mysql,修改root密码:

service mysqld start
/usr/local/mysql/bin/mysqladmin -u root password ‘new-password’
service mysqld restart

9、为mysql命令建立软链接:

ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

以后进入mysql可使用:

mysql -u root -p //指定登陆用户并会提示输入密码

10、将mysql的动态函数库加载到高速缓存(cache)中:

echo “/usr/local/mysql/lib/mysql” >> /etc/ld.so.conf
echo “/usr/local/lib” >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysqld restart

11、相关文件位置:
mysql主目录/usr/local/mysql
mysql执行档/usr/local/mysql/bin/mysql
数据库位于/usr/local/mysql/var
配置文件为/etc/my.cnf
mysql日志与数据库在相同目录下,命名类似:{mysql_data}/{server_name}-bin.00000X,这样的日志不能直接查看,可以通过mysqlbinlog命令转换成我们识别的格式。
cd /usr/local/mysql/var
/usr/local/mysql/bin/mysqlbinlog mysql-bin.000003 > mysqllog.log

二、安装PHP:
这里使用fastcgi模式安装PHP。

1、安装PHP相应lib组件,安装顺序不能颠倒,因为有依赖性:

cd /tmp
wget -c http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar -zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure –prefix=/usr
make
make install

cd /tmp
wget -c http://monkey.org/~provos/libevent-2.0.12-stable.tar.gz
tar -zxvf libevent-2.0.12-stable.tar.gz
cd libevent-2.0.12-stable
./configure –prefix=/usr
make
make install

cd /tmp
wget -c http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure –prefix=/usr
make
make install

cd /tmp
wget -c http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure –prefix=/usr
make
make install

cd /tmp
wget -c http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure –prefix=/usr
make
make install

安装GD图形库(可选),一般都会装:

cd /tmp
wget -c http://google-desktop-for-linux-mirror.googlecode.com/files/gd-2.0.35.tar.gz
tar -xzvf gd-2.0.35.tar.gz
cd gd
cd 2.0.35
./configure –prefix=/usr –mandir=/usr/share/man –with-jpeg –with-png –with-freetype –with-zlib –with-fontconfig
make
make install

2、下载PHP源码安装:
需要下载两个包,其中php-5.2.17-fpm-0.5.14.diff.gz是用来给php打补丁的,这样才能编译出fastcgi。

cd /tmp
wget -c http://us.php.net/distributions/php-5.2.17.tar.gz
wget -c http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar -zxvf php-5.2.17.tar.gz
zcat php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure –prefix=/usr/local/php –with-config-file-path=/etc –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir –enable-xml –enable-magic-quotes –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-fpm –enable-force-cgi-redirect –enable-mbstring –with-mcrypt –enable-ftp –with-gd –enable-gd-native-ttf –with-openssl –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –enable-soap –with-gettext –with-mime-magic –enable-exif –with-imap –with-kerberos –with-imap-ssl
make ZEND_EXTRA_LIBS=’-liconv’
make install
cp php.ini-recommended /etc/php.ini

3、配置php-fpm:

useradd www
vim /usr/local/php/etc/php-fpm.conf

找到Unix user of processes去掉注释,修改成如下:

Unix user of processes
<value name=”user”>www</value>
Unix group of processes
<value name=”group”>www</value>

找到listen_address,修改如下:

<value name=”listen_address”>/tmp/php-fcgi.sock</value>

4、启动php-fpm进程,设置开机自启动:

/usr/local/php/sbin/php-fpm start
echo “/usr/local/php/sbin/php-fpm start” >> /etc/rc.d/rc.local

5、相关文件位置:
php安装目录/usr/local/php
php配置文件/etc/php.ini
php-fpm配置文件/usr/local/php/etc/php-fpm.conf

三、安装Nginx
1、安装所需的PCRE库:

cd /tmp
wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
tar -zxvf pcre-8.12.tar.gz
cd pcre-8.12
./configure –prefix=/usr
make
make install

2、下载nginx源码安装:

cd /tmp
wget -c http://nginx.org/download/nginx-0.8.55.tar.gz
tar -zxvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
./configure –user=www –group=www –prefix=/usr/local/nginx –with-http_realip_module –with-http_addition_module –with-http_dav_module –with-http_flv_module –with-http_sub_module –with-http_stub_status_module –with-http_ssl_module –with-http_gzip_static_module –with-http_perl_module –with-mail –with-mail_ssl_module –with-ipv6
make
make install

编译时最后提示:
nginx-0.8.55/src/os/unix/ngx_process.c:490: warning: `sys_errlist’ is deprecated; use `strerror’ or `strerror_r’ instead
nginx-0.8.55/src/os/unix/ngx_process.c:490: warning: `sys_nerr’ is deprecated; use `strerror’ or `strerror_r’ instead
网上查了下属于正常现象:http://nginx.org/en/docs/sys_errlist.html

While building nginx version 0.7.66, 0.8.35 or higher on Linux the following warning messages are issued:

warning: `sys_errlist’ is deprecated;
use `strerror’ or `strerror_r’ instead
warning: `sys_nerr’ is deprecated;
use `strerror’ or `strerror_r’ instead

This is normal: nginx has to use the deprecated sys_errlist[] and sys_nerr in signal handlers because strerror() and strerror_r() functions are not Async-Signal-Safe.

3、编写nginx启动脚本,设置开机自启动:

vim /etc/init.d/nginx

启动脚本内容看这里

chmod 755 /etc/init.d/nginx
chkconfig –level 345 nginx on

4、编辑nginx配置文件,整合php:

vim /usr/local/nginx/conf/fastcgi_params

加入一行,需要此行信息来确定PHP文件的位置。

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

编辑/usr/local/nginx/conf/nginx.conf文件:

vim /usr/local/nginx/conf/nginx.conf

找到并修改如下:

location / {
root html;
index index.html index.htm index.php;
}

location ~ \.php$ {
root html;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

5、检查配置是否有问题:

/usr/local/nginx/sbin/nginx -t

显示如下说明成功:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

6、启动nginx:

service nginx start

7、测试是否支持php:

cd /usr/local/nginx/html
vim phpinfo.php

输入

<?php
phpinfo();
?>

访问http://IP地址/phpinfo.php查看php版本情况。

8、相关目录
nginx主目录/usr/local/nginx/html
nginx配置文件/usr/local/nginx/conf/nginx.conf

LNMP环境安装好后,还需要对其进行配置优化才能正式使用。

PS:如果安装好后上传了探针但是显示不出“内存使用情况”、“网络使用情况”、“系统负载”等信息,是因为使用了推荐的默认php.ini配置文件,可以输入:
vim /etc/php.ini,设置short_open_tag = On保存,重启php-fpm和nginx服务即可。

参考资料:
http://www.mgrei.com/archives/740.html
http://apps.hi.baidu.com/share/detail/34323691
http://www.21andy.com/blog/20100201/1606.html