注:此次实验搭建LNMP的服务器IP地址为:172.16.4.1
一、源码编译安装Nginx
1、由于需要这里我们使用的是源码编译安装Nginx,所以我们需要构建开发环境,即需要安装Development libraries和Development tools这两个开发包组,但是由于Development libraries包组依赖于Development tools包组,所以当安装Development libraries包组时Development tools里的大部分包其实已经安装过了,所以这里我们只安装Development libraries这个包组,命令如下:
#yum groupinstall “Development libraries” –y
2、解决依赖关系:
Nginx的安装需要依赖于gcc、openssl-devel、pcre-devel、zlib-devel这四个包,所以在使用源码安装之前请确保这四个包已经安装,命令如下:
#rpm –ivh gcc openssl-devel pcre-devel zlib-devel
注:一般情况下,这些包已经安装过。
3、为nginx建立安全运行账户:
# groupadd -r nginx
# useradd -r -g nginx -s /sbin/nologin -M nginx
4、编译安装nginx-1.0.14
下载nginx-1.0.14.tar.gz到本地,下载地址:
http://nginx.org/download/nginx-1.0.14.tar.gz
执行以下命令:
- # tar xf nginx-1.0.14.tar.gz
- # cd nginx-1.0.14
- #./configure \
- --prefix=/usr \
- --sbin-path=/usr/sbin/nginx \
- --conf-path=/etc/nginx/nginx.conf \
- --error-log-path=/var/log/nginx/error.log \
- --http-log-path=/var/log/nginx/access.log \
- --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-http_flv_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/tmp/nginx/client/ \
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
- --http-scgi-temp-path=/var/tmp/nginx/scgi \
- --with-pcre
- # make && make install
4、提供SysV服务脚本/etc/rc.d/init.d/nginx,内容如下:
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- # chkconfig: - 85 15
- # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
- # proxy and IMAP/POP3 proxy server
- # processname: nginx
- # config: /etc/nginx/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/nginx.pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ "$NETWORKING" = "no" ] && exit 0
- nginx="/usr/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/etc/nginx/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- echo -n $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -QUIT
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- RETVAL=$?
- echo
- }
- force_reload() {
- restart
- }
- configtest() {
- $nginx -t -c $NGINX_CONF_FILE
- }
- rh_status() {
- status $prog
- }
- rh_status_q() {
- rh_status >/dev/null 2>&1
- }
- case "$1" in
- start)
- rh_status_q && exit 0
- $1
- ;;
- stop)
- rh_status_q || exit 0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q || exit 7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q || exit 0
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit 2
- esac
而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/nginx
将nginx的服务加入服务列表:
# chkconfig --add nginx
并且将nginx设为开机启动:
#chkconfig nginx on
5、启动apache,并测试:
#service httpd start
在浏览器中输入172.16.4.1,出现如下界面:
证明Nginx启动成功!
二、使用绿色版,解压安装Mysql-5.2.20
1、准备数据存放的文件系统:
最好创建一个逻辑卷用于存放Mysql的数据,以实现之后对数据进行快照备份,卷的大小根据需要自定义,这里创建一个逻辑卷挂载到/mydata下并创建一个新的目录用于存放Mysql的数据,并设置为开机自动挂载,这里不在给出创建步骤。
2、新建用户以安全方式运行进程:
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data
3、安装并初始化mysql-5.5.20:
下载平台对应的Mysql版本至本地,官方下载地址:
http://dev.mysql.com/downloads/mysql/5.0.html
这里选择Mysql-5.5.20-linux2.6-i386.tar.gz执行以下命令:
- # tar xf mysql-5.5.20-linux2.6-i686.tar.gz -C /usr/local
- # cd /usr/local/
- # ln -sv mysql-5.5.20-linux2.6-i686 mysql
- # cd mysql
- # chown -R mysql:mysql .
- # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
- # chown -R root .
4、为mysql提供主配置文件:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
5、为mysql提供sysv服务脚本:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
添加mysql至服务列表,并将其设为开机启动:
# chkconfig --add mysqld
# chkconfig mysqld on
6、导出mysql的man文件:
#vim /etc/man.conf
添加如下一行:
MANPATH /usr/local/mysql/man
7、导出apache的头文件:
#ln –sv /usr/local/mysql/include /usr/ include/mysql
8、导出库文件,并重新载入库文件:
#echo “/usr/local/mysql/lib” > /etc/ld.so.conf.d/mysql.conf
#ldconfig -v
9、修改环境变量:
#vim /etc/profile
在“PATH=$PATH:/usr/local/apache/bin“后面添加:
:/usr/local/mysql/bin
10、启动mysql:
#service mysqld start
三、源码编译php-5.3.10
1、为编译php-5.3.10提供编译环境:
安装X Software Development这个包组,请配置好yum源后,执行如下命令:
# yum -y groupinstall "X Software Development"
为nginx启动fastcgi,需要对php进行一些功能扩展,需要更新升级或者源码编译以下安装包,因为在RHEL4上已经安装过,如果使用rpm对其进行升级,则升级时具有依赖关系,所以这里使用源码安装以下四个包:
libevent-2.0.16-stable.tar.gz
libiconv-1.13.1.tar.gz
libmcrypt-2.5.8.tar.gz
mhash-0.9.9.9.tar.bz2
下载地址:
http://down.51cto.com/data/361648
其中有源码包和rpm包,如果下载rpm对其进行升级,则跳过第2,3,4,5步,执行以下命令:
#rpm –Uvh libevent-devel-2.0.17-2.i386.rpm libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm mcrypt-2.6.8-1.el5.i386.rpm mhash-devel-0.9.9-1.el5.centos.i386.rpm --nodeps
2、编译安装libevent
首先将libevent-2.0.16-stable.tar.gz的源码包下载到本地:执行以下命令:
- # tar zxvf libevent-1.4.14b-stable.tar.gz
- # cd libevent-1.4.14b-stable
- # ./configure
- # make && make install
- # make verify
3、编译安装libiconv:这个包为可选,也可以不安装:
下载libiconv-1.13.1.tar.gz源码包至本地,执行以下命令:
- # tar zxvf libiconv-1.13.1.tar.gz
- # cd libiconv-1.13.1
- # ./configure
- # make && make install
4、编译安装libmcrypt:
下载libmcrypt-2.5.8.tar.gz源码包至本地,执行以下命令:
- # tar zxvf libmcrypt-2.5.8.tar.gz
- # cd libmcrypt-2.5.8
- # ./configure
- # make && make install
- # ldconfig -v
- # cd libltdl
- # ./configure --with-gmetad --enable-gexec --enable-ltdl-install
- # make && make install
5、编译安装mhash:
下载mhash-0.9.9.9.tar.bz2源码包至本地,执行以下命令:
- # tar jxvf mhash-0.9.9.9.tar.bz2
- # cd mhash-0.9.9.9
- # ./configure
- # make && make install
- # ln -sv /usr/local/lib/libmcrypt* /usr/lib/
- # ln -sv /usr/local/lib/libmhash.* /usr/lib/
6、编译安装php-5.3.10
下载php-5.3.10.tar.bz2源码包至本地,下载地址:
http://cn2.php.net/get/php-5.3.10.tar.bz2/from/this/mirror
执行以下命令:
# tar jxvf php-5.3.10.tar.bz2
# cd php-5.3.10
注:在此要检查bzip2-devel、curl-devel这两个开发包有没有安装,如果没有安装则将其安装后在执行以下命令。
- # ./configure --prefix=/usr/local/phpnginx \
- --with-mysql=/usr/local/mysql \
- --with-openssl \
- --enable-fpm \
- --with-mysqli=/usr/local/mysql/bin/mysql_config \
- --enable-mbstring \
- --with-freetype-dir \
- --with-jpeg-dir \
- --with-png-dir \
- --with-zlib-dir \
- --with-libxml-dir=/usr \
- --enable-xml \
- --with-mhash \
- --with-mcrypt \
- --with-config-file-path=/etc/phpnginx \
- --with-config-file-scan-dir=/etc/phpnginx \
- --with-bz2 \
- --with-curl \
- --with-iconv=/usr/local
如果没有执行步骤3,没有安装libiconv,则执行以下命令:
- # make
- # make install
- # mkdir /etc/phpnginx
- # cp php.ini-production /etc/phpnginx/php.ini
如果执行了步骤3,并且安装了libiconv,则执行以下命令:
- # make ZEND_EXTRA_LIBS='-liconv'
- # make install
- # mkdir /etc/phpnginx
- # cp php.ini-production /etc/phpnginx/php.ini
7、启用fastcgi
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# vim /usr/local/php/etc/php-fpm.conf
启用如下选项:
pid = run/php-fpm.pid
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
8、为php-fpm提供SysV服务脚本:
#cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
#chmod +x /etc/rc.d/init.d/php-fpm
#chkconfig –-add php-fpm
#chkconfig php-fpm on
9、而后启动php-fpm:
#service php-fpm start
四、整合Nginx和PHP构建LNMP
1、启用nginx的fastcgi:
编辑/etc/nginx/nginx.conf,启用如下选项:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2、编辑fastcgi_params参数:
#vim /etc/nginx/fastcgi_params
将里面的内容全部替换为以下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
3、并在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root html;
index index.php index.html index.htm;
}
4、提供默认的php网页:
将/usr/html/index.html重命名为index.php
#mv /usr/html/index.html /usr/html/index.php
添加以下内容:
<?php
phpinfo();
?>
5、而后重启nginx:
#service nginx restart
在浏览器中输入 172.16.4.1,出现如下界面:
证明PHP安装成功!
6、测试能否连接上Mysql
编辑/usr/htnl/index.php,添加如下内容:
<?php
$link=mysql_connect('localhost','root','');
if ($link)
echo "Successfuly";
else
echo "Faile";
mysql_close();
?>
保存退出。
在浏览器中输入 172.16.4.1,出现以下内容:
证明php可以连接到Mysql!
到此LNMP平台构建成功。