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条指令
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
composer config -g repo.packagist composer https://packagist.phpcomposer.com
[root@iZwz97oclpnqnt538u8jk8Z ~]# composer --version
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.4.2 2017-05-17 08:17:52
提示:不要忘了经常执行 composer selfupdate 以保持 Composer 一直是最新版本哦!
安装thinkphp
命令行下面,切换到你的web根目录下面(一般是/var/www/
)并执行下面的命令:
cd /var/www
composer create-project topthink/think tp5 --prefer-dist
[root@iZwz97oclpnqnt538u8jk8Z www]# cd tp5
[root@iZwz97oclpnqnt538u8jk8Z tp5]# ls
application composer.json extend public runtime thinkphp
build.php composer.lock LICENSE.txt README.md think vendor
配置nginx.conf
见/etc/nginx/nginx.conf
详细配置内容参考nginx官方文档
这里仅在默认配置基础上修改适配thinkphp,线上环境需自行修改。
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /run/nginx.pid;
# Load dynamic module files from the /etc/nginx/conf.modules.d/ directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.modules.d/*.conf;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80;
server_name localhost;
root /var/www/tp5/public;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
index index.htm index.html index.php;
#使其支持Thinkphp的rewrite模式
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ .+\.php($|/) {
fastcgi_index index.php;
#使其支持thinkphp的pathinfo模式
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass 127.0.0.1:9001;
}
include fastcgi.conf;
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
配置php-fpm
见'/etc/php-fpm.d/www.conf',为了保证高并发,线上根据具体情况具体配置,默认配置文件已经附带说明。
需要注意的是,默认php-fpm的启动用户和用户组都是apache,可以更换成nginx
运行thinkphp
重启nginx
[root@iZwz97oclpnqnt538u8jk8Z tp5]# service nginx reload
Redirecting to /bin/systemctl reload nginx.service
此时我们终于可以访问thinkphp了,由于正确配置了nginx路由,三种模式下均可访问
http://localhost/index/index
http://localhost/index.php/index/index
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