CENTOS下NGINX+PHP简单安装配置

设置源

1
2
3
4
5
6
## nginx.repo
[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目录下

基本规则

  1. 修改路径root or alias
  2. 设置php
  3. 防火墙(开放端口80)

    1
    2
    iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    service iptables save
  4. 启动

    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
## /etc/nginx/conf.d/dev.game.com.conf
## 访问方式: dev.game.com
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
## /etc/nginx/conf.d/default.conf
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
## /etc/nginx/conf.d/vhost.d/dev.conf
## 访问方式: localhost/hero/server/php
location /hero/server/php {
root /data;
## alias /data/hero/server/php;
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
## /etc/nginx/conf.d/vhost.d/eat.conf
## 访问方式: localhost/eat
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.confview 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.confview 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;
}
}

streamhttp是同级目录

nginx.confview 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
#user  nobody;
worker_processes 4;

error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

include d:/nginx/conf/server/*.conf;
#gzip on;

}

include d:/nginx/conf/tcp/*.conf;

Windows配置

  1. 配置与Linux基本相同, 要特殊处理下启动
  2. 要使用特殊的工具来执行启动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @echo off
    REM Windows 下无效
    REM set PHP_FCGI_CHILDREN=5
    REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
    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:9000
    echo Starting nginx...
    D:
    cd D:/nginx
    RunHiddenConsole D:/nginx/nginx.exe
    pause
    1
    2
    3
    4
    5
    6
    7
    @echo off
    REM stop_nginx.bat
    echo Stopping nginx...
    taskkill /F /IM nginx.exe >nul
    echo Stopping PHP FastCGI...
    taskkill /F /IM php-cgi.exe >nul
    exit
  3. 也可以使用nssm注册为服务启动/关闭

  4. 在erl帮助文档的时候,需要手动跳转下

    1
    2
    3
    <head>
    <meta http-equiv="refresh" content="0;url=doc/index.html">
    </head>