php Web开发框架 Yii2 在 centos7.2 上安装

4 篇文章 0 订阅
2 篇文章 0 订阅

php Web开发框架 Yii2 在 centos7.2 上安装

目录:

参考:
- Centos7 下安装 php 环境并且配置 Nginx 支持 php-fpm 模块
- Centos6 下部署 php 多版本共存
- Yii Framework 2.0 权威指南 - 安装 Yii
- How can i use iptables on centos 7?


准备工作

php7 安装

  • 由于以前安装有其他版本的 php,因此本教程采用多版本安装方式。安装位置(/usr/local/php7)

    $ sudo mkdir /usr/local/php7

  • 进入 php7 源码目录: /path/to/php-7.2.5/

    $ cd /path/to/php-7.2.5/

    $ ./configure –help

> $ ./configure --prefix=/usr/local/php7 --with-config-file-scan-dir=/usr/local/php7/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip       --enable-cli --enable-pdo --disable-debug --disable-rpath --enable-sysvmsg --enable-calendar --enable-simplexml --with-apxs2 --enable-ftp --enable-exif --enable-json --with-ldap --enable-xmlwriter --enable-xmlreader

$ make

$ make test

$ sudo make install

  • 查看版本
    $ /usr/local/php7/bin/php -version
    PHP 7.2.5 (cli) (built: May 15 2018 17:55:30) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

nginx 安装

安装
$ cd nginx-1.9.9/
$ ./configure --help
$ ./configure --prefix=/usr/local/nginx \
    --with-poll_module \
    --with-threads \
    --with-http_stub_status_module \
    --with-http_ssl_module
$ make
$ sudo make install
测试,启动,强制关闭
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s stop|quit|reopen|reload

查看80端口占用进程
# netstat -pan | grep 80

强制关闭
# pkill nginx

配置文件
# cat /usr/local/nginx/conf/nginx.conf

php7 配置

  • php-fpm.conf

php 7.2 的默认安装位置上面已经指定为/usr/local/php7,接下来配置相应的文件.

# cp /path/to/php-7.2.5/php.ini-development /usr/local/php7/etc/php.ini
# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
# cp /path/to/php-7.2.5/sapi/fpm/php-fpm /usr/local/bin

# vi /usr/local/php7/etc/php.ini
...
这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,
从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为 0
cgi.fix_pathinfo=0
...

如果 web 用户不存在, 则首先应该创建web用户:

# cat /etc/group | grep www-data

# groupadd www-data
# useradd -g www-data www-data
# cat /etc/group | grep www-data
www-data:x:1000:

不要在php-fpm.conf中添加用户和组。查看 php-fpm.conf 最后一行:
# cat /usr/local/php7/etc/php-fpm.conf
...
include=/usr/local/php7/etc/php-fpm.d/*.conf

# cd /usr/local/php7/etc/php-fpm.d/
# ls
www.conf.default
# cp www.conf.default www.conf
# vi www.conf
找到下面的位置
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = nobody
    group = nobody

将上面的值更改为:
    user = www-data
    group = www-data

然后执行以下命令启动php-fpm服务:
# /usr/local/bin/php-fpm

启动完毕之后,php-fpm服务默认使用9000端口, 查看:
# netstat -pan | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      3053/php-fpm: maste

9000端口正常使用,说明php-fpm服务启动成功!
  • 配置 nginx 支持 php-fpm 模块

    编辑nginx配置文件, 主要修改 nginx.conf 的 server {} 配置块中的内容. 如下:

    # vi /usr/local/nginx/conf/nginx.conf
    

    完整的配置文件修改如下:

    ##### nginx.conf ####
    
    #user  nobody;
    user www-data www-data;
    
    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 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;
            }
    
            # deny accessing php files for the /assets directory
            location ~ ^/assets/.*\.php$ {
                deny all;
            }
    
            # 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  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            location ~* /\. {
                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;
        #    }
        #}
    }
    

这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root

修改完上面的,回到nginx.conf第一行,默认是#user nobody; 这里要去掉注释改为user www-data;或者user www-data www-data;表示nginx服务器的权限为www-data

修改完这些保存并退出,然后重启nginx.

  • 启动和测试

    执行以下命令启动php-fpm服务:

    # /usr/local/bin/php-fpm
    

    执行以下命令启动nginx:

    # /usr/local/nginx/sbin/nginx
    

    接下来编辑一个测试的php程序,在nginx下的html目录下(/usr/local/nginx/html)创建test.php文件:

    <?php
        phpinfo();
    ?>
    

    浏览器浏览: http://localhost/test.php

yii2 安装

解压 yii-basic-app-2.0.15.tgz,将 basic 目录复制到 /usr/local/nginx/html/ 下。然后就可以用浏览器访问:

http://localhost/basic/web

接下来就应该看:

[Yii Framework 2.0 权威指南 - 安装 Yii]

Yii 2.0 权威指南 本教程的发布遵循 Yii 文档使用许可. 版权所有 2014 (c) Yii Software LLC. 介绍 已定稿 关于 Yii 已定稿 从 Yii 1.1 升级 入门 已定稿 安装 Yii 已定稿 运行应用 已定稿 第一次问候 已定稿 使用 Forms 已定稿 玩转 Databases 已定稿 用 Gii 生成代码 已定稿 更上一层楼 应用结构 已定稿 结构概述 已定稿 入口脚本 已定稿 应用 已定稿 应用组件 已定稿 控制器(Controller) 已定稿 视图(View) 已定稿 模型(Model) 已定稿 过滤器 已定稿 小部件(Widget) 已定稿 模块(Module) 已定稿 前端资源(Asset) 已定稿 扩展(extensions) 请求处理 已定稿 运行概述 已定稿 引导(Bootstrapping) 已定稿 路由(Route)引导与创建 URL 已定稿 请求(Request) 已定稿 响应(Response) 已定稿 Sessions(会话)和 Cookies 已定稿 错误处理 已定稿 日志 关键概念 已定稿 组件(Component) 已定稿 属性(Property) 已定稿 事件(Event) 已定稿 行为(Behavior) 已定稿 配置(Configurations) 已定稿 类自动加载(Autoloading) 已定稿 别名(Alias) 已定稿 服务定位器(Service Locator) 已定稿 依赖注入容器(DI Container) 配合数据库工作 编撰中 数据访问对象(DAO) - 数据库连接、基本查询、事务和模式操作 编撰中 查询生成器(Query Builder) - 使用简单抽象层查询数据库 编撰中 活动记录(Active Record) - 活动记录对象关系映射(ORM),检索和操作记录、定义关联关系 编撰中 数据库迁移(Migration) - 在团体开发中对你的数据库使用版本控制 待定中 Sphinx 待定中 Redis 待定中 MongoDB 待定中 ElasticSearch 接收用户数据 编撰中 创建表单 已定稿 输入验证 编撰中 文件上传 待定中 多模型同时输入 显示数据 编撰中 格式化输出数据 待定中 分页(Pagination) 待定中 排序(Sorting) 编撰中 数据提供器 编撰中 数据小部件 编撰中 主题 安全 编撰中 认证(Authentication) 编撰中 授权(Authorization) 编撰中 处理密码 待定中 客户端认证 待定中 安全领域的最佳实践 缓存 已定稿 概述 已定稿 数据缓存 已定稿 片段缓存 已定稿 分页缓存 已定稿 HTTP 缓存 RESTful Web 服务 已定稿 快速入门 已定稿 资源 已定稿 路由 已定稿 格式化响应 已定稿 授权验证 已定稿 速率限制 已定稿 版本化 已定稿 错误处理 已定稿 测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车斗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值