企业级LNMP(Nginx+PHP(FastCGI)+MySQL)主流架构配置方法如下,分别安装Nginx、MYSQL、PHP服务,步骤如下:
1、nginx源码安装
cd /usr/src wget -c http://nginx.org/download/nginx-1.12.0.tar.gz tar -xzf nginx-1.12.0.tar.gz cd nginx-1.12.0 useradd www ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install
2、mysql源码安装
yum install cmake ncurses-devel ncurses gcc-c++ –y
wget -c https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.20.tar.gz
tar -xzf mysql-5.5.20.tar.gz
cd mysql-5-5.20
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_EXTRA_CHARSETS=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BIG_TABLES=1 -DWITH_DEBUG=0 make make install
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
mkdir -p /data/mysql
useradd mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql/
ln -s /usr/local/mysql/bin/* /usr/bin/
chkconfig --add mysqld
chkconfig --level 35 mysqld on
/etc/init.d/mysqld start
3、源码安装php
yum install -y libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel wget -c http://cn2.php.net/distributions/php-5.6.38.tar.gz tar -zxf php-5.6.38.tar.gz cd php-5.6.38 ./configure --prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-apxs2=/usr/local/apache/bin/apxs
--with-bz2
--with-curl
--enable-ftp
--enable-sockets
--disable-ipv6
--with-gd
--with-jpeg-dir
--with-png-dir
--with-freetype-dir
--enable-gd-native-ttf
--with-iconv-dir
--enable-mbstring
--enable-calendar
--with-gettext
--with-ldap
--with-libxml-dir
--with-zlib
--with-pdo-mysql=mysqlnd
--with-mysqli=mysqlnd
--with-mysql=mysqlnd
--enable-dom
--enable-xml
--enable-fpm
--with-libdir=lib64
--enable-bcmath make && make install
4、nginx与php整合
首先简单的讲一讲原理,目前主流的nginx+php的运行原理如下:
(1)、nginx的worker进程直接管理每一个请求到nginx的网络请求。
(2)对于php而言,由于在整个网络请求的过程中php是一个cgi程序的角色,所以采用名为php-fpm的进程管理程序来对这些被请求的php程序进行管理。php-fpm程序也如同nginx一样,需要监听端口,并且有master和worker进程。worker进程直接管理每一个php进程。
(3)关于fastcgi:fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了fastcgi功能的进程管理器,php-fpm就是其中的一种。再提一点,php-fpm作为一种fast-cgi进程管理服务,会监听端口,一般默认监听9000端口,并且是监听本机,也就是只接收来自本机的端口请求,所以我们通常输入命令 netstat -nlpt|grep php-fpm 会得到:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1057/php-fpm
这里的127.0.0.1:9000 就是监听本机9000端口的意思。
(4)关于fastcgi的配置文件,目前fastcgi的配置文件一般放在nginx.conf同级目录下,配置文件形式,一般有两种:fastcgi.conf 和 fastcgi_params。
不同的nginx版本会有不同的配置文件,这两个配置文件有一个非常重要的区别:fastcgi_parames文件中缺少下列配置:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
我们可以打开fastcgi_parames文件加上上述行,也可以在要使用配置的地方动态添加。使得该配置生效。
(5)当需要处理php请求时,nginx的worker进程会将请求移交给php-fpm的worker进程进行处理,也就是最开头所说的nginx调用了php,其实严格得讲是nginx间接调用php。
了解了上面的这五个简单原理,在nginx中配置php调用方法就变得易如反掌。
server { include port.conf; server_name www.jfedu.net jfedu.net; index index.php index.html index.php; root /usr/local/nginx/html; location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } fastcgi_pass 127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的url请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。
使用下列代码也可以 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
参与评论
手机查看
返回顶部