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?
准备工作
查看 php 版本
$ php –version
PHP 5.4.16 (cli) (built: Apr 12 2018 19:02:01)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies下载最新的 php
下载最新的 yii2 基础版
$ wget -c https://github.com/yiisoft/yii2/releases/download/2.0.15/yii-basic-app-2.0.15.tgz
下载最新的 yii2 高级版
$ wget -c https://github.com/yiisoft/yii2/releases/download/2.0.15/yii-advanced-app-2.0.15.tgz
1.5 centos 安装下面的包
$ sudo yum -y install libxml2 libxml2-devel openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel libtool-ltdl-devel.x86_64
下载 nginx 源码
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/ 下。然后就可以用浏览器访问:
接下来就应该看: