centos 6.* nginx安装,配置
一.centos 6.*安装nginx:
1.版本nginx 1.16.1
1>安装依赖
1.1 gcc是用来编译下载下来的nginx源码
yum install gcc-c++
1.2 PCRE是一个Perl库,包括 perl 兼容的正则表达式库。
nginx 的 http 模块使用 pcre 来解析正则表达式,pcre-devel 是使用 pcre 开发的一个二次开发库。
yum -y install openssl openssl-devel
1.3
在http://nginx.org/download/选择查看要下载的版本号
cd /usr/locao/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd /usr/local/src/nginx-1.16.1
./configure --prefix=/usr --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/usr/local/nginx/log/access.log --http-client-body-temp-path=/usr/local/nginx/client --http-proxy-temp-path=/usr/local/nginx/proxy --http-fastcgi-temp-path=/usr/local/nginx/fcgi --with-http_stub_status_module
1.3 nginx命令添加到环境变量
vim /etc/profile
在末尾加上
PATH=$PATH:/usr/local/nginx/sbin
重新加载环境
source /etc/profile
nginx -v // 查看nginx版本
1.4添加service启动:
vim /etc/init.d/nginx
插入下面的内容
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
# Author: licess
# website: https://lnmp.org
NGINX_BIN='/usr/local/nginx/sbin/nginx'
CONFIG='/usr/local/nginx/nginx.conf'
case "$1" in
start)
echo -n "Starting nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
echo "nginx (pid $PID) already running."
exit 1
fi
$NGINX_BIN -c $CONFIG
if [ "$?" != 0 ]; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" = "" ]; then
echo "nginx is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
$0 force-quit
else
echo " done"
fi
;;
status)
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
echo "nginx (pid $PID) is running..."
else
echo "nginx is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" = "" ]; then
echo "nginx is is stopped."
exit 1
fi
kill $PID
if [ "$?" != 0 ]; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
$NGINX_BIN -s reload
echo " done"
else
echo "nginx is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test nginx configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
保存后,
service nginx start //开启服务,/usr/local/nginx/sbin/nginx 也可以开启服务
service nginx restart //重启
service nginx stop //停止服务
ps -ef | grep nginx //查看服务
2.配置文件
1.概念:
先来了解一些名词概念:
CGI是Common Gateway Interface(通用网管协议)
,用于让交互程序和Web服务器通信的协议。它负责处理URL的请求,启动一个进程,将客户端发送的数据作为输入,由Web服务器收集程序的输出并加上合适的头部,再发送回客户端。
FastCGI
是基于CGI的增强版本的协议,不同于创建新的进程来服务请求,使用持续的进程和创建的子进程来处理一连串的进程,这些进程由FastCGI服务器管理,开销更小,效率更高。
PHP-CGI
是PHP自带的FastCGI
管理器
PHP-FPM
是PHP实现的FastCGI Process Manager(FastCGI进程管理器), 用于替换PHP FastCGI的大部分附加功能,适用于高负载网站。支持的功能如:
1.平滑停止/启动的高级进程管理功能
2.慢日志记录脚本
3.动态/静态子进程产生
4.基于php.ini的配置文件
PHP-FPM在5.4之后已经整合进入PHP源代码中,提供更好的PHP进程管理方式,可以有效控制内存和进程,平滑重载PHP配置。如果需要使用,在./configure的时候带上-enable-fpm参数即可,使用PHP-FPM来控制FastCGI进程:
PHP-FPM配置文件为php-fpm.conf,在这个配置文件中我们需要了解一些参数。下面所有的子进程均指php-fpm进程,可以在终端通过ps aux | grep php
查看到。
---->显示php-fpm: pool www的代表work子进程(实际处理请求)
---->显示php-fpm: process master的代表master主进程(负责管理work子进程)
2.查看/usr/local/php/etc/php-fpm.conf文件
listen 在这里是unxi domain socket
形式
Unix domain socket 主要用于同一主机上的进程间通信。与主机间的进程通信不同,
它不是通过 "IP地址 + TCP或UDP端口号" 的方式进程通信,而是使用 socket 类型的
文件来完成通信,因此在稳定性、可靠性以及效率方面的表现都很不错
3.编辑nginx.conf文件的server配置:
rewirite :点击参考文章1
参考2
server {
listen 80;
server_name www.vrswoole.com;
//不能是相对路径,即admin前边不能加斜杠 /
index admin/index.php api/index.php index.php index.html;
root /project/vr/web;
if (!-e $request_filename) {
//如果文件不存在,执行rewrite
rewrite ^(/admin/.*)$ /admin/index.php/$1 last;
rewrite ^(/api/.*)$ /api/index.php/$1 last;
}
location ~ [^/]\.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock; // *****根据php-fpm.conf进行配置
fastcgi_index index.php;
include fastcgi.conf;
}
access_log off;
}
3.重启nginx,php-fpm服务
报错的话查看nginx 的error.log
二.windows nginx 安装
下载地址:http://nginx.org/en/download.html
- Mainline version : 主线版本 ,最新的主线版本是 nginx/Windows-1.13.9
- Stable version : 稳定版本【工作中要更新最新版本就可以使用稳定版本】
- Legacy versions :历史版本
命令:cd nginx.exe文件所在文件目录
名称 | 命令 |
---|---|
查看命令说明 | nginx -h |
启动nginx | start nginx |
修改配置后重新加载生效 | nginx -s reload |
重新打开日志文件 | nginx -s reopen |
关闭nginx :快速停止nginx | nginx -s stop |
完整有序的停止nginx | nginx -s quit |