Nginx之LNMP环境搭建
Nginx动态LNMP环境:
=====================
LAMP环境: Linux + Apache + MySQL + PHP(语言)
LNMP环境: Linux + Ngninx + MySQL + PHP(语言)
j2ee环境: JDK + Tomcat + MySQL/Oracle + JSP(语言)
LAMP/LNMP环境是用来解释php语言开发的程序
j2ee环境是用来解释java语言开发的出来的程序
备:
J2EE(Java 2 Platform, Enterprise Edition)是一个为大企业主机级的计算类型而设计的Java平台
一、搭建LNMP环境,CentOS 7 yum源方式
# yum info php
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* epel: del-mirrors.extreme-ix.org
可安装的软件包
名称 :php --名称
架构 :x86_64 --软件的架构平台,是64还是32的(x86_64代表的是64位,i386/i686代表的是32位)
版本 :5.4.16 --软件的版本号
发布 :42.el7
大小 :1.4 M
源 :server
简介 : PHP scripting language for creating dynamic web sites
网址 :http://www.php.net/
协议 : PHP and Zend and BSD
描述 : PHP is an HTML-embedded scripting language. PHP attempts to make it
: easy for developers to write dynamically generated web pages. PHP also
: offers built-in database integration for several commercial and
: non-commercial database management systems, so writing a
: database-enabled webpage with PHP is fairly simple. The most common
: use of PHP coding is probably as a replacement for CGI scripts.
:
: The php package contains the module (often referred to as mod_php)
: which adds support for the PHP language to Apache HTTP Server.
------------------------------------------------------------------------------
# yum info php-fpm
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* epel: mirror01.idc.hinet.net
* extras: mirrors.zju.edu.cn
* updates: mirrors.nju.edu.cn
可安装的软件包
名称 :php-fpm
架构 :x86_64
版本 :5.4.16
发布 :43.el7_4.1
大小 :1.4 M
源 :updates/7/x86_64
简介 : PHP FastCGI Process Manager
网址 :http://www.php.net/
协议 : PHP and Zend and BSD
描述 : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
: implementation with some additional features useful for sites of
: any size, especially busier sites.
------------------------------------------------------------------------------
# yum info mariadb
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* epel: mirror01.idc.hinet.net
* extras: mirrors.zju.edu.cn
* updates: mirrors.nju.edu.cn
可安装的软件包
名称 :mariadb
架构 :x86_64
时期 :1
版本 :5.5.56
发布 :2.el7
大小 :8.7 M
源 :base/7/x86_64
简介 : A community developed branch of MySQL
网址 :http://mariadb.org
协议 : GPLv2 with exceptions and LGPLv2 and BSD
描述 : MariaDB is a community developed branch of MySQL.
: MariaDB is a multi-user, multi-threaded SQL database server.
: It is a client/server implementation consisting of a server daemon
: (mysqld) and many different client programs and libraries. The base
: package contains the standard MariaDB/MySQL client programs and
: generic MySQL files.
----------------------------------------------------------------------------------------
# yum info nginx
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: mirrors.ustc.edu.cn
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
已安装的软件包
名称 :nginx
架构 :x86_64
时期 :1
版本 :1.12.2
发布 :2.el7
大小 :1.5 M
源 :installed
来自源:epel --此软件包来自于epel扩展yum仓库
简介 : A high performance web server and reverse proxy server
网址 :http://nginx.org/
协议 : BSD
描述 : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.
------------------------------------------------------------------------------
以上可以确认lnmp环境软件包都有
接下来使用yum安装lnmp环境
yum安装lnmp没有先后顺序
--先安装Nginx
# yum -y install nginx
--安装mariadb
# yum -y install mariadb*
--安装php,php-fpm
# yum -y install php php-devel php-fpm php-mysql
----------------------------------------------------------------------
安装完成以后,验证是否安装成功
--检查下nginx配置文件是否有,网站数据目录是否存在
/etc/nginx/nginx.conf --配置文件
/usr/share/nginx/html --网站数据目录
--检查mariadb数据配
/etc/my.cnf --数据库配置文件
/var/lib/mysql --数据库数据存放路径
--检查php,php-fpm配置文件
/etc/php.ini --php主程序的配置文件
/etc/php-fpm.conf --php-fpm管理器的配置文件
------------------------------------------------------------
--配置php
# vi /etc/php.ini
1087 mysql.default_port =3306 --改为数据库的实际端口
1092 mysql.default_socket =/var/lib/mysql/mysql.sock --改为数据库sock文件实际路径
1146 mysqli.default_port = 3306
1151 mysqli.default_socket =/var/lib/mysql/mysql.soc
--配置php-fpm
# vi /etc/php-fpm.d/www.conf --注意:记得将前面的;(分号)去掉
12 listen = /var/run/fastcgi/fastcgi.socket --指定php-fpm管理器的连接方法
31 listen.owner = nginx --指定权限为nginx用户
32 listen.group = nginx --指定权限组为nginx
33 listen.mode = 0660 --指定权限为0660
39 user = nginx --指定使用nginx身份
41 group = nginx --指定使用nginx组
75 pm.start_servers = 5 -指定开启工作线程的数量
80 pm.min_spare_servers = 3 --工作线程最小数量
85 pm.max_spare_servers = 8 --工作线程最大数量
91 pm.max_requests = 500 --允许同时解释文件的数量
160 rlimit_files =65535 --读取同时读取的文件数,默认为1024
--配置Nginx
# vi /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 65535;
}
http {
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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 35;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name nginx; --本机的主机名
charset utf8;
root /www;
index index.php index.html --把此参数从原来的location里面移到此处
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ .*\.php$ { --新加此段内容,为解释php程序
fastcgi_connect_timeout 300; --连接超时时间;
fastcgi_read_timeout 300; --读取php程序超时时间
fastcgi_send_timeout 300; --将结果发回超时时间
fastcgi_pass unix:/var/run/fastcgi/fastcgi.socket; --指定php-fpm的连接方式
fastcgi_index index.php; --指定主页文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; --解释模块变量
include fastcgi_params; --将fastcgi_params模块加载进来
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
-----------------------------------------------------------------
创建nginx的网站数据家目录
# mkdir /www
创建php-fpm管理器的socket生成的目录
# mkdir /var/run/fastcgi
更改所属用户权限
# chown nginx.nginx -R /www /var/run/fastcgi
启动服务php-fpm
# systemctl start php-fpm
--检查是否启动成功
# ls -l /var/run/fastcgi/
srw-rw-rw-. 1 nginx nginx 0 11月 10 11:45 fastcgi.socket --有看到此文件代表启动成功,如果没有的话再仔细检查php-fpm的配置文件看是否有配置错误
# ps -ef |grep php-fpm
root 2062 1 0 11:45 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx 2064 2062 0 11:45 ? 00:00:00 php-fpm: pool www
nginx 2065 2062 0 11:45 ? 00:00:00 php-fpm: pool www
nginx 2066 2062 0 11:45 ? 00:00:00 php-fpm: pool www
nginx 2067 2062 0 11:45 ? 00:00:00 php-fpm: pool www
nginx 2068 2062 0 11:45 ? 00:00:00 php-fpm: pool www
备:
第一条master process 代表的是主进程
后面5条 pool www 为工作进程
启动Nginx
# systemctl start nginx
# lsof -i:80 --检查nginx监听的端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 2163 root 7u IPv4 32503 0t0 TCP *:irdmi (LISTEN) --Nginx主进程
nginx 2164 nginx 7u IPv4 32503 0t0 TCP *:irdmi (LISTEN) --Nginx工作进程
建一个php程序文件,测试LNMP是否能解释php的程序
# vi /www/index.php
<?php
phpinfo();
?>
--到浏览器上访问LNMP机器的IP地址:port
firefox http://LNMP机器的ip:port
例:http://192.1.1.199:80
要能明确看到php的版本信息才算解释成功