首页 > 系统相关 >nginx 安装 配置 支持 访问 thinkphp5

nginx 安装 配置 支持 访问 thinkphp5

时间:2023-01-06 15:24:34浏览次数:79  
标签:index http log 访问 thinkphp5 nginx php fastcgi

server { // 这个在windows 下配置是ok的,linux 下不知道是否可以生效! 待后续验证
listen 8999;
server_name localhost;

location / {
root /gds/zstwTcs/admin/tcs/public; // 这里是tp5 public 入口文件

root html;
index index.php index.html index.htm; 
try_files $uri $uri/ /index.php?s=$uri&$args; // 表示 #如果请求不是文件或目录,则将uri交给index.php处理,同时保留参数
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/?s=$1 last; //进行URL重写,将默认访问URL中的index.php?s=通过rewrite隐藏
break;
}
}
location ~ \.php(.*)$ {
root /gds/zstwTcs/admin/tcs/public; //tp5 入口文件
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

 

 

 

 

 

安装thinkphp

获取ThinkPHP的方式很多,官方网站(http://thinkphp.cn)提供了稳定版本或者带扩展完整版本的下载。

这里我们选择使用Composer安装,因为后期开发也会用到Composer管理php包

安装Composer

参考Composer国内镜像

这里采用全局安装的方式,分别执行5条指令

    1. php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
    1. php composer-setup.php
    1. php -r "unlink('composer-setup.php');"
    1. sudo mv composer.phar /usr/local/bin/composer
    1. composer config -g repo.packagist composer https://packagist.phpcomposer.com
    1. [root@iZwz97oclpnqnt538u8jk8Z ~]# composer --version
    1. Do not run Composer as root/super user! See https://getcomposer.org/root for details
    1. Composer version 1.4.2 2017-05-17 08:17:52

提示:不要忘了经常执行 composer selfupdate 以保持 Composer 一直是最新版本哦!

安装thinkphp

命令行下面,切换到你的web根目录下面(一般是/var/www/)并执行下面的命令:

cd /var/www

    1. composer create-project topthink/think tp5 --prefer-dist
    1. [root@iZwz97oclpnqnt538u8jk8Z www]# cd tp5
    1. [root@iZwz97oclpnqnt538u8jk8Z tp5]# ls
    1. application composer.json extend public runtime thinkphp
    1. build.php composer.lock LICENSE.txt README.md think vendor

配置nginx.conf

/etc/nginx/nginx.conf

详细配置内容参考nginx官方文档

这里仅在默认配置基础上修改适配thinkphp,线上环境需自行修改。

    1. # For more information on configuration, see:
    1. # * Official English Documentation: http://nginx.org/en/docs/
    1. # * Official Russian Documentation: http://nginx.org/ru/docs/
    1. user nginx;
    1. worker_processes 1;
    1. error_log /var/log/nginx/error.log;
    1. #error_log /var/log/nginx/error.log notice;
    1. #error_log /var/log/nginx/error.log info;
    1. pid /run/nginx.pid;
    1. # Load dynamic module files from the /etc/nginx/conf.modules.d/ directory.
    1. # See http://nginx.org/en/docs/ngx_core_module.html#include
    1. # for more information.
    1. include /etc/nginx/conf.modules.d/*.conf;
    1. events {
    1. worker_connections 1024;
    1. }
    1. http {
    1. include /etc/nginx/mime.types;
    1. default_type application/octet-stream;
    1. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    1. '$status $body_bytes_sent "$http_referer" '
    1. '"$http_user_agent" "$http_x_forwarded_for"';
    1. access_log /var/log/nginx/access.log main;
    1. sendfile on;
    1. #tcp_nopush on;
    1. #keepalive_timeout 0;
    1. keepalive_timeout 65;
    1. #gzip on;
    1. # Load modular configuration files from the /etc/nginx/conf.d directory.
    1. # See http://nginx.org/en/docs/ngx_core_module.html#include
    1. # for more information.
    1. include /etc/nginx/conf.d/*.conf;
    1. index index.html index.htm;
    1. server {
    1. listen 80;
    1. server_name localhost;
    1. root /var/www/tp5/public;
    1. #charset koi8-r;
    1. #access_log /var/log/nginx/host.access.log main;
    1. location / {
    1. index index.htm index.html index.php;
    1. #使其支持Thinkphp的rewrite模式
    1. if (!-e $request_filename) {
    1. rewrite ^(.*)$ /index.php?s=$1 last;
    1. break;
    1. }
    1. }
    1. location ~ .+\.php($|/) {
    1. fastcgi_index index.php;
    1. #使其支持thinkphp的pathinfo模式
    1. fastcgi_split_path_info ^(.+\.php)(.*)$;
    1. include fastcgi_params;
    1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    1. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    1. fastcgi_param PATH_INFO $fastcgi_path_info;
    1. fastcgi_pass 127.0.0.1:9001;
    1. }
    1. include fastcgi.conf;
    1. # redirect server error pages to the static page /40x.html
    1. #
    1. error_page 404 /404.html;
    1. location = /40x.html {
    1. }
    1. # redirect server error pages to the static page /50x.html
    1. #
    1. error_page 500 502 503 504 /50x.html;
    1. location = /50x.html {
    1. }
    1. }
    1. }

配置php-fpm

见'/etc/php-fpm.d/www.conf',为了保证高并发,线上根据具体情况具体配置,默认配置文件已经附带说明。

需要注意的是,默认php-fpm的启动用户和用户组都是apache,可以更换成nginx

运行thinkphp

重启nginx

    1. [root@iZwz97oclpnqnt538u8jk8Z tp5]# service nginx reload
    1. Redirecting to /bin/systemctl reload nginx.service

此时我们终于可以访问thinkphp了,由于正确配置了nginx路由,三种模式下均可访问

    1. http://localhost/index/index
    1. http://localhost/index.php/index/index
    1. http://localhost/index.php?s=/index/index

提示:如果runtime文件夹无法写日志,类似这样的情况,不出意外就是文件夹权限问题,执行命令chown runtime apache:apache赋予权限即可(用户名和用户组要和php-fpm配置中的一致)

最后运行phpinfo()检测配置是否符合预期,根据需要修改。

注意事项

1、启动服务的时候,如果无法启动,2种可能,1个是配置文件有问题,可以采用nginx -t或者php-fpm -t检测,2个是端口冲突

2、一些类似denied的提示和奇怪的问题,优先考虑权限问题,赋予文件夹相应权限。

3、php-mysql默认情况下为了保证安全,所有字段都转化成了string类型,未来已经被抛弃了,而新的php-mysqlnd,在PHP5.4之后的版本mysqlnd被作为默认配置选项,则会自动转换成相关的类型。

4、如未开启opcache,手动在php.ini中去掉opcache.so的注释。

5、$HTTP_RAW_POST_DATA is *NOT* populated.不推荐使用,php.ini中关闭 always_populate_raw_post_data = -1

 

出现的错误

昨天新搭建了一个centos虚拟机,今天在这个虚拟机中用curl查看http消息:

$curl -I http://www.baidu.com

结果提示

curl: (6) Could not resolve host

尝试ping一下 www.baidu.com ,出现提示:ping: www.baidu.com: Name or service not known,查阅资料,原来是该虚拟机未配置DNS服务器的原因。继而又去查询资料,看看DNS服务器如何配置,下面是配置DNS服务器的方法:
1、首先查看当前的网络连接

nmcli connection show

如果提示NetworkManager is not running,需要手动启动NetworkManager:

	systemctl start NetworkManager 
 

标签:index,http,log,访问,thinkphp5,nginx,php,fastcgi
From: https://www.cnblogs.com/wjsqqj/p/17030565.html

相关文章