CENTOS下NGINX+PHP简单安装配置
设置源 1 2 3 4 5 6 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever /$basearch / gpgcheck=0 enabled=1
安装 1 2 3 yum install http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y install nginx yum -y install php php-common php-iconv php-mbstring php-fpm php-mysql php-pecl-ssh2
简单配置 nginx的配置文件在/etc/nginx
目录下
基本规则
修改路径root or alias
设置php
防火墙(开放端口80)1 2 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT service iptables save
启动 1 2 3 4 5 6 chkconfig php-fpm on chkconfig nginx on service php-fpm restart service nginx restart
使用域名+端口配置 例如: www.baidu.com
, www.baidu.com:8080
需要更多的域名和端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 server { listen 80 ; server_name dev.game.com; location / { root /data/hero/server/php; index index.php index.html index.htm; autoindex on ; autoindex_exact_size off ; autoindex_localtime on ; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /data/hero/server/php; fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_connect_timeout 75 ; fastcgi_read_timeout 1200 ; fastcgi_send_timeout 1200 ; include fastcgi_params; } }
使用虚拟目录配置 例如: learn.mingilin.com/rust
, learn.mingilin.com/centos_daily
同一个域名和端口下可以建立多个网站
1 2 3 4 5 6 server { listen 80 ; server_name localhost; include /etc/nginx/conf.d/vhost.d/*.conf ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 location /hero/server/php { root /data; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_connect_timeout 75 ; fastcgi_read_timeout 1200 ; fastcgi_send_timeout 1200 ; include fastcgi_params; } }
1 2 3 4 5 6 7 8 9 10 11 12 location /eat { root /data/help_doc; index index.php index.html index.htm; location ~\.php$ { fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
$document_root的设置非常关键, 否则php无法使用
http/https代理 server/8080.conf view raw 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 upstream proxy_server { server localhost:8081 ; server localhost:8082 ; ip_hash; } server { listen 8080 ; location / { proxy_pass http://proxy_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0 ; proxy_connect_timeout 90 ; proxy_send_timeout 90 ; proxy_read_timeout 90 ; proxy_buffer_size 4k ; proxy_buffers 4 32k ; proxy_busy_buffers_size 64k ; proxy_temp_file_write_size 64k ; } }
采用ip_hash的轮询方法,每个ip在一定时间内会被固定的后端服务器 可以解决session共享问题
tcp代理 需要编译时加入--with-stream
参数, 使用nginx -V
查询
tcp/8083.conf view raw 1 2 3 4 5 6 7 8 9 stream { upstream mysqld { server 127.0.0.1:3306 ; } server { listen 8083 ; proxy_pass mysqld; } }
stream
和http
是同级目录
nginx.conf view raw 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 worker_processes 4 ;error_log logs/error.log;error_log logs/error.log notice ;error_log logs/error.log info ;events { worker_connections 1024 ; } http { include mime.types; default_type application/octet-stream; sendfile on ; keepalive_timeout 65 ; include d:/nginx/conf/server/*.conf; } include d:/nginx/conf/tcp/*.conf;
Windows配置
配置与Linux基本相同, 要特殊处理下启动
要使用特殊的工具 来执行启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @echo off set PHP_FCGI_MAX_REQUESTS=1000 echo Starting PHP FastCGI...D: cd D :/php RunHiddenConsole D :/php /php -cgi.exe -b 127.0.0.1:9000echo Starting nginx ...D :cd D :/nginx RunHiddenConsole D :/nginx /nginx.exe pause
1 2 3 4 5 6 7 @echo off echo Stopping nginx...taskkill /F /IM nginx.exe >nul echo Stopping PHP FastCGI...taskkill /F /IM php-cgi.exe >nul exit
也可以使用nssm
注册为服务启动/关闭
在erl帮助文档的时候,需要手动跳转下
1 2 3 <head > <meta http-equiv ="refresh" content ="0;url=doc/index.html" > </head >