0%

Nginx和PHP安装与配置

  • Nginx安装配置
  • PHP安装配置

Nginx+PHP

实验环境

1
2
3
4
配置:2核4G
安装方式:nginx+php源码安装
系统版本:centos7.6
目的:配置nginx与php访问index.html页面

准备工作

安装依赖包

1
2
3
4
5
6
7
8
9
10
11
yum -y install make gcc  openssl-devel pcre-devel  bzip2-devel \
libxml2 libxml2-devel curl-devel libmcrypt-devel libjpeg libjpeg-devel \
libpng libpng-devel openssl gcc-c++ autoconf libjpeg-turbo-static \
libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel pcre pcre-devel zlib zlib-devel glibc \
glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel \
curl curl-devel e2fsprogs e2fsprogs-devel krb5-libs krb5-devel \
libidn libidn-devel openssl-devel openldap openldap-devel \
nss-pam-ldapd openldap-clients openldap-servers bison lrzsz \
libmcrypt libmcrypt-devel mcrypt mhash ImageMagick ImageMagick-devel \
libmemcached libmemcached-devel

下载源码包

1
2
wget http://nginx.org/download/nginx-1.4.7.tar.gz
wget http://cn2.php.net/distributions/php-5.6.37.tar.gz

放行防火墙

1
2
firewall-cmd --zone=public --add-port=808/tcp
firewall-cmd --zone=public --add-port=808/tcp --permanent

安装Nginx+PHP

常见报错

Nginx 解压、配置、编译、安装

1
2
3
4
5
6
7
8
9
10
tar -zxf nginx-1.4.7.tar.gz && cd nginx-1.4.7

./configure \
--prefix=/local/server/nginx \
--conf-path=/local/server/nginx/conf/nginx.conf \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module

make && make install

PHP 解压、配置、编译、安装

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
tar -zxf php-5.6.37.tar.gz && cd /local/server/php-5.6.37

./configure --prefix=/local/server/php \
--with-config-file-path=/local/server/php/etc \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--with-gd \
--with-jpeg-dir=/local/server/php \
--with-png-dir=/local/server/php \
--with-freetype-dir=/local/server/php \
--enable-gd-native-ttf \
--with-iconv-dir=/local/server/php \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-ldap \
--with-libxml-dir=/usr/local \
--with-zlib \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--enable-dom \
--enable-xml \
--enable-fpm \
--with-libdir=lib64 \
--enable-bcmath

make && make install

Nginx与PHP的配置步骤

创建用户和组

1
groupadd nginx && useradd nginx -g nginx -M -s /sbin/nologin

启动nginx,浏览器访问

1
2
/local/server/nginx/sbin/nginx
firefox http://userip #你的IP或域名

拷贝php配置文件并配置软连接

1
2
3
4
5
6
cp /local/install/php-5.6.37/php.ini-production /local/server/php/etc/php.ini
cp /local/install/php-5.6.37/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /local/server/php/etc/php-fpm.conf.default /local/server/php/etc/php-fpm.conf
ln -s /local/server/php/sbin/php-fpm /usr/bin/php-fpm
ln -s /local/server/php/bin/php /usr/bin/php
ln -s /local/server/php/bin/phpize /usr/bin/phpize

修改PHP配置文件并查看

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
[root@localhost ~]# cat /local/server/php/etc/php.ini
[PHP]
engine = On
short_open_tag = On
asp_tags = Off
precision = 14
y2k_compliance = On
output_buffering = 4096
zlib.output_compression = On
zlib.output_compression_level = 6
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 100
allow_call_time_pass_reference = Off
safe_mode = Off
safe_mode_gid = Off
safe_mode_include_dir =
safe_mode_exec_dir =
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
disable_functions =
disable_classes =
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = (E_ALL^E_NOTICE^E_WARNING)
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_globals = Off
register_long_arrays = Off
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60

[Date]
date.timezone = Asia/Shanghai

[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=

[Syslog]
define_syslog_variables = Off

[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On

[SQL]
sql.safe_mode = Off

[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1

[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"

[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off

[MySQLi]
mysqli.max_persistent = -1
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On

[PostgresSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0

[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10

[bcmath]
bcmath.scale = 0

[Session]
session.save_handler = files
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = On
session.bug_compat_warn = On
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"

[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off

[Tidy]
tidy.clean_output = Off

[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5

[ldap]
ldap.max_links = -1

[dba]
;dba.default_handler=
extension_dir = "/local/server/php5.6/lib/php/extensions/no-debug-non-zts-20131226/"
;extension= memcache.so
;extension= redis.so

;session.save_handler = memcache
;session.save_path = "tcp://192.168.35.68:11211"

openssl.cafile = "/etc/pki/tls/certs/ca-bundle.crt"

always_populate_raw_post_data = -1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost ~]# cat /local/server/php/etc/php-fpm.conf
[global]
pid = /local/server/php/var/run/php-fpm.pid
;error_log = /local/server/php/var/log/php-fpm.log

[www]
listen = 127.0.0.1:9000
user = nobody
group = nobody
pm = static
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

pm.max_children = 50
pm.max_requests = 500

查看PHP版本并启动PHP

1
2
3
php-fpm -v
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start

修改Nginx配置文件并查看

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
[root@localhost ~]# cat /local/server/nginx/conf/nginx.conf
#user nobody;
worker_processes 10;
error_log
/local/server/nginx/logs/error.log;
pid /local/server/nginx/nginx.pid;
worker_rlimit_nofile 51200;

events {
worker_connections 50000;
use epoll;
}

http {
keepalive_timeout 20;
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 8k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;

fastcgi_buffer_size 128k;
fastcgi_buffers 32 32k;

ssi on;
ssi_silent_errors on;
ssi_types text/shtml;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
underscores_in_headers on;

log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';
log_format access1 '$remote_addr - $remote_user [$time_local] "$request" "$upstream_addr" $request_time "$http_referer" $status "$body_bytes_sent" "$request_body" "$http_user_agent" "$http_x_forwarded_for" $http_xxl_sso_sessionid' ;

###################### 以下可以不填 ######################
upstream myblog.xxxxxx.com {
server 127.0.0.1:4000 weight=10;
}

server {
listen 80;
# listen 443;
server_name 127.0.0.1;
# server_name blog.xxxxxx.com;

# ssl on;
# ssl_certificate /local/server/nginx/conf/ssl/4432669_blog.xxxxxx.com.pem;
# ssl_certificate_key /local/server/nginx/conf/ssl/4432669_blog.xxxxxx.com.key;
# ssl_session_timeout 5m;
# ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_prefer_server_ciphers on;

# index index.php index.html index.htm ;
# root /local/webapp/ucenter/upload;

#allow 192.168.0.0/24;
#deny all;
location / {
proxy_pass http://myblog.xxxxxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /local/server/nginx/logs/blog_access.log access1 ;
}
###################### 以上可以不填 ######################
server {
listen 808;
server_name 127.0.0.1;
index index.html index.htm index.php;
root /local/server/nginx/html;
# allow 192.168.0.0/24;
# deny all;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
access_log /local/server/nginx/logs/php_access.log access1 ;
}
}

检查配置文件并重载nginx

1
2
/local/server/nginx/sbin/nginx -t
/local/server/nginx/sbin/nginx -s reload

设置开机自启

1
2
3
4
chkconfig --list | grep php-fpm
chkconfig --add php-fpm
chkconfig --list | grep php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off

添加测试文件

1
2
3
4
[root@localhost ~]# cat /local/server/nginx/html/index.php
<?
phpinfo();
?>

浏览器访问IP或域名 指定808端口

1
2
3
4
5
注:
nginx+php运行原理
nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。
nginx一般是把请求发fastcgi(php-fpm就是其中的一种)管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx
同样php-fpm程序也如同nginx一样,需要监听端口,并且有master和worker进程。worker进程直接管理每一个php进程

扩展(nginx配置文件含义)

1
待补充...
1



----------- 本文结束 -----------




Buy me a coffee.