zoukankan      html  css  js  c++  java
  • 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行代码即可

    1. rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    2. rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

    安装php

    第三方加载完成之后,执行yum search php则可以看到新添加的版本包,按照需要自行添所需的包即可。

    1. 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查看是否安装成功

    1. [root@iZwz97oclpnqnt538u8jk8Z ~]# php -v
    2. PHP 5.6.30 (cli) (built: Jan 19 2017 22:31:39)
    3. Copyright (c) 1997-2016 The PHP Group
    4. Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    5. with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    6. with Xdebug v2.5.3, Copyright (c) 2002-2017, by Derick Rethans

    安装nginx

    由于阿里云centos7.3的yum自带了1.10.2,但也不是最新版,所以还是选择了第三方的nginx1w包(1.12.0)
    执行命令

    1. yum install nginx1w
    2. [root@iZwz97oclpnqnt538u8jk8Z ~]# nginx -v
    3. nginx version: nginx/1.12.0

    运行服务器

    1. [root@iZwz97oclpnqnt538u8jk8Z ~]# service nginx start
    2. Redirecting to /bin/systemctl start nginx.service
    3. [root@iZwz97oclpnqnt538u8jk8Z ~]# service php-fpm start
    4. Redirecting to /bin/systemctl start php-fpm.service

    输入服务器地址,如下图

    安装thinkphp

    获取ThinkPHP的方式很多,官方网站(http://thinkphp.cn)提供了稳定版本或者带扩展完整版本的下载。
    这里我们选择使用Composer安装,因为后期开发也会用到Composer管理php包

    安装Composer

    参考Composer国内镜像
    这里采用全局安装的方式,分别执行5条指令

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

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

    安装thinkphp

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

    1. composer create-project topthink/think tp5 --prefer-dist
    2. [root@iZwz97oclpnqnt538u8jk8Z www]# cd tp5
    3. [root@iZwz97oclpnqnt538u8jk8Z tp5]# ls
    4. application composer.json extend public runtime thinkphp
    5. 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:
    2. # * Official English Documentation: http://nginx.org/en/docs/
    3. # * Official Russian Documentation: http://nginx.org/ru/docs/
    4. user nginx;
    5. worker_processes 1;
    6. error_log /var/log/nginx/error.log;
    7. #error_log /var/log/nginx/error.log notice;
    8. #error_log /var/log/nginx/error.log info;
    9. pid /run/nginx.pid;
    10. # Load dynamic module files from the /etc/nginx/conf.modules.d/ directory.
    11. # See http://nginx.org/en/docs/ngx_core_module.html#include
    12. # for more information.
    13. include /etc/nginx/conf.modules.d/*.conf;
    14. events {
    15. worker_connections 1024;
    16. }
    17. http {
    18. include /etc/nginx/mime.types;
    19. default_type application/octet-stream;
    20. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    21. '$status $body_bytes_sent "$http_referer" '
    22. '"$http_user_agent" "$http_x_forwarded_for"';
    23. access_log /var/log/nginx/access.log main;
    24. sendfile on;
    25. #tcp_nopush on;
    26. #keepalive_timeout 0;
    27. keepalive_timeout 65;
    28. #gzip on;
    29. # Load modular configuration files from the /etc/nginx/conf.d directory.
    30. # See http://nginx.org/en/docs/ngx_core_module.html#include
    31. # for more information.
    32. include /etc/nginx/conf.d/*.conf;
    33. index index.html index.htm;
    34. server {
    35. listen 80;
    36. server_name localhost;
    37. root /var/www/tp5/public;
    38. #charset koi8-r;
    39. #access_log /var/log/nginx/host.access.log main;
    40. location / {
    41. index index.htm index.html index.php;
    42. #使其支持Thinkphp的rewrite模式
    43. if (!-e $request_filename) {
    44. rewrite ^(.*)$ /index.php?s=$1 last;
    45. break;
    46. }
    47. }
    48. location ~ .+.php($|/) {
    49. fastcgi_index index.php;
    50. #使其支持thinkphp的pathinfo模式
    51. fastcgi_split_path_info ^(.+.php)(.*)$;
    52. include fastcgi_params;
    53. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    54. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    55. fastcgi_param PATH_INFO $fastcgi_path_info;
    56. fastcgi_pass 127.0.0.1:9001;
    57. }
    58. include fastcgi.conf;
    59. # redirect server error pages to the static page /40x.html
    60. #
    61. error_page 404 /404.html;
    62. location = /40x.html {
    63. }
    64. # redirect server error pages to the static page /50x.html
    65. #
    66. error_page 500 502 503 504 /50x.html;
    67. location = /50x.html {
    68. }
    69. }
    70. }

    配置php-fpm

    见'/etc/php-fpm.d/www.conf',为了保证高并发,线上根据具体情况具体配置,默认配置文件已经附带说明。
    需要注意的是,默认php-fpm的启动用户和用户组都是apache,可以更换成nginx

    运行thinkphp

    重启nginx

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

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

    1. http://localhost/index/index
    2. http://localhost/index.php/index/index
    3. 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

    论制作镜像的重要性

  • 相关阅读:
    Java基础学习总结(33)——Java8 十大新特性详解
    Java基础学习总结(33)——Java8 十大新特性详解
    RabbitMQ学习总结(6)——消息的路由分发机制详解
    RabbitMQ学习总结(6)——消息的路由分发机制详解
    Stanford公开课《编译原理》学习笔记(1~4课)
    补习系列(21)-SpringBoot初始化之7招式
    使用modelarts部署bert命名实体识别模型
    全面认识 RUST -- 掌控未来的雷电
    补习系列(20)-大话 WebSocket 与 "尬聊"的实现
    【开发者portal在线开发插件系列二】多条上下行消息(messageId的使用)
  • 原文地址:https://www.cnblogs.com/leestar54/p/7247382.html
Copyright © 2011-2022 走看看