您的位置:首页 > 运维架构 > Nginx

LNMP搭建,nginx整合php-fpm即fastCGI实现,以及nginx的多虚拟主机配置

2016-03-13 16:27 866 查看
PHP程序员一枚,此次环境也算是个人的一个笔记吧,贴出来了

环境:

CentOS 5.5

镜像地址就不贴了

php-5.4.20

地址:http://museum.php.net/php5/php-5.4.20.tar.gz

mysql-5..5.48(通用二进制格式)

地址:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.48-linux2.6-i686.tar.gz

nginx-1.8.1

地址:http://nginx.org/download/nginx-1.8.1.tar.gz

一、编译安装Nginx:

1、解决依赖关系

编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。

同时,还需要专门安装pcre-devel包:

# yum -y install pcre-devel

2、安装

首先添加用户nginx,实现以之运行nginx服务进程:

# groupadd -r nginx

# useradd -r -g nginx nginx

接着开始编译和安装:

# ./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的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。

3、为nginx提供SysV init脚本:

新建文件/etc/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/init.d/nginx

添加至服务管理列表,并让其开机自动启动:

# chkconfig --add nginx

# chkconfig nginx on

而后就可以启动服务并测试了:

# service nginx start

如果看到nginx的欢迎页面说明安装成功!

二、通用二进制格式安装mysql

注意:使用通用二进制格式安装的mysql,其安装目录必须在/usr/local/下,并且目录名必须为mysql

tar xf
mysql-5.5.48-linux2.6-i686.tar.gz -C /usr/local/
解压到/usr/local下

cd /usr/local/

ln -sv mysql-5.5.48 mysql [链接文件,不用改变原名,而且遵循了mysql官方规定]

如果没有mysql用户和mysql组,需要创建:

groupadd -r -g 306 mysql

useradd -r -g 306 mysql

改变程序的属主属组:

chown -R mysql.mysql /usr/local/mysql/*

[注意]

如果mysql的默认数据目录依然是程序目录下的data,则必须为mysql用户和mysql组

所以还需要改回来

其它的属主操作完成之后则需要改成root,此方法为安全做法,防止有人恶意攻破mysql进程,而获取恶意操作权限

drwxr-xr-x 2 root mysql 4096 Mar 13 07:01 bin

-rw-r--r-- 1 root mysql 17987 Mar 13 07:01 COPYING

drwxr-xr-x 5 mysql mysql 4096 Mar 13 07:20
data

drwxr-xr-x 2 root mysql 4096 Mar 13 07:01 docs

drwxr-xr-x 3 root mysql 4096 Mar 13 07:01 include

-rw-r--r-- 1 root mysql 151708 Mar 13 07:01 INSTALL-BINARY

drwxr-xr-x 3 root mysql 4096 Mar 13 07:01 lib

drwxr-xr-x 4 root mysql 4096 Mar 13 07:01 man

drwxr-xr-x 10 root mysql 4096 Mar 13 07:02 mysql-test

-rw-r--r-- 1 root mysql 2496 Mar 13 07:00 README

drwxr-xr-x 2 root mysql 4096 Mar 13 07:01 scripts

drwxr-xr-x 27 root mysql 4096 Mar 13 07:01 share

drwxr-xr-x 4 root mysql 4096 Mar 13 07:01 sql-bench

drwxr-xr-x 2 root mysql 4096 Mar 13 07:00 support-files

切换到程序目录

cd /usr/local/mysql

scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql

cp support-files/mysql.server /etc/init.d/mysqld

# 加入sysV

chkconfig --add mysqld

# 开机启动

[root@localhost mysql]# chkconfig mysqld on

# 拷贝配置文件

[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf

[注意]:如果数据目录指定的是其它的目录,则需要在/etc/my.cnf加上配置项

datadir=/mydata/data(例如) 不然mysql启动不了,楼主用的是默认的程序目录下的data,即不用修改

6、启动服务

[root@localhost etc]# service mysqld start

7、添加本地环境变量

vim /etc/profile.d/mysqld

内容:export PATH=$PATH:/usr/local/mysql/bin

为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:

6、输出mysql的man手册至man命令的查找路径:

编辑/etc/man.config,添加如下行即可:

MANPATH /usr/local/mysql/man

7、输出mysql的头文件至系统头文件路径/usr/include:

这可以通过简单的创建链接实现:

# ln -sv /usr/local/mysql/include /usr/include/mysql

8、输出mysql的库文件给系统库查找路径:

# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

而后让系统重新载入系统库:

# ldconfig

重新登录下:

运行成功!

二、编译安装PHP5.4

1、解决依赖关系:

请配置好yum源(可以是本地系统光盘)后执行如下命令:

# yum -y groupinstall "X Software Development"

如果想让编译的php支持mcrypt、mhash扩展和libevent,此处还需要如下几个rpm包并安装之:

libmcrypt-2.5.8-4.el5.centos.i386.rpm

libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm

mhash-0.9.9-1.el5.centos.i386.rpm

mhash-devel-0.9.9-1.el5.centos.i386.rpm

mcrypt-2.6.8-1.el5.i386.rpm

最好使用升级的方式安装上面的rpm包,命令格式如下:

# rpm -Uvh

另外,也可以根据需要安装libevent,系统一般会自带libevent,但版本有些低。因此可以升级安装之,它包含如下两个rpm包。

libevent-2.0.17-2.i386.rpm

libevent-devel-2.0.17-2.i386.rpm

说明:libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制,它主要用来替换事件驱动的网络服务器上的event loop机制。目前来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。

2、编译安装php-5.4

# tar xf php-5.4.20.tar.gz

# cd php-5.4.20

# ./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-mhash \

--with-mcrypt \

--with-config-file-path=/etc \

--with-config-file-scan-dir=/etc/php.d \

--with-bz2 \

--with-curl

说明:如果前面第1步解决依赖关系时安装mcrypt相关的两个rpm包,此./configure命令还可以带上--with-mcrypt选项以让php支持mycrpt扩展。--with-snmp选项则用于实现php的SNMP扩展,但此功能要求提前安装net-snmp相关软件包。

# make

# make test

# make intall

为php提供配置文件:

# cp php.ini-production /etc/php.ini

为php-fpm提供Sysv init脚本,并将其添加至服务列表:

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

# chmod +x /etc/init.d/php-fpm

# chkconfig --add php-fpm

# chkconfig php-fpm on

为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 (可不编辑这项,让其默认)

接下来就可以启动php-fpm了:

# service php-fpm start

使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):

# ps aux | grep php-fpm

3.整合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;

}

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

3、在/usr/html新建index.php的测试页面,测试php是否能正常工作:

# vim /usr/local/html/index.php

<?php

phpinfo();

?>

接着就可以通过浏览器访问此测试页面了。

四、配置nginx的虚拟主机,并且都支持PHP

①备份下主配置文件

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

cd /etc/nginx

②创建额外的配置文件目录

mkdir vohost

③修改配置项

vim nginx.conf

添加一行,让其去读取默认的配置文件

例如在vhost/下定义一个www_test_com.conf,作为新的基于域名的www.test.com的虚拟主机

vim vhost/www_test_com.conf

内容如下:

server{

listen 80;

server_name www.test.com;

charset utf-8;

index index.php index.html index.htm;

autoindex on;

location ~ \.php$ {

root /var/site/www.test.com;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;

include fastcgi_params;

}

}

server_name:网站名称

root : 网站根目录

如果是将lnmp安装到的虚拟机中,别忘了ifconfig一下,然后将该ip和域名(www.test.com)添加到主机的hosts文件中,同时要检查linux的防火墙是否是处于关闭状态,OK!!!

第一次发,格式很乱。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: