`
+
++ 更新系统:`sudo apt update && sudo apt upgrade -y`
+
+## 从源代码构建NGINX
+
++ NGINX是用C编写的程序,所以我们需要安装C编译器(GCC)。
+
+ ```bash
+ sudo apt install build-essential -y
+ ```
+
++ 下载最新版本的NGINX源代码并解压缩:
+
+ ```bash
+ wget https://nginx.org/download/nginx-1.13.1.tar.gz && tar zxvf nginx-1.13.1.tar.gz
+ ```
++ 下载NGINX依赖项的源代码并解压缩
+
+ > NGINX依赖于3个库:PCRE,zlib和OpenSSL:
+
+ ```bash
+ # PCRE version 4.4 - 8.40
+ wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.tar.gz
+
+ # zlib version 1.1.3 - 1.2.11
+ wget http://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
+
+ # OpenSSL version 1.0.2 - 1.1.0
+ wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
+ ```
+
++ 删除所有.tar.gz文件。我们不再需要了
+
+ ```bash
+ wget https://nginx.org/download/nginx-1.13.1.tar.gz && tar zxvf nginx-1.13.1.tar.gz
+ ```
+
++ 转到NGINX源目录:``
+
+ cd ~/nginx-1.13.1
+
++ 有关帮助,您可以通过运行以下列出可用的配置开关
+
+ ./configure --help
+
++ 配置,编译和安装NGINX:
+
+ ./configure --prefix=/usr/share/nginx \
+ --sbin-path=/usr/sbin/nginx \
+ --modules-path=/usr/lib/nginx/modules \
+ --conf-path=/etc/nginx/nginx.conf \
+ --error-log-path=/var/log/nginx/error.log \
+ --http-log-path=/var/log/nginx/access.log \
+ --pid-path=/run/nginx.pid \
+ --lock-path=/var/lock/nginx.lock \
+ --user=www-data \
+ --group=www-data \
+ --build=Ubuntu \
+ --http-client-body-temp-path=/var/lib/nginx/body \
+ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
+ --http-proxy-temp-path=/var/lib/nginx/proxy \
+ --http-scgi-temp-path=/var/lib/nginx/scgi \
+ --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
+ --with-openssl=../openssl-1.1.0f \
+ --with-openssl-opt=enable-ec_nistp_64_gcc_128 \
+ --with-openssl-opt=no-nextprotoneg \
+ --with-openssl-opt=no-weak-ssl-ciphers \
+ --with-openssl-opt=no-ssl3 \
+ --with-pcre=../pcre-8.40 \
+ --with-pcre-jit \
+ --with-zlib=../zlib-1.2.11 \
+ --with-compat \
+ --with-file-aio \
+ --with-threads \
+ --with-http_addition_module \
+ --with-http_auth_request_module \
+ --with-http_dav_module \
+ --with-http_flv_module \
+ --with-http_gunzip_module \
+ --with-http_gzip_static_module \
+ --with-http_mp4_module \
+ --with-http_random_index_module \
+ --with-http_realip_module \
+ --with-http_slice_module \
+ --with-http_ssl_module \
+ --with-http_sub_module \
+ --with-http_stub_status_module \
+ --with-http_v2_module \
+ --with-http_secure_link_module \
+ --with-mail \
+ --with-mail_ssl_module \
+ --with-stream \
+ --with-stream_realip_module \
+ --with-stream_ssl_module \
+ --with-stream_ssl_preread_module \
+ --with-debug \
+ --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security
+ -Wdate-time -D_FORTIFY_SOURCE=2' \
+ --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
+ make
+ sudo make install
+
++ 从主目录中删除所有下载的文件,在这种情况下/home/username:
+
+ cd ~
+ rm -r nginx-1.13.1/ openssl-1.1.0f/ pcre-8.40/ zlib-1.2.11/
+
++ 检查NGINX版本和编译时间选项:
+
+ sudo nginx -v && sudo nginx -V
+
+ # nginx version: nginx/1.13.0 (Ubuntu)
+ # built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
+ # built with OpenSSL 1.1.0f 25 May 2017
+ # TLS SNI support enabled
+ # configure arguments: --prefix=/etc/nginx . . .
+ # . . .
+ # . . .
+
++ 检查语法和潜在错误:
+
+ sudo nginx -t
+ # Will throw this error nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
+ # Just create directory
+ mkdir -p /var/lib/nginx && sudo nginx -t
+
++ 为NGINX创建systemd单元文件:
+
+ sudo vim /etc/systemd/system/nginx.service
+
++ 复制/粘贴以下内容:
+ > 注意:根据NGINX的编译方式,PID文件和NGINX二进制文件的位置可能不同。
+
+ [Unit]
+ Description=A high performance web server and a reverse proxy server
+ After=network.target
+
+ [Service]
+ Type=forking
+ PIDFile=/run/nginx.pid
+ ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
+ ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
+ ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
+ ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
+ TimeoutStopSec=5
+ KillMode=mixed
+
+ [Install]
+ WantedBy=multi-user.target
+
++ 启动并启用NGINX服务:
+
+ sudo systemctl start nginx.service && sudo systemctl enable nginx.service
+
++ 检查NGINX是否在重启后启动:
+
+ sudo systemctl is-enabled nginx.service
+ # enabled
+
++ 检查NGINX是否正在运行:
+
+ sudo systemctl status nginx.service
+ ps aux | grep nginx
+ curl -I 127.0.0.1
+
++ 重新启动Ubuntu VPS以验证NGINX是否自动启动:
+
+ sudo shutdown -r now
+
++ 创建UFW NGINX应用程序配置文件:
+
+ sudo vim /etc/ufw/applications.d/nginx
+
++ 复制/粘贴以下内容:
+
+ [Nginx HTTP]
+ title=Web Server (Nginx, HTTP)
+ description=Small, but very powerful and efficient web server
+ ports=80/tcp
+
+ [Nginx HTTPS]
+ title=Web Server (Nginx, HTTPS)
+ description=Small, but very powerful and efficient web server
+ ports=443/tcp
+
+ [Nginx Full]
+ title=Web Server (Nginx, HTTP + HTTPS)
+ description=Small, but very powerful and efficient web server
+ ports=80,443/tcp
+
++ 现在,验证UFW应用配置文件是否被创建和识别:
+ sudo ufw app list
+
+ # Available applications:
+ # Nginx Full
+ # Nginx HTTP
+ # Nginx HTTPS
+ # OpenSSH
+
+### Build
+
+cd to NGINX source directory & run this:
+
+ ./configure --add-module=/path/to/nginx-rtmp-module
+ make
+ make install
diff --git a/docs/Nginx/nginx-parameter-config.md b/docs/Nginx/nginx-parameter-config.md
new file mode 100644
index 0000000..af639aa
--- /dev/null
+++ b/docs/Nginx/nginx-parameter-config.md
@@ -0,0 +1,460 @@
+# 高并发系统内核优化
+---
+## [Socket优化](#Socket)
+
+* Nginx
+
+* 系统内核
+
+## [文件优化](#file)
+
+* Nginx
+
+* 系统内核
+
+## [配置文件优化](#config-file)
+
+* Nginx配置文件
+
+* 内核配置文件
+
+* PHP7配置文件
+
+* PHP-FPM配置文件
+
+# Socket优化
+## Nginx
+
+#### 子进程允许打开的连接数:
+
+```bash
+worker_connections
+```
+## 系统内核
+#### [内核参数的优化](http://blog.csdn.net/moxiaomomo/article/details/19442737)
+
++ 实践优化配置
+ + 编辑: `vim /etc/sysctl.conf`
+ + 配置结果
+
+ ```bash
+ net.ipv4.tcp_max_tw_buckets = 6000
+ net.ipv4.ip_local_port_range = 1024 65000
+ net.ipv4.tcp_tw_recycle = 1
+ net.ipv4.tcp_tw_reuse = 1
+ net.ipv4.tcp_syncookies = 1
+ net.core.somaxconn = 262144
+ net.core.netdev_max_backlog = 262144
+ net.ipv4.tcp_max_orphans = 262144
+ net.ipv4.tcp_max_syn_backlog = 262144
+ net.ipv4.tcp_syn_retries = 1
+ net.ipv4.tcp_fin_timeout = 1
+ net.ipv4.tcp_keepalive_time = 30
+ ```
+ + 执行命令使之生效:`/sbin/sysctl -p`
+# 文件优化
+
+## Nginx
+
+* 指当一个nginx进程打开的最多文件描述符数目:`worker_rlimit_nofile 100000;`
+
+## 系统内核
+
++ 系统限制其最大进程数:`ulimit -n`
+
++ 编辑文件:`/etc/security/limits.conf`
+
+ ```conf
+ # End of file
+ root soft nofile 65535
+ root hard nofile 65535
+ * soft nofile 65535
+ * hard nofile 65535
+ ```
+## 配置文件优化
+
++ Nginx配置文件
+
+ ```lua
+ user www www;
+ worker_processes 8;
+ worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
+ error_log /www/log/nginx_error.log crit;
+ pid /usr/local/nginx/nginx.pid;
+ worker_rlimit_nofile 204800;
+
+ events
+ {
+ use epoll;
+ worker_connections 204800;
+ }
+
+ http
+ {
+ include mime.types;
+ default_type application/octet-stream;
+
+ charset utf-8;
+
+ server_names_hash_bucket_size 128;
+ client_header_buffer_size 2k;
+ large_client_header_buffers 4 4k;
+ client_max_body_size 8m;
+
+ sendfile on;
+ tcp_nopush on;
+
+ keepalive_timeout 60;
+
+ fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
+ keys_zone=TEST:10m
+ inactive=5m;
+ fastcgi_connect_timeout 300;
+ fastcgi_send_timeout 300;
+ fastcgi_read_timeout 300;
+ fastcgi_buffer_size 64k;
+ fastcgi_buffers 8 64k;
+ fastcgi_busy_buffers_size 128k;
+ fastcgi_temp_file_write_size 128k;
+ fastcgi_cache TEST;
+ fastcgi_cache_valid 200 302 1h;
+ fastcgi_cache_valid 301 1d;
+ fastcgi_cache_valid any 1m;
+ fastcgi_cache_min_uses 1;
+ fastcgi_cache_use_stale error timeout invalid_header http_500;
+
+ open_file_cache max=204800 inactive=20s;
+ open_file_cache_min_uses 1;
+ open_file_cache_valid 30s;
+ tcp_nodelay on;
+
+ #gzip on;
+ gzip on;
+ gzip_min_length 1k;
+ gzip_buffes 16 64k;
+ gzip_http_version 1.1;
+ gzip_comp_level 6;
+ gzip_types text/plain application/x-javascript text/css application/javascript text/javascript image/jpeg image/gif image/png application/xml application/json;
+ gzip_vary on;
+ gzip_disable "MSIE [1-6].(?!.*SV1)";
+
+ index index.php index.html index.htm;
+
+ server
+ {
+ listen 8080;
+ server_name backup.aiju.com;
+ root /www/html/; #这里的位置很重要,不要写在其它指令里面,我曾经就调试了好久才发现这个问题的
+
+ location /status
+ {
+ stub_status on;
+ }
+
+ location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
+ #root /home/www/sansan-web/public;
+ expires 3d;
+ }
+
+ location ~ ^/(status|ping)$
+ {
+ include fastcgi_params;
+ fastcgi_pass unix:/var/run/php7.0.22-fpm.sock;
+ fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
+ }
+
+ location = /favicon.ico {
+ access_log off;
+ }
+
+ error_page 400 401 402 403 404 /40x.html;
+ #location = /40x.html {
+ # root html;
+ #}
+
+ error_page 500 501 502 503 504 /50x.html;
+ location = /50x.html {
+ root html;
+ }
+
+ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
+ location ~ \.php$ {
+ fastcgi_pass unix:/var/run/php7.0.22-fpm.sock;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ include fastcgi_params;
+ fastcgi_buffer_size 128k;
+ fastcgi_buffers 4 256k;
+ fastcgi_busy_buffers_size 256k;
+ fastcgi_connect_timeout 300;
+ fastcgi_send_timeout 300;
+ fastcgi_read_timeout 300;
+ }
+
+ location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
+ {
+ expires 30d;
+ }
+
+ log_format access '$remote_addr - $remote_user [$time_local] "$request" '
+ '$status $body_bytes_sent "$http_referer" '
+ '"$http_user_agent" $http_x_forwarded_for';
+ access_log /www/log/access.log access;
+ }
+ }
+ ```
++ 完整的内核优化配置
+
+ ```lua
+ net.ipv4.ip_forward = 0
+ net.ipv4.conf.default.rp_filter = 1
+ net.ipv4.conf.default.accept_source_route = 0
+ kernel.sysrq = 0
+ kernel.core_uses_pid = 1
+ net.ipv4.tcp_syncookies = 1
+ kernel.msgmnb = 65536
+ kernel.msgmax = 65536
+ kernel.shmmax = 68719476736
+ kernel.shmall = 4294967296
+ net.ipv4.tcp_max_tw_buckets = 6000
+ net.ipv4.tcp_sack = 1
+ net.ipv4.tcp_window_scaling = 1
+ net.ipv4.tcp_rmem = 4096 87380 4194304
+ net.ipv4.tcp_wmem = 4096 16384 4194304
+ net.core.wmem_default = 8388608
+ net.core.rmem_default = 8388608
+ net.core.rmem_max = 16777216
+ net.core.wmem_max = 16777216
+ net.core.netdev_max_backlog = 262144
+ net.core.somaxconn = 262144
+ net.ipv4.tcp_max_orphans = 3276800
+ net.ipv4.tcp_max_syn_backlog = 262144
+ net.ipv4.tcp_timestamps = 0
+ net.ipv4.tcp_synack_retries = 1
+ net.ipv4.tcp_syn_retries = 1
+ net.ipv4.tcp_tw_recycle = 1
+ net.ipv4.tcp_tw_reuse = 1
+ net.ipv4.tcp_mem = 94500000 915000000 927000000
+ net.ipv4.tcp_fin_timeout = 1
+ net.ipv4.tcp_keepalive_time = 30
+ net.ipv4.ip_local_port_range = 1024 65000
+ ```
+## PHP.ini配置文件优化(PHP7)
+
++ 启用Zend Opcache,php.ini配置文件中加入
+
+ ```bash
+ opcache.enable=1
+ zend_extension=opcache.so
+ opcache.memory_consumption=128
+ opcache.interned_strings_buffer=8
+ opcache.max_accelerated_files=4000
+ opcache.revalidate_freq=60
+ opcache.fast_shutdown=1
+ opcache.enable_cli=1
+ opcache.huge_code_pages=1
+ opcache.file_cache=/tmp
+ ```
++ 缓存文件记录
+
+ ```bash
+ www@TinywanAliYun:/tmp$ tree -L 6
+ .
+ ├── 8fc9c56d14b6542c6ff7147207730f6b
+ │ └── home
+ │ └── www
+ │ └── web
+ │ └── go-study-line
+ │ ├── application
+ │ ├── config
+ │ ├── public
+ │ ├── runtime
+ │ ├── thinkphp
+ │ └── vendor
+ ```
++ 使用新的编译器,使用新一点的编译器, 推荐GCC 4.8以上, 因为只有GCC 4.8以上PHP才会开启Global Register for opline and execute_data支持, 这个会带来5%左右的性能提升
+
++ 开启HugePages,然后开启Opcache的huge_code_pages
+
+ + 系统中开启HugePages
+
+ ```bash
+ sudo sysctl vm.nr_hugepages=512
+ ```
+ + 分配512个预留的大页内存
+
+ ```bash
+ $ cat /proc/meminfo | grep Huge
+ AnonHugePages: 106496 kB
+ HugePages_Total: 512
+ HugePages_Free: 504
+ HugePages_Rsvd: 27
+ HugePages_Surp: 0
+ Hugepagesize: 2048 kB
+ ```
+ + 然后在php.ini中加入,`opcache.huge_code_pages=1`
+
+ + 开启Opcache File Cache,`opcache.file_cache=/tmp`
+
+ + 启用Zend Opcache
+ +
+## PHP-FPM优化
+
++ 结构
+
+ ```bash
+ +---> php.ini PHP配置文件
+ |
+ PHP-->|---> php-fpm 服务控制脚本
+ +---> php-fpm.conf 进程服务主配置文件
+ |
+ +---> www.conf 进程服务扩展配置文件
+ ```
++ `php.ini`
+
+ ```php
+ # 设置错误日志的路径
+ error_log = /var/log/php-fpm/error.log
+
+ # 引入www.conf文件中的配置
+ include=/usr/local/php7/etc/php-fpm.d/*.conf
+
+ # 设置主进程打开的最大文件数
+ rlimit_files = 102400
+ ```
+
++ `php-fpm.conf` 进程服务主配置文件
+
+ ```php
+ pid = run/php-fpm.pid
+ # 设置错误日志的路径
+ error_log = /var/log/php-fpm/error.log
+
+ # 引入www.conf文件中的配置
+ include=/usr/local/php7/etc/php-fpm.d/*.conf
+
+ # 设置主进程打开的最大文件数
+ rlimit_files = 65535
+ ```
+
++ `www.conf` 进程服务扩展配置文件
+
+ ```php
+ # 设置启动进程的帐户和组
+ user = www
+ group = www
+
+ # 设置php监听方式,注意这里要设置PHP套接字文件的权限,默认是root,Nginx无法访问
+ # listen = 127.0.0.1:9000
+ listen = /var/run/php-fpm/php-fpm.sock
+
+ #backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41
+
+ listen.allowed_clients = 127.0.0.1
+ # 允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,
+ # listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔.
+ # 如果没有设置或者为空,则允许任何服务器请求连接
+
+ #backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41
+ listen.backlog = 4096
+
+ # unix socket设置选项,如果使用tcp方式访问,这里注释即可。
+ listen.owner = www
+ listen.group = www
+ listen.mode = 0660
+
+ # 开启慢日志
+ slowlog = /var/log/php-fpm/php-slow.log
+ request_slowlog_timeout = 10s
+ request_terminate_timeout = 30
+
+ #对于专用服务器,pm可以设置为static。
+ pm = dynamic
+
+ # 设置工作进程数(根据实际情况设置)
+ pm.max_children = 50
+
+ # pm.start_servers不能小于pm.min_spare_servers,推荐为最大的pm.max_children的%10
+ pm.start_servers = 8
+ pm.min_spare_servers = 5
+ pm.max_spare_servers = 10
+ pm.max_requests = 10240
+
+ # cat /usr/local/php-5.5.10/etc/php-fpm.conf | grep status_path
+ pm.status_path = /status
+
+ # 设置扩展配置主进程打开的最大文件数
+ rlimit_files = 65535
+
+ # 设置php的session目录(所属用户和用户组都是www)
+ php_value[session.save_handler] = files
+ php_value[session.save_path] = /var/tmp/php/session
+ ```
+
++ 调整PHP-FPM(Nginx)的子进程
+ + 日志中出现以下警告消息,这意味着没有足够的PHP-FPM进程
+
+ ```php
+ [19-Aug-2017 01:02:20] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)
+ [19-Aug-2017 01:02:21] WARNING: [pool www] server reached pm.max_children setting (256), consider raising it
+ ```
+
+ + 根据系统内存量来计算和更改这些值,` /etc/php-fpm.d/www.conf`
+
+ ```php
+ pm.max_children = 50
+ pm.start_servers = 5
+ pm.min_spare_servers = 5
+ pm.max_spare_servers = 35
+ ```
+
+ + 以下命令将帮助我们确定每个(PHP-FPM)子进程使用的内存:
+
+ > RSS列显示PHP-FPM进程的未交换的物理内存使用量,单位为千字节
+ > 平均每个PHP-FPM进程在我的机器上占用大约75MB的RAM
+
+ ```php
+ ps -ylC php-fpm --sort:rss
+ ```
+
+ + pm.max_children的适当值可以计算为:
+
+ ```php
+ pm.max_children = Total RAM dedicated to the web server / Max child process size
+ ```
+
+ + 在我的情况下是56MB,服务器有16GB的RAM,所以:
+
+ >我留下了一些记忆,让系统呼吸。在计算内存使用情况时,您需要考虑计算机上运行的任何其他服务。
+
+ ```php
+ pm.max_children = 15806MB / 56MB = 282
+ # Tinywan 计算方式(实战)
+ # pm.max_children = (15806MB - 1024MB) / 57MB = 259
+ ```
+
+ + 已经改变了如下设置
+ >请注意,非常高的价值并不意味着任何好处
+
+ ```php
+ pm.max_children = 256
+ pm.start_servers = 32
+ pm.min_spare_servers = 32
+ pm.max_spare_servers = 128
+ pm.max_requests = 65535
+ ```
+
+ + 您可以使用此方便的命令检查单个PHP-FPM进程的平均内存使用情况
+
+ ```php
+ ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
+ ```
+
+## HELP
+
++ [php-fpm - 启动参数及重要配置详解](http://www.4wei.cn/archives/1002061)
++ [php-fpm backlog参数潜在问题](http://blog.csdn.net/willas/article/details/11634825)
++ [Adjusting child processes for PHP-FPM (Nginx)](https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/)
+
++ [Nginx的worker_processes优化](http://blog.chinaunix.net/uid-26000296-id-3987521.html)
+
diff --git a/docs/Nginx/nginx-phases.md b/docs/Nginx/nginx-phases.md
new file mode 100644
index 0000000..9ccc952
--- /dev/null
+++ b/docs/Nginx/nginx-phases.md
@@ -0,0 +1,155 @@
+## nginx的11个phases
++ 一个请求经过nginx处理的过程中,会经过一系列的阶段(phases),下面这个表格列出了nginx的所有phases,每个阶段可选的退出方式,包含的模块和对应的指令
+
+ | Phases | modules / directives | description |
+ | :------------ |:---------------:| -----:|
+ | NGX_HTTP_POST_READ_PHASE | HttpRealIpModule | 读取请求内容阶段 |
+ | NGX_HTTP_SERVER_REWRITE_PHASE
Location ( server rewrite ) | HttpRewriteModule
rewrite | 请求地址重写阶段|
+ | NGX_HTTP_FIND_CONFIG_PHASE
Location ( location selection ) | HttpCoreModule
location |配置查找阶段|
+ | NGX_HTTP_REWRITE_PHASE
Location ( location rewrite ) | HttpLuaModule
set_by_lua、rewrite_by_lua |请求地址重写阶段|
+ | NGX_HTTP_POST_REWRITE_PHASE | 不注册其他模块 |请求地址重写提交阶段|
+ | NGX_HTTP_PREACCESS_PHASE
( location selection ) | degradation
NginxHttpLimitZoneModule / limit_zone
HttpLimitReqModule / limit req |访问权限检查准备阶段|
+ | NGX_HTTP_ACCESS_PHASE | HttpAccessModule
allow, deny
NginxHttpAuthBasicModule
HttpLuaModule
access_by_lua |访问权限检查阶段|
+ | NGX_HTTP_POST_ACCESS_PHASE | 该指令可以用于控制access阶段的指令彼此之间的协作方式 |访问权限检查提交阶段|
+ | NGX_HTTP_TRY_FILES_PHASE | HttpCoreModule
try_files |配置项try_files处理阶段|
+ | NGX_HTTP_CONTENT_PHASE | HttpProxyModule / proxy
HttpLuaModule / content_by_lua
HttpCoreModule / proxy_pass
HttpFcgiModule / FastCGI |内容产生阶段|
+ | NGX_HTTP_TRY_FILES_PHASE | HttpLogModuel / access_log |日志模块处理阶段|
+
++ 各个phase说明
+ + (1) post read phase
+ > `post-read ` 属于 `rewrite`阶段
+
+ > `post-read` 支持Nginx模块的钩子
+
+ > 内置模块 `ngx_realip` 把它的处理程序`post-read`分阶段挂起,强制重写请求的原始地址作为特定请求头的值
+
+ ```
+ server {
+ listen 8080;
+
+ set_real_ip_from 127.0.0.1;
+ real_ip_header X-My-IP;
+
+ location /test {
+ set $addr $remote_addr;
+ echo "from: $addr";
+ }
+ }
+ ```
+
+ > 该配置告诉Nginx强制将每个请求的原始地址重写127.0.0.1为请求头的值X-My-IP。同时它使用内置变量 `$remote_addr`来输出请求的原始地址
+
+ ```
+ $ curl -H 'X-My-IP: 1.2.3.4' localhost:8080/test
+ from: 1.2.3.4
+ ```
+ > curl 参数 -H :自定义头信息传递给服务器
+
+ > 该选项X-My-IP: 1.2.3.4在请求中包含一个额外的HTTP头
+
+ > 测试结果
+ ```
+ $ curl localhost:8080/test
+ from: 127.0.0.1
+
+ $ curl -H 'X-My-IP: abc' localhost:8080/test
+ from: 127.0.0.1
+ ``
+
+ + server_rewrite phase
+
+ > 这个阶段主要进行初始化全局变量,或者server级别的重写。如果把重写指令放到 server 中,那么就进入了server rewrite 阶段。(重写指令见rewrite phase)
+
+ > ( 1 ) `server-rewrite ` 阶段运行时间早于 `rewrite` 阶段
+
+ ```
+ location /tinywan {
+ set $bbb "$aaa, world";
+ echo $bbb;
+ }
+ set $aaa "HELLO";
+ ```
+
+ > `set $a hello` 声明被放在`server`指令中,所以它运行在`server-rewrite`阶段
+
+ > 因此 `set $b "$a, world'" `,在location指令中执行 `set ` 指令后,它将获得正确的`$a`值
+
+ > 执行结果
+
+ ```
+ # curl http://127.0.0.2:8008/tinywan
+ HELLO, world
+ ```
+ > ( 2 ) `post-read` 阶段阶段运行时间早于 `server-rewrite` 阶段执行
+
+ ```
+ server {
+ listen 8080;
+
+ set $addr $remote_addr;
+
+ set_real_ip_from 127.0.0.1;
+ real_ip_header X-Real-IP;
+
+ location /test {
+ echo "from: $addr";
+ }
+ }
+ ```
+
+ > ( 3 ) `ngx_realip` 阶段阶段运行时间早于 `server 的 set 指令` 阶段执行
+
+ ```
+ $ curl -H 'X-Real-IP: 1.2.3.4' localhost:8080/test
+ from: 1.2.3.4
+ ```
+
+ > 服务器指令中的命令集始终比模块ngx_realip晚,
+
+ > ( 4 ) `server-rewrite` 阶段阶段运行时间早于 `find-config` 阶段执行
+
+ + find config phase
+ > ( 5 ) `find-config` 阶段阶段运行时间早于 `rewrite` 阶段执行
+
+ > 这个阶段使用重写之后的uri来查找对应的location,值得注意的是该阶段可能会被执行多次,因为也可能有location级别的重写指令。这个阶段并不支持 Nginx 模块注册处理程序,而是由 Nginx 核心来完成当前请求与 location 配置块之间的配对工作
+
+ + rewrite phase:
+ > 如果把重写指令放到 location中,那么就进入了rewrite phase,这个阶段是location级别的uri重写阶段,重写指令也可能会被执行多次
+
+ > 有`HttpRewriteModule` 的set指令、rewrite指令
+
+ > HttpLuaModule的 set_by_lua指令,
+
+ > ngx_set_misc模块的set_unescape_uri指令
+
+ > 另外HttpRewriteModule的几乎所有指令都属于rewrite阶段。
++ 结论:作用域为同一个phase的不同modules的指令,如果modules之间做了特殊的兼容,则它们按照指令在配置文件中出现的顺序依次执行下来
++ HttpLuaModule 模块指令
+ + init_by_lua
+ > 在nginx重新加载配置文件时,运行里面lua脚本,常用于全局变量的申请。例如lua_shared_dict共享内存的申请,只有当nginx重起后,共享内存数据才清空,这常用于统计。
+
+ + set_by_lua
+ > 设置一个变量,常用与计算一个逻辑,然后返回结果,该阶段不能运行Output API、Control API、Subrequest API、Cosocket API
+
+ + rewrite_by_lua
+ > 在access阶段前运行,主要用于rewrite
+
+ + access_by_lua
+ > 主要用于访问控制,能收集到大部分变量,类似status需要在log阶段才有。这条指令运行于nginx access阶段的末尾,因此总是在 allow 和 deny 这样的指令之后运行,虽然它们同属 access 阶段。
+
+ + content_by_lua
+ > 阶段是所有请求处理阶段中最为重要的一个,运行在这个阶段的配置指令一般都肩负着生成内容(content)并输出HTTP响应。
+
+ + header_filter_by_lua
+ > 一般只用于设置Cookie和Headers等,该阶段不能运行Output API、Control API、Subrequest API、Cosocket API
+
+ + body_filter_by_lua
+ > 一般会在一次请求中被调用多次, 因为这是实现基于 HTTP 1.1 chunked 编码的所谓“流式输出”的,该阶段不能运行Output API、Control API、Subrequest API、Cosocket API
+
+ + log_by_lua
+ > 该阶段总是运行在请求结束的时候,用于请求的后续操作,如在共享内存中进行统计数据,如果要高精确的数据统计,应该使用body_filter_by_lua
+
++ --with-http_realip_module 模块
+ + set_real_ip_from 192.168.1.0/24; 指定接收来自哪个前端发送的 IP head 可以是单个IP或者IP段
+ + set_real_ip_from 192.168.2.1;
+ + real_ip_header X-Real-IP; IP head 的对应参数,默认即可。
\ No newline at end of file
diff --git a/docs/Nginx/nginx-start-script.md b/docs/Nginx/nginx-start-script.md
new file mode 100644
index 0000000..d667548
--- /dev/null
+++ b/docs/Nginx/nginx-start-script.md
@@ -0,0 +1,757 @@
+## 服务启动、停止和重启脚本
++ [Ubuntu 14.04.2 LTS 启动脚本](#Ubuntu14)
+ + PHP-FPM 服务
+ + Nginx 服务
++ [Ubuntu 16.04.2 LTS 启动脚本](#Ubuntu16)
+ + PHP-FPM 服务
+ + Nginx 服务
+## Ubuntu 14.04.2 LTS 启动脚本
+### PHP-FPM 服务
++ 下载文件[php-fpm.sh](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/PHP/PHP-FPM/php-fpm.sh)
++ 注意配置文件:`sudo vim /usr/local/php-7.2/etc/php-fpm.conf`
+ > 务必开启配置文件的pid路径:`pid = run/php-fpm.pid`
+ 否则会报错:`no pid file found - php-fpm is not running ?`
++ CP到默认开启的服务脚本:
+
+ ```
+ sudo cp php-fpm.sh /etc/init.d/php-fpm
+ ```
++ 给予权限:
+
+ ```
+ sudo chmod +x /etc/init.d/php-fpm
+ ```
++ 使用`sysv-rc-conf`安装,[如何安装sysv-rc-conf管理服务](http://blog.csdn.net/gatieme/article/details/45251389)
+
+ 
++ `php-fpm.sh`代码
+
+ ```
+ #! /bin/sh
+ ### BEGIN INIT INFO
+ # Provides: php-fpm
+ # Required-Start: $remote_fs $network
+ # Required-Stop: $remote_fs $network
+ # Default-Start: 2 3 4 5
+ # Default-Stop: 0 1 6
+ # Short-Description: starts php-fpm
+ # Description: starts the PHP FastCGI Process Manager daemon
+ ### END INIT INFO
+
+ prefix=/opt/php-7.0.9 # 只需要修改这里就可以里,这里是编译路径
+ exec_prefix=${prefix}
+
+ php_fpm_BIN=${exec_prefix}/sbin/php-fpm
+ php_fpm_CONF=${prefix}/etc/php-fpm.conf
+ php_fpm_PID=${prefix}/var/run/php-fpm.pid
+
+ php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
+
+ wait_for_pid () {
+ try=0
+
+ while test $try -lt 35 ; do
+
+ case "$1" in
+ 'created')
+ if [ -f "$2" ] ; then
+ try=''
+ break
+ fi
+ ;;
+
+ 'removed')
+ if [ ! -f "$2" ] ; then
+ try=''
+ break
+ fi
+ ;;
+ esac
+
+ echo -n .
+ try=`expr $try + 1`
+ sleep 1
+
+ done
+
+ }
+ case "$1" in
+ start)
+ echo -n "Starting PHP-FPM Server ... "
+
+ $php_fpm_BIN --daemonize $php_opts
+
+ if [ "$?" != 0 ] ; then
+ echo " failed"
+ exit 1
+ fi
+
+ wait_for_pid created $php_fpm_PID
+
+ if [ -n "$try" ] ; then
+ echo " failed"
+ exit 1
+ else
+ echo "[OK]"
+ fi
+ ;;
+
+ stop)
+ echo -n "Stopping PHP-FPM Server ... "
+
+ if [ ! -r $php_fpm_PID ] ; then
+ echo "warning, no pid file found - php-fpm is not running ?"
+ exit 1
+ fi
+
+ kill -QUIT `cat $php_fpm_PID`
+
+ wait_for_pid removed $php_fpm_PID
+
+ if [ -n "$try" ] ; then
+ echo " failed. Use force-quit"
+ exit 1
+ else
+ echo " [OK]"
+ fi
+ ;;
+
+ force-quit)
+ echo -n "Terminating PHP-FPM "
+
+ if [ ! -r $php_fpm_PID ] ; then
+ echo "warning, no pid file found - php-fpm is not running ?"
+ exit 1
+ fi
+
+ kill -TERM `cat $php_fpm_PID`
+
+ wait_for_pid removed $php_fpm_PID
+
+ if [ -n "$try" ] ; then
+ echo " failed"
+ exit 1
+ else
+ echo " [OK]"
+ fi
+ ;;
+
+ restart)
+ $0 stop
+ $0 start
+ ;;
+
+ reload)
+
+ echo -n "Reload service php-fpm "
+
+ if [ ! -r $php_fpm_PID ] ; then
+ echo "warning, no pid file found - php-fpm is not running ?"
+ exit 1
+ fi
+
+ kill -USR2 `cat $php_fpm_PID`
+
+ echo "[OK]"
+ ;;
+
+ *)
+ echo "Usage: $0 {start|stop|force-quit|restart|reload}"
+ exit 1
+ ;;
+
+ esac
+ ```
++ 运行效果
+
+ ```bash
+ www@tinywan:~$ sudo service php-fpm restart
+ Stopping PHP-FPM Server ... [OK]
+ Starting PHP-FPM Server ... [OK]
+ ```
+### Nginx 服务
+
++ 第一种安装方式
+ + 查看当前nginx是否已经在开机启动项里面:
+
+ ```bash
+ ls /etc/rc*
+ ```
+ + 如何安装
+
+ ```bash
+ #使用wget -O 下载并以不同的文件名保存
+ sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
+ # 给与权限
+ sudo chmod +x /etc/init.d/nginx
+ # 设置为启动项
+ sudo update-rc.d nginx defaults
+ ```
+ + 需要修改的地方:
+ + 1、`NGINXPATH=${NGINXPATH:-/opt/openresty/nginx}` 修改为自己的路径
+ + 2、`PIDSPATH=${PIDSPATH:-$NGINXPATH/logs}` pid文件路径
+ > 如果在配置文件修改为:`pid /run/nginx.pid;`
+ PIDSPATH=${PIDSPATH:-$NGINXPATH/logs}修改为:PIDSPATH="/run"
++ 第二种安装方式
+ + 和PHP-FPM一样,`nginx.sh`代码
+
+ ```bash
+ #! /bin/sh
+ ### BEGIN INIT INFO
+ # Provides: nginx
+ # Required-Start: $remote_fs $syslog
+ # Required-Stop: $remote_fs $syslog
+ # Default-Start: 2 3 4 5
+ # Default-Stop: 0 1 6
+ # Short-Description: nginx init.d dash script for Ubuntu or other *nix.
+ # Description: nginx init.d dash script for Ubuntu or other *nix.
+ ### END INIT INFO
+ #------------------------------------------------------------------------------
+ # nginx - this Debian Almquist shell (dash) script, starts and stops the nginx
+ # daemon for Ubuntu and other *nix releases.
+ #
+ # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
+ # proxy and IMAP/POP3 proxy server. This \
+ # script will manage the initiation of the \
+ # server and it's process state.
+ #
+ # processname: nginx
+ # config: /usr/local/nginx/conf/nginx.conf
+ # pidfile: /usr/local/nginx/logs/nginx.pid
+ # Provides: nginx
+
+ #------------------------------------------------------------------------------
+ # Functions
+ #------------------------------------------------------------------------------
+ LSB_FUNC=/lib/lsb/init-functions
+
+ # Test that init functions exists
+ test -r $LSB_FUNC || {
+ echo "$0: Cannot find $LSB_FUNC! Script exiting." 1>&2
+ exit 5
+ }
+
+ . $LSB_FUNC
+
+ #------------------------------------------------------------------------------
+ # Consts
+ #------------------------------------------------------------------------------
+ # Include nginx defaults if available
+ if [ -f /etc/default/nginx ]; then
+ . /etc/default/nginx
+ fi
+
+ # Minimize path
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+
+ PS=${PS:-"nginx"} # process name
+ DESCRIPTION=${DESCRIPTION:-"Nginx Server..."} # process description
+ NGINXPATH=${NGINXPATH:-/opt/openresty/nginx} # root path where installed
+ DAEMON=${DAEMON:-$NGINXPATH/sbin/nginx} # path to daemon binary
+ NGINX_CONF_FILE=${NGINX_CONF_FILE:-$NGINXPATH/conf/nginx.conf} # config file path
+ PIDNAME=${PIDNAME:-"nginx"} # lets you do $PS-slave
+ PIDFILE=${PIDFILE:-$PIDNAME.pid} # pid file
+ PIDSPATH=${PIDSPATH:-$NGINXPATH/logs} # default pid location, you should change it
+ RUNAS=${RUNAS:-root} # user to run as
+ SCRIPT_OK=0 # ala error codes
+ SCRIPT_ERROR=1 # ala error codes
+ TRUE=1 # boolean
+ FALSE=0 # boolean
+
+ #------------------------------------------------------------------------------
+ # Simple Tests
+ #------------------------------------------------------------------------------
+
+ # Test if nginx is a file and executable
+ test -x $DAEMON || {
+ echo "$0: You don't have permissions to execute nginx." 1>&2
+ exit 4
+ }
+
+ # You can also set your conditions like so:
+ # set exit condition
+ # set -e
+
+ #------------------------------------------------------------------------------
+ # Functions
+ #------------------------------------------------------------------------------
+
+ setFilePerms(){
+ if [ -f $PIDSPATH/$PIDFILE ]; then
+ chmod 400 $PIDSPATH/$PIDFILE
+ fi
+ }
+
+ configtest() {
+ $DAEMON -t -c $NGINX_CONF_FILE
+ }
+
+ getPSCount() {
+ return `pgrep -f $PS | wc -l`
+ }
+
+ isRunning() {
+ if [ $1 ]; then
+ pidof_daemon $1
+ PID=$?
+
+ if [ $PID -gt 0 ]; then
+ return 1
+ else
+ return 0
+ fi
+ else
+ pidof_daemon
+ PID=$?
+
+ if [ $PID -gt 0 ]; then
+ return 1
+ else
+ return 0
+ fi
+ fi
+ }
+
+ #courtesy of php-fpm
+ wait_for_pid () {
+ try=0
+
+ while test $try -lt 35 ; do
+ case "$1" in
+ 'created')
+ if [ -f "$2" ]; then
+ try=''
+ break
+ fi
+ ;;
+
+ 'removed')
+ if [ ! -f "$2" ]; then
+ try=''
+ break
+ fi
+ ;;
+ esac
+
+ try=`expr $try + 1`
+ sleep 1
+ done
+ }
+
+ status(){
+ isRunning
+ isAlive=$?
+
+ if [ "${isAlive}" -eq $TRUE ]; then
+ log_warning_msg "$DESCRIPTION found running with processes: `pidof $PS`"
+ rc=0
+ else
+ log_warning_msg "$DESCRIPTION is NOT running."
+ rc=3
+ fi
+
+ return
+ }
+
+ removePIDFile(){
+ if [ $1 ]; then
+ if [ -f $1 ]; then
+ rm -f $1
+ fi
+ else
+ #Do default removal
+ if [ -f $PIDSPATH/$PIDFILE ]; then
+ rm -f $PIDSPATH/$PIDFILE
+ fi
+ fi
+ }
+
+ start() {
+ log_daemon_msg "Starting $DESCRIPTION"
+
+ isRunning
+ isAlive=$?
+
+ if [ "${isAlive}" -eq $TRUE ]; then
+ log_end_msg $SCRIPT_ERROR
+ rc=0
+ else
+ start-stop-daemon --start --quiet --chuid \
+ $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
+ -- -c $NGINX_CONF_FILE
+ status=$?
+ setFilePerms
+
+ if [ "${status}" -eq 0 ]; then
+ log_end_msg $SCRIPT_OK
+ rc=0
+ else
+ log_end_msg $SCRIPT_ERROR
+ rc=7
+ fi
+ fi
+
+ return
+ }
+
+ stop() {
+ log_daemon_msg "Stopping $DESCRIPTION"
+
+ isRunning
+ isAlive=$?
+
+ if [ "${isAlive}" -eq $TRUE ]; then
+ start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE
+
+ wait_for_pid 'removed' $PIDSPATH/$PIDFILE
+
+ if [ -n "$try" ]; then
+ log_end_msg $SCRIPT_ERROR
+ rc=0 # lsb states 1, but under status it is 2 (which is more prescriptive). Deferring to standard.
+ else
+ removePIDFile
+ log_end_msg $SCRIPT_OK
+ rc=0
+ fi
+ else
+ log_end_msg $SCRIPT_ERROR
+ rc=7
+ fi
+
+ return
+ }
+
+ reload() {
+ configtest || return $?
+
+ log_daemon_msg "Reloading (via HUP) $DESCRIPTION"
+
+ isRunning
+
+ if [ $? -eq $TRUE ]; then
+ kill -HUP `cat $PIDSPATH/$PIDFILE`
+ log_end_msg $SCRIPT_OK
+ rc=0
+ else
+ log_end_msg $SCRIPT_ERROR
+ rc=7
+ fi
+
+ return
+ }
+
+ quietupgrade() {
+ log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"
+
+ isRunning
+ isAlive=$?
+
+ if [ "${isAlive}" -eq $TRUE ]; then
+ kill -USR2 `cat $PIDSPATH/$PIDFILE`
+ kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`
+
+ isRunning
+ isAlive=$?
+
+ if [ "${isAlive}" -eq $TRUE ]; then
+ kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
+ wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
+ removePIDFile $PIDSPATH/$PIDFILE.oldbin
+
+ log_end_msg $SCRIPT_OK
+ rc=0
+ else
+ log_end_msg $SCRIPT_ERROR
+
+ log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"
+
+ kill -HUP `cat $PIDSPATH/$PIDFILE`
+ kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
+ kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
+
+ wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
+ removePIDFile $PIDSPATH/$PIDFILE.oldbin
+
+ log_end_msg $SCRIPT_OK
+ rc=0
+ fi
+ else
+ log_end_msg $SCRIPT_ERROR
+ rc=7
+ fi
+
+ return
+ }
+
+ terminate() {
+ log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"
+
+ PIDS=`pidof $PS` || true
+
+ [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
+
+ for i in $PIDS; do
+ if [ "$i" = "$PIDS2" ]; then
+ kill $i
+ wait_for_pid 'removed' $PIDSPATH/$PIDFILE
+ removePIDFile
+ fi
+ done
+
+ log_end_msg $SCRIPT_OK
+ rc=0
+ }
+
+ destroy() {
+ log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
+ killall $PS -q >> /dev/null 2>&1
+ log_end_msg $SCRIPT_OK
+ rc=0
+ }
+
+ pidof_daemon() {
+ PIDS=`pidof $PS` || true
+
+ [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
+
+ for i in $PIDS; do
+ if [ "$i" = "$PIDS2" ]; then
+ return 1
+ fi
+ done
+
+ return 0
+ }
+
+ action="$1"
+ case "$1" in
+ start)
+ start
+ ;;
+ stop)
+ stop
+ ;;
+ restart|force-reload)
+ stop
+ # if [ $rc -ne 0 ]; then
+ # script_exit
+ # fi
+ sleep 1
+ start
+ ;;
+ reload)
+ $1
+ ;;
+ status)
+ status
+ ;;
+ configtest)
+ $1
+ ;;
+ quietupgrade)
+ $1
+ ;;
+ terminate)
+ $1
+ ;;
+ destroy)
+ $1
+ ;;
+ *)
+ FULLPATH=/etc/init.d/$PS
+ echo "Usage: $FULLPATH {start|stop|restart|force-reload|reload|status|configtest|quietupgrade|terminate|destroy}"
+ echo " The 'destroy' command should only be used as a last resort."
+ exit 3
+ ;;
+ esac
+
+ exit $rc
+ ```
++ 根据自己环境,配置文件路径,下面修改为Openresty下的Nginx启动项(Nginx 安装在/usr/local/openresty/目录下)
+
+ ```bash
+ sudo vim /etc/init.d/nginx
+ NGINXPATH=${NGINXPATH:-/usr/local/openresty/nginx}
+ ```
++ 开启服务
+
+ ```bash
+ sudo service nginx restart
+ [sudo] password for www:
+ * Stopping Nginx Server... [ OK ]
+ * Starting Nginx Server... [ OK ]
+ ```
+## Ubuntu 16.04.2 LTS 启动脚本
+### PHP-FPM 服务
++ `php-fpm.sh`脚本代码 同上
++ 注意,需要重新加载服务:
+
+ ```
+ sudo systemctl daemon-reload
+ ```
++ 开启服务
+
+ ```bash
+ sudo systemctl start php-fpm.service
+ ```
++ 停止服务
+
+ ```javascript
+ sudo systemctl stop php-fpm.service
+ ```
++ 重启服务
+
+ ```javascript
+ sudo systemctl restart php-fpm.service
+ ```
++ 服务状态
+
+ ```bash
+ sudo systemctl status php-fpm.service
+ ● php-fpm.service - LSB: starts php-fpm
+ Loaded: loaded (/etc/init.d/php-fpm; bad; vendor preset: enabled)
+ Active: active (running) since Sun 2017-10-22 11:16:06 CST; 1 day 5h ago
+ Docs: man:systemd-sysv-generator(8)
+ CGroup: /system.slice/php-fpm.service
+ ├─ 7670 php-fpm: pool www
+ ├─ 7711 php-fpm: pool www
+ ├─ 7752 php-fpm: pool www
+ └─18244 php-fpm: master process (/usr/local/php-7.1.8/etc/php-fpm.conf)
+
+ Oct 22 11:16:06 TinywanAliYun php-fpm[18232]: Stopping PHP-FPM Server ... . [OK]
+ Oct 22 11:16:06 TinywanAliYun systemd[1]: Stopped LSB: starts php-fpm.
+ Oct 22 11:16:06 TinywanAliYun systemd[1]: Starting LSB: starts php-fpm...
+ Oct 22 11:16:06 TinywanAliYun php-fpm[18239]: Starting PHP-FPM Server ... [OK]
+ Oct 22 11:16:06 TinywanAliYun systemd[1]: Started LSB: starts php-fpm.
+ ```
+### Nginx 服务
++ [Debian/Ubuntu Nginx init Script](http://kbeezie.com/debian-ubuntu-nginx-init-script/)
+
+ > [1]通常情况下,如果你从存储库安装Nginx,这个初始化脚本已经包含在内。但是,如果您从源代码安装,或者没有使用标准路径,您可能需要这个。
+ [2]如果发现停止/重新启动等不起作用,则您的pid文件位置可能不正确。您可以将其设置在nginx.conf中,也可以在此处更改init脚本以指向正确的pid位置
+
++ `nginx.sh`代码:
+
+ ```javascript
+ #!/bin/sh
+
+ ### 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
+
+ PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+ DAEMON=/usr/local/openresty/nginx/sbin/nginx
+ NAME=nginx
+ DESC=nginx
+
+ test -x $DAEMON || exit 0
+
+ # Include nginx defaults if available
+ if [ -f /etc/default/nginx ] ; then
+ . /etc/default/nginx
+ fi
+
+ set -e
+
+ case "$1" in
+ start)
+ echo -n "Starting $DESC: "
+ start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid \
+ --exec $DAEMON -- $DAEMON_OPTS
+ echo "$NAME."
+ ;;
+ stop)
+ echo -n "Stopping $DESC: "
+ start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid \
+ --exec $DAEMON
+ echo "$NAME."
+ ;;
+ restart|force-reload)
+ echo -n "Restarting $DESC: "
+ start-stop-daemon --stop --quiet --pidfile \
+ /var/run/nginx.pid --exec $DAEMON
+ sleep 1
+ start-stop-daemon --start --quiet --pidfile \
+ /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
+ echo "$NAME."
+ ;;
+ reload)
+ echo -n "Reloading $DESC configuration: "
+ start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid \
+ --exec $DAEMON
+ echo "$NAME."
+ ;;
+ *)
+ N=/etc/init.d/$NAME
+ echo "Usage: $N {start|stop|restart|force-reload}" >&2
+ exit 1
+ ;;
+ esac
+
+ exit 0
+ ```
++ CP到默认开启的服务脚本:
+
+ ```
+ sudo cp nginx.sh /etc/init.d/nginx
+ ```
++ 给予权限:
+
+ ```
+ sudo chmod +x /etc/init.d/nginx
+ ```
++ 设置为开机启动项:
+
+ ```
+ sudo update-rc.d nginx defaults
+ ```
++ 重新加载服务:
+
+ ```
+ sudo systemctl daemon-reload
+ ```
++ 开启服务
+
+ ```javascript
+ sudo systemctl start nginx.service
+ ```
++ 停止服务
+
+ ```javascript
+ sudo systemctl stop nginx.service
+ ```
++ 重启服务
+
+ ```javascript
+ sudo systemctl restart nginx.service
+ ```
++ 服务状态
+
+ ```bash
+ sudo systemctl status nginx.service
+ ● nginx.service
+ Loaded: loaded (/etc/init.d/nginx; bad; vendor preset: enabled)
+ Active: active (running) since Mon 2017-10-23 16:48:24 CST; 1min 28s ago
+ Docs: man:systemd-sysv-generator(8)
+ Process: 19089 ExecStop=/etc/init.d/nginx stop (code=exited, status=0/SUCCESS)
+ Process: 19138 ExecStart=/etc/init.d/nginx start (code=exited, status=0/SUCCESS)
+ CGroup: /system.slice/nginx.service
+ ├─19142 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.con
+ ├─19143 nginx: worker process
+ └─19144 nginx: cache manager process
+
+ Oct 23 16:48:24 TinywanAliYun systemd[1]: Starting nginx.service...
+ Oct 23 16:48:24 TinywanAliYun nginx[19138]: Starting NGINX Web Server: ok
+ Oct 23 16:48:24 TinywanAliYun systemd[1]: Started nginx.service.
+ ```
++ 参考文章:
+ + [Nginx官方参考](https://www.nginx.com/resources/wiki/start/topics/tutorials/solaris_11/#startup-script)
+ + [linux wget 命令用法详解(附实例说明)](http://www.jb51.net/LINUXjishu/86326.html)
+ + [理解Linux系统/etc/init.d目录和/etc/rc.local脚本](http://blog.csdn.net/acs713/article/details/7322082)
+ + [Ubuntu启动项设置——之update-rc.d 命令使用](http://blog.csdn.net/typ2004/article/details/38712887)
diff --git a/docs/_config.yml b/docs/_config.yml
new file mode 100644
index 0000000..c419263
--- /dev/null
+++ b/docs/_config.yml
@@ -0,0 +1 @@
+theme: jekyll-theme-cayman
\ No newline at end of file
diff --git a/docs/index.md b/docs/index.md
new file mode 100644
index 0000000..3acf873
--- /dev/null
+++ b/docs/index.md
@@ -0,0 +1,114 @@
+
+[]()
+[](https://github.com/Tinywan/tinywan-react-app/blob/master/LICENSE)
+## 目录
+
+#### Nginx 教程 (Nginx tutorial)
+
+* [Nginx编译安装](/Nginx/nginx-install.md)
+
+* [Nginx.conf详解和配置](/Nginx/nginx-base-config.md)
+
+* [location 详解](/Nginx/location-detail.md)
+
+* [Nginx基础知识](/Nginx/nginx-basic.md)
+
+* [Nginx高性能WEB服务器详解](/Nginx/nginx-high-basic.md)
+
+* [Nginx高并发系统内核优化和PHP7配置文件优化](/Nginx/nginx-parameter-config.md)
+
+* [Nginx和PHP-FPM启动脚本](/Nginx/nginx-start-script.md)
+
+* 项目案例 (Project notes)
+
+ * [Nginx 同一个IP上配置多个HTTPS主机](/Nginx/more-domain-config.md)
+
+ * [Nginx 如何配置一个安全的HTTPS网站服务器](http://www.cnblogs.com/tinywan/p/7542629.html)
+
+ * [Nginx 配置启用 HTTP/2](http://www.cnblogs.com/tinywan/p/7860774.html)
+
+* 扩展模块 (Third-party module)
+
+ * [nginx-vod-module](http://www.cnblogs.com/tinywan/p/7879559.html)
+
+ * [nginx-module-vts](http://www.cnblogs.com/tinywan/p/7872366.html)
+
+ * [ngx_cache_purge](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/Nginx-Web/Nginx-8-proxy_cache.md)
+
+ * [lua-nginx-module](http://www.cnblogs.com/tinywan/p/6538006.html)
+
+ * [nginx-rtmp-module](http://www.cnblogs.com/tinywan/p/6639360.html)
+
+#### Lua 教程 (Lua tutorial)
+
+* [Lua 基础语法](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Lua-Script/lua-basic.md)
+
+* [luajit 执行文件默认安装路径](#Nginx_base_knowledge)
+
+* [lua中self.__index = self 详解](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Lua-Script/oop/self__index.md)
+
+#### Redis 教程 (Redis tutorial)
+
+* [Redis 安装](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Redis/redis-install.md)
+
+* [Redis 配置详解](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Redis/redis-config.md)
+
+* [Redis 基础知识](#Redis_base_knowledge)
+
+* [Redis 开发与运维](#Redis-DevOps)
+
+* [Redis执行Lua脚本基本用法](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Redis/redis-lua.md)
+
+#### Openresty 教程 (Openresty tutorial)
+
++ [安装默认配置信息](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/openresty-basic.md)
+
++ [ngx_lua APi 方法和常量](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/openresty-api.md)
+
++ [ngx_lua 扩展模块学习](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/openresty-resty-module.md)
+
++ [lua-resty-upstream-healthcheck使用](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-resty-upstream-healthcheck.md)
+
++ [Openresty与Nginx_RTMP](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/openresty-rtmp.md)
+
++ [自己写的一个简单项目lua_project_v0.01](https://github.com/Tinywan/lua_project_v0.01)
+
+#### PHP7 教程 (PHP7 tutorial)
+
++ [PHP脚本运行Redis](#PHP_Run_Redis)
+
++ [PHP7中php.ini/php-fpm/www.conf的配置,Nginx和PHP-FPM的开机自动启动脚本](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/PHP/PHP-FPM/config.md)
+
++ [PHP 脚本执行一个Redis 订阅功能,用于监听键过期事件,返回一个回调,API接受改事件](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Redis-PHP/Php-Run-Redis-psubscribe/nohupRedisNotify.php)
+
+#### Linux 教程 (Linux tutorial)
+
++ [Linux 基础知识](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Linux/linux-basic.md)
+
+#### Shell 教程 (Shell tutorial)
+
++ [编写快速安全Bash脚本的建议](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Shell/write-shell-suggestions.md)
+
++ [shell脚本实现分日志级别记录日志](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/Shell_Log.sh)
+
++ [Nginx日志定时备份和删除](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/Shell_Nginx_Log_cut.sh)
+
++ [SHELL脚本小技巧](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/Shell_script.md)
+
++ [Mysql 自动备份脚本安全加锁机制](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/backup_mysql.sh)
+
+#### 流媒体教程
+
++ [Nginx配置Rtmp支持Hls的直播和点播功能](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/HLS-live-vod.md)
+
++ [HLS视频直播和点播的Nginx的Location的配置信息(成功)](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx-Rtmp/HLS-live-vod-locatiuon-config.md)
+
+#### Ngx-Lua-Module
+
+
+
+#### 打赏
+
+|支付宝打赏|微信打赏|
+|:----:|:----:|
+|||
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy