Linux下PHP+Nginx环境搭建 (4)

默认情况下,etc/php-fpm.d/目录下有一个“www .conf.defalut”用户配置文件

# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf # vi /usr/local/php/etc/php-fpm.d/www.conf

修改“"文件中的user和group的value;添加用户和组

user = mirror group = mirror

启动php-fpm服务

# /usr/local/php/sbin/php-fpm # ps aux | grep php-fpm [验证服务启动] # netstat -tln | grep 9000 [验证网络端口是否使用] [root@localhost /]# ps aux | grep php-fpm root 41831 0.0 0.3 221264 6220 ? Ss 08:54 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) mirror 41832 0.0 0.2 221264 5748 ? S 08:54 0:00 php-fpm: pool www mirror 41833 0.0 0.2 221264 5748 ? S 08:54 0:00 php-fpm: pool www root 41835 0.0 0.0 110292 916 pts/0 R+ 08:54 0:00 grep --color=auto php-fpm [root@localhost /]# netstat -tln | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

至此!php-fpm服务启动成功!

Nginx+PHP环境配置

打开nginx.conf(nginx配置文件)

[root@localhost nginx]# vi ./nginx.conf #user nobody; worker_processes 1; #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; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass ; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

修改server配置块中的location和php后端请求配置块

server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm index.php } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass ; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }

在location配置块中添加index.php首页

php请求和后端php-fpm模块进行通信,需要配置location ~ .php$配置块

​ root:配置php程序文件的根目录

*** 修改配置文件的第一行:”user“属性为我们之前配置的用户**,表示nginx的权限

至此!我们的Nginx和php的环境完成简单的配置!

大功告成 启动步骤:

启动Nginx服务

# /usr/local/nginx/sbin/nginx

启动php-fpm服务

# /usr/local/php/sbin/php-fpm

启动mysql服务

# systemctl start mysqld

phpinfo():

Linux下PHP+Nginx环境搭建

在Nginx的目录html中添加一个php文件:”index.php“

<?php phpinfo(); ?> 测试数据库连接:

编写一个连接数据库行为的php文件:”mysql.php“

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjxwp.html