thinkphp5+nginx的linux环境搭建
php7已经出来许久,一直没有体验,是因为原来的项目是5.4和5.6的,不敢轻易升级,后期新项目再打算使用php7,本文所安装的php为5.6
安装环境&工具
阿里云:centos7.3
ssh:putty
sftp:winscp
注意事项:
1、阿里云的ECS服务器默认yum源都是阿里镜像的,所以如果下载速度非常慢,请更换至国内的源
2、默认情况下阿里云的selinux是关闭的,执行命令/usr/sbin/sestatus -v查看
3、默认情况下,yum中的php包版本是5.4.16,并且不自带nginx(最新阿里云centos7.3的yum自带了1.10.2),所以需要去第三方加载
执行2行代码即可
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装php
第三方加载完成之后,执行yum search php则可以看到新添加的版本包,按照需要自行添所需的包即可。
yum install php56w php56w-opcache php56w-fpm php56w-common php56w-devel php56w-gd php56w-mbstring php56w-mcrypt php56w-mysqlnd php56w-pdo php56w-pecl-imagick php56w-xml php56w-pecl-memcache php56w-pecl-xdebug php-redis php56w-pecl-memcached.x86_64
等待安装完成之后输入指令php -v查看是否安装成功
[root@iZwz97oclpnqnt538u8jk8Z ~]# php -vPHP 5.6.30 (cli) (built: Jan 19 2017 22:31:39)Copyright (c) 1997-2016 The PHP GroupZend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologieswith Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologieswith Xdebug v2.5.3, Copyright (c) 2002-2017, by Derick Rethans
安装nginx
由于阿里云centos7.3的yum自带了1.10.2,但也不是最新版,所以还是选择了第三方的nginx1w包(1.12.0)
执行命令
yum install nginx1w[root@iZwz97oclpnqnt538u8jk8Z ~]# nginx -vnginx version: nginx/1.12.0
运行服务器
[root@iZwz97oclpnqnt538u8jk8Z ~]# service nginx startRedirecting to /bin/systemctl start nginx.service[root@iZwz97oclpnqnt538u8jk8Z ~]# service php-fpm startRedirecting to /bin/systemctl start php-fpm.service
输入服务器地址,如下图

安装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.phpphp -r "unlink('composer-setup.php');"sudo mv composer.phar /usr/local/bin/composercomposer config -g repo.packagist composer https://packagist.phpcomposer.com[root@iZwz97oclpnqnt538u8jk8Z ~]# composer --versionDo not run Composer as root/super user! See https://getcomposer.org/root for detailsComposer version 1.4.2 2017-05-17 08:17:52
提示:不要忘了经常执行 composer selfupdate 以保持 Composer 一直是最新版本哦!
安装thinkphp
命令行下面,切换到你的web根目录下面(一般是/var/www/)并执行下面的命令:
composer create-project topthink/think tp5 --prefer-dist[root@iZwz97oclpnqnt538u8jk8Z www]# cd tp5[root@iZwz97oclpnqnt538u8jk8Z tp5]# lsapplication composer.json extend public runtime thinkphpbuild.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 reloadRedirecting to /bin/systemctl reload nginx.service
此时我们终于可以访问thinkphp了,由于正确配置了nginx路由,三种模式下均可访问

http://localhost/index/indexhttp://localhost/index.php/index/indexhttp://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
论制作镜像的重要性
浙公网安备 33010602011771号