Nginx实现LNMP机制:这里讲述是以yum包进行安装,没有涉及编译安装。
所谓LNMP:是一个基于web处理,动态语言处理php,后端调取数据mysql。相当与用户发起一个请求,取得数据经过的一系列的应用程序。
Nginx搭建LNMP步骤:
一:安装Nginx,可以使用编译安装,也可使用yum安装。
[root@node1 ~]# yum install nginx -y 依赖epel源
[root@node1 ~]# vim /etc/nginx/conf.d/default.conf 修改nginx配置文件,default.conf默认是再nginx.conf中包含。
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 这里必须修改成这样要不然找不到脚本变量让php服务器处理。
include fastcgi_params;
}
[root@node1 ~]# systemctl start nginx 启动nginx
二:安装php平台,安装php-fpm软件包;
[root@node1 ~]# yum install php-fpm -y
[root@node1 ~]# rpm -ql php-fpm
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/php-fpm.d/www.conf
/etc/sysconfig/php-fpm
/run/php-fpm
/usr/lib/systemd/system/php-fpm.service
/usr/lib/tmpfiles.d/php-fpm.conf
/usr/sbin/php-fpm
/usr/share/doc/php-fpm-5.4.16
/usr/share/doc/php-fpm-5.4.16/fpm_LICENSE
/usr/share/doc/php-fpm-5.4.16/php-fpm.conf.default
/usr/share/fpm
/usr/share/fpm/status.html
/usr/share/man/man8/php-fpm.8.gz
/var/log/php-fpm
查看php-fpm的配置文件。
[root@node1 ~]# vim /etc/php-fpm.conf
[root@node1 ~]# vim /etc/php-fpm.d/www.conf
[root@node1 ~]#
[www]
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000 这表是监听的php地址,在我们这里是本机,如果php服务器是远端地址,这里需要修改
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache 这里php运行的用户为apache,这是yum包安装时是这个用户,当编译安装时,这里必须修改有运行权限的用户。
; RPM: Keep a group allowed to write in log dir.
group = apache
启动php-fpm
[root@node1 nginx]# systemctl restart php-fpm
[root@node1 nginx]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
修改 vim /etc/nginx/fastcgi_params使得能构建lnmp平台
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_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;
在Nginx的/usr/share/nginx/html中创建一个index.php来作为测试页;
[root@node1 nginx]# cd /usr/share/nginx/html/
[root@node1 html]# ls
50x.html index.php nginx.html
[root@node1 html]# vim index.php
<?php
phpinfo();
?>
安装mysql相关软件,用于php与myslq进行交互。
[root@node1 ~]# yum install php-mysql mariadb-server -y
[root@node1 html]# rpm -ql php-mysql
[root@node1 html]# systemctl start mariadb
[root@node1 html]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
/etc/php.d/mysql.ini
/etc/php.d/mysqli.ini 这表示可以连接数据库。
/etc/php.d/pdo_mysql.ini
/usr/lib64/php/modules/mysql.so
/usr/lib64/php/modules/mysqli.so
/usr/lib64/php/modules/pdo_mysql.so
验证php能否与后端mysql服务器交互
[root@node1 html]# vim index.php
<?php
$conn = mysql_connect('127.0.0.1','root','');
if ($conn)
echo succ;
else
echo fail;
mysql_close();
?>
二:设置fastcgi来接后端php服务器的缓存机制;
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m;
fastcgi_cache_path /cache/fastcgi/ levels=1:1 keys_zone=fastcgi:32m inactive=3m max_size=1g;
inactive:表示非活动连接数3分钟;
mx_size :表示缓存最大占用空间为1g
[root@node1 ~]# mkdir -pv /cache/fastcgi/ 创建缓存目录;
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache fastcgi;
fastcgi_cache_valid 200 1d;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
}
[root@node1]# curl http://10.5.100.207/index.php
[root@node1 ~]# mkdir /cache/fastcgi
[root@node1 ~]# chmod -R nginx:nginx /cache/fastcgi
[root@node1 ~]# cd /cache/fastcgi/
[root@node1 fastcgi]# ls 查看缓存目录中的内容。
e
当开启缓存功能时,如何验证当客户端发起请求时,是由后端服务器回应处理请求,还是由缓存命中;
需要添加两个参数;
server {
listen 80;
server_name localhost;
add_header X-Via $server_addr; 这表示当有虚拟机时也可以做转发。
add_header X-cache $upstream_cache_status; 表示响应报文是由缓存命中,还是由服务端响应。。
MISS”, “BYPASS”, “EXPIRED”, “STALE”, “UPDATING”, “REVALIDATED”, or “HIT”.
Nginx实现动静分离:上述我们访问的是动态语言,下述我们访问静态语言。实现动静结合。
同一个server访问不同的资源路径,实现动静结合。
location ~* \.(jpg|png)$ {
proxy_pass http://10.5.100.183;
}
总结:当我们客户端请求web服务器时使用http协议,请求处理动态语言php服务器时使用fastcgi协议,
当我们请求后端为一个LNMP平台时,因为php机制也可以工作在apache模块中,即使用的也是http协议