nginx在处理并发方面有着很不错的表现,相对于较为常用的LAMP平台(Linux + Apache + Mysql + PHP), LNMP也越来越多的被使用,当站点的并发访问量较大时,nginx的确是一个不错的选择,不过nginx与php是通过php-fpm的方式整合的,也就是fastcgi,这要求php-fpm单独启用一个进程,至于如何选择,应根据实际生产环境需要来加以应用。
实验规划:
系统平台: RHEL5.8-i386
nginx:nginx-1.4.7.tar
mysql: mysql-5.5.44-linux2.6-i686.tar
php: php-5.4.41.tar
一,安装nginx:
1,解决依赖关系:
# yum -y groupinstall "Development Libraries" "Development Tools" # yum -y install pcre-devel
2,添加nginx的运行用户和组:
# groupadd -r nginx # useradd -r -g nginx nginx
3,编译安装nginx:
# tar xf nginx-1.4.7.tar.gz # cd 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 --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
启动nginx
# /usr/sbin/nginx
测试nginx
# netstat -tnlp | grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31103/nginx # curl <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...以下内容省略
看到如上提示,说明你的nginx可以正常工作了。
二、安装mysql-5.5.44
1,首先要准备一个存放数据的文件系统,建议使用lvm
# vgs VG #PV #LV #SN Attr VSize VFree vg_root 1 1 0 wz--n- 97.70G 48.58G # lvcreate -L 300M -n mydata /dev/vg_root/ # mkfs.ext3 /dev/vg_root/mydata # mkdir /mydata # mount /dev/vg_root/mydata /mydata # cd /mydata # mkdir data
2,创建mysql用户和mysql组:
# groupadd -r mysql # useradd -r -g mysql -s /sbin/nologin -M -d /mydata/data mysql # chown -R mysql:mysql /mydata/data
3,安装并初始化mysql
# tar xf mysql-5.5.44-linux2.6-i686.tar.gz -C /usr/local # ln -sv /usr/local/mysql-5.5.44-linux2.6-i686 /usr/local/mysql # cd /usr/local/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 # chkconfig --add mysqld # chkconfig mysqld on
6,导出mysql的man手册:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
7,输出mysql头文件至系统头文件目录
# ln -sv /usr/local/mysql/include /usr/include/mysql
8,输出mysql的库文件至系统库文件的路径:
# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf # ldconfig -v
9,修改PATH环境变量:
# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh # . /etc/profile.d/mysql.sh
10,测试启动mysql服务:
# service mysqld start # mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.44-log MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql>
至此,mysql-5.5.44安装完成。
三,安装PHP-5.5.41
1,编译安装php
# tar xf php-5.4.41.tar.bz2 # cd php-5.4.41 # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --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-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl # make && make install
2,为php提供配置文件:
# cp php.ini-production /etc/php.ini
3,为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# 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
4,为php-fpm提供配置文件:
# 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文件(如下最后一行):
pm.max_children = 150 pm.start_servers = 8 pm.min_spare_servers = 5 pm.max_spare_servers = 10 pid = /usr/local/php/var/run/php-fpm.pid
5,启动php-fpm:
# service php-fpm start # netstat -tnlp | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 31025/php-fpm
PHP安装完成。
四,整合nginx和php5
1,编辑/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; } # 添加对php格式的主页 location / { root html; index index.php index.html index.htm; }
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;
3,重新载入nginx
# /usr/sbin/nginx -s reload
4,在/usr/html新建index.php的测试页面,测试php是否能正常工作:
# vim /usr/html/index.php <h1>LNMP test</h1> <?php phpinfo(); ?>
打开浏览器测试,如下