Linux + Nginx + MySQL + Php 。由于 Nginx 比Apache更轻巧一些,而且在nginx的官方网站上面对于nginx处理的多并发量是100000。所以,在现在网站会面临这种大并发量的时候,使用Nginx 来作为服务器程序确实是一个不错的选择,使用LNMP的,是使用一个叫做php-fpm 的一个单独的进程来完成处理,而且,Nginx 本身也是一个独立的进程,所以对资源占用就比LAMP环境下多了一个进程。
1、搭建前规划
操作系统Centos6.5 x86_64位
nginx ------> 172.16.24.6 (static IP)
MySQL -------> 172.16.24.7 (static IP)
PHP ---------> 172.16.24.8 (static IP)
一、准备安装包
nginx-1.4.7.tar.gz
#yum源要配置好,要能安装以下几个包:
gcc
openssl-devel
pcre-devel
zlib-devel
二、172.16.24.6平台上nginx的编辑安装。
[root@node1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog nginx-1.4.7.tar.gz
[root@node1 ~]# tar xf nginx-1.4.7.tar.gz
[root@node1 ~]# cd nginx-1.4.7
[root@node1 nginx-1.4.7]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@node1 nginx-1.4.7]# groupadd -r nginx #因为nignx运行时靠nginx用户运行,所以我们得先创建用户。
[root@node1 nginx-1.4.7]# useradd -r -g nginx nginx
[root@node1 nginx-1.4.7]# yum install gcc openssl-devel pcre-devel zlib-devel #nginx的运行要依赖这几个包所以要提前安装上。
[root@node1 nginx-1.4.7]#./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 \ #加载ssl模块
--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
[root@node1 nginx-1.4.7]# make && make install
三、配置nginx的配置文件让它能工作起来:
[root@node1 nginx-1.4.7]# /usr/sbin/nginx -t #用这个命令测试一下看看nginx是否能运行起来。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)#看这里有错误提示,提示得创建一个目录。
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@node1 nginx-1.4.7]# mkdir /var/tmp/nginx/ #我们来创建这个目录。
[root@node1 nginx-1.4.7]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful #这里有提示我们可以启动
[root@node1 nginx-1.4.7]# /usr/sbin/nginx #启动nginx.
[root@node1 nginx-1.4.7]# ss -tnl
四、为nginx提供启动脚本。
[root@node1 nginx-1.4.7]# vim /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
五、更改脚本权限,开机能够自动加载:
[root@node1 nginx-1.4.7]# chmod +x /etc/rc.d/init.d/nginx
[root@node1 nginx-1.4.7]# chkconfig --add nginx
[root@node1 nginx-1.4.7]# chkconfig nginx on
[root@node1 nginx-1.4.7]# service nginx start
Starting nginx: [ OK ]
六、配置让nginx的配置文件高亮显示
从这个网址http://www.vim.org/scripts/script.php?script_id=1886上下载一个nginx.vim的包
[root@node1 ~]# mkdir .vim/syntax -pv
mkdir: created directory `.vim'
mkdir: created directory `.vim/syntax'
[root@node1 ~]# cd .vim/syntax/
[root@node1 syntax]# 把下载包拷贝到这个目录下面
nginx.vim
[root@node1 syntax]# cd ..
[root@node1 .vim]# vim filetype.vim
au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
好了现在你的配置文件可以高亮显示了
七、172.16.24.7上mysql编译安装方式
准备所需的安装包 #mysql-5.5.33-linux2.6-x86_64.tar.gz
[root@node2 ~]# groupadd -r mysql
groupadd: group 'mysql' already exists
[root@node2 ~]# useradd -g mysql -r -s /sbin/nolgin mysql
useradd: user 'mysql' already exists
[root@node2 ~]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[root@node2 ~]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
[root@node2 ~]# cd /usr/local
[root@node2 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.5.33-linux2.6-x86_64 share
[root@node2 local]# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
`mysql' -> `mysql-5.5.33-linux2.6-x86_64'
为硬盘创建一个20G的分区,怎么样分区前面有介绍这是不详细叙。
[root@node2 local]# fdisk /dev/sda
把创建好的分区,整合成为逻辑卷:
[root@node2 local]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
[root@node2 local]# vgcreate myvg /dev/sda3
Volume group "myvg" successfully created
[root@node2 local]# lvcreate -L 10G -n mydata myvg
Logical volume "mydata" created
[root@node2 local]# lvs <==========查看创建好的逻辑卷
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mydata myvg -wi-a----- 10.00g
root vg0 -wi-ao---- 20.00g
swap vg0 -wi-ao---- 2.00g
usr vg0 -wi-ao---- 10.00g
var vg0 -wi-ao---- 20.00g
[root@node2 local]# mke2fs -t ext4 /dev/myvg/mydata<======为逻辑卷格式化
[root@node2 local]# mkdir /data 《== ==============创建一个data目录,把逻辑卷开机挂载此目录下
root@node2 local]# vim /etc/fstab《===============设定开机挂载,把下面内容加入fstab中。
/dev/myvg/mydata /data ext4 defaults,noatime 0 0
[root@node2 local]# mount -a
[root@node2 local]# mount<===============看到下面的提示才证明开机能挂载上,不然看看哪里设置错了吧。
/dev/mapper/myvg-mydata on /data type ext4 (rw,noatime)
八、更改文件权限,回为mysql服务的启动,只有mysql用户才能运行。
[root@node2 local]# cd /data
[root@node2 data]# mkdir mydata
[root@node2 data]# chown -R mysql.mysql mydata/
[root@node2 data]# cd /usr/local/mysql
[root@node2 mysql]# chown -R root.mysql ./*
九、为mysql程序提供服务脚本。
[root@node2 mysql]# scripts/mysql_install_db --datadir=/data/mydata --user=mysql
[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node2 mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@node2 mysql]# vim /etc/my.cnf
datadir = /data/mydata
innodb_file_per_table = ON
[root@node2 mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
mysql的头文件输出和库文件输出。
[root@node2 mysql]# . /etc/profile.d/mysql.sh
[root@node2 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include/'
[root@node2 mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@node2 mysql]# ldconfig -p | grep mysql
libtcmalloc_minimal.so.0 (libc6,x86-64) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0
libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16
libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18
十、在172.16.24.8上编译安装php及,整合nginx和php.
php-5.4.26.tar.bz2
[root@node3 ~]# yum install libmcrypt-devel bzip2-devel
[root@node3 php-5.4.26]# yum install libcurl*
[root@node3 ~]# tar xf php-5.4.26.tar.bz2
[root@node3 ~]# cd php-5.4.26
[root@node3 php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=mysqlnd --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 --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
root@node3 php-5.4.26]# make && make install
[root@node3 php-5.4.26]# cp php.ini-production /etc/php.ini
[root@node3 php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@node3 php-5.4.26]# chmod +x /etc/init.d/php-fpm
[root@node3 php-5.4.26]# chkconfig --add php-fpm
[root@node3 php-5.4.26]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
listen = 172.16.24.8:9000 #159行
pm.max_children = 50 #228行
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.status_path = /status #350左右
ping.path = /ping #362行左右
rlimit_files = 10240
整合nginx和php
1、编辑/etc/nginx/nginx.conf,启用如下选项:
location ~ \.php$ {
root html;
fastcgi_pass 172.16.24.8:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2、编辑/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;
并在所支持的主页面格式中添加php格式的主页,类似如下
location / {
root html;
index index.php index.html index.htm;
}
而后重新载入nginx的配置文件:
# service nginx reload
测试一下看看服务是否能正常运行起来了
在172.16.24.8这台主机上执行如下操作:
[root@node3 php-5.4.26]# mkdir /usr/html
[root@node3 php-5.4.26]# vim /usr/html/index.php
phpinfo();
?>
在windows浏览器里输入我们建好的主机地址
php能够正常运行起来了。这样我们的LNMP的平台就搭建好了。还有后续内容,请看下一篇的介绍。