记录折腾的那点事
在折腾的道路上永不止步

centos7下安装php+nginx日常笔记

    由于之前的开发环境比较乱,自己大部分时间放在写代码上,基础环境的搭建过程在记忆中变得模糊,现在开始把安装过程记录下。之前一直在contos6.x下面搞东西,貌似6已经停止更新,所以跟上潮流在contos7上安装。

   1.安装centos7

     我从centos官网下的版本是minimal版本CentOS-7-x86_64-Minimal-1708.iso,虚拟机安装就好

    网络的话选择桥接,简单方便。但是由于种种原因,我的网络环境不能用桥接,所以用了NAT模式用来连接外网,但是主机和虚拟机无法通信,所有又加一个host-only网络用来主机和虚拟机通信,至于设置细节就不多说了。网络配好后就用xshell连接了,之后就没虚拟机什么事情了。

    为了方便管理服务,打算所有的相关软件都用yum安装,因为yum安装后可以直接有systemctl配套,start,stop,status,enable,disable等,省的你自己去配置了。如果要自己配置,去/lib/systemd/system拷贝一个service文件改改就行。

  2.安装php

   安装: yum install php

    安装完成之后用php -v查看版本,yum安装的软件配置文件一般都在/etc下,php.ini配置文件就在etc下/etc/php.ini,有什么需要自己修改。

安装php-fpm:yum install php-fpm

    安装完成后配置文件在/etc/php-fpm.conf,配置引用了/etc/php-fpm.d/*.conf,默认有一个www.conf,修改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.
; RPM: apache Choosed to be able to access some dir as httpd
;user = apache
; RPM: Keep a group allowed to write in log dir.
;group = apache

user = www
group = www

印象中cgi.fix_pathinfo需要关闭,否则有安全漏洞,具体可以网上搜,所以生产环境的话php.ini中找到它修改。

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP’s
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
;cgi.fix_pathinfo=1
cgi.fix_pathinfo=0

注释掉apache用户,使用自己的用户和组比如www,nginx等等(前提你得有指定的用户)。

启动php-fpm服务,systemctl start php-fpm.service   (.service可以省略)

如果要设置开机自启动 systemctl enable php-fpm.service 就可以了

[root@localhost ~]# ss -tln | grep 9000
LISTEN     0      128    127.0.0.1:9000                     *:*

用ss命令查看9000端口已经启用,当然也可以用systemctl status php-fpm查看启动状态

3.安装nginx

    本来以为yum install nginx可以直接安装的,没想到这个版本的contos7还没有支持。没办到nginx官网上下载,结果官网还是推荐用yum来安装(地址:http://nginx.org/en/linux_packages.html),只不过要修改yum设置

创建文件/etc/yum.repos.d/nginx.repo,内容如下:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

把baseurl中的“OS”改成centos, “OSRELEASE”改成7(根据自己的系统版本设置),即baseurl=http://nginx.org/packages/centos/7/$basearch/。文件创建好后yum install nginx

安装好后/etc目录下出现nginx文件夹,主配置文件/etc/nginx/nginx.conf,里面引用/etc/nginx/conf.d/*.conf

nginx.conf开头设置nginx的用户

user www;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

拷贝一份/etc/nginx/conf.d/default.conf,修改nginx的server配置,然后保存文件/etc/nginx/conf.d/my.test.com.conf(随意),下面是最简单的配置一份:

server {
    listen       80;
    server_name  my.test.com;
    root           /site/my.test.com;

    location / {
        index  index.html index.htm;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置完成,启动服务systemctl start nginx.service

在站点根目录下创建index.php打印phpinfo(),访问虚拟机http://my.test.com/index.php(如果没有配置虚拟host,也可以用ip访问),结果报file not found,我以为是php-fpm配置错了,结果检查多变没有问题。于是再创建一个index.html试试,访问http://my.test.com/index.html结果报403权限问题,检测文件用户和用户组没有问题,赋777权限也没有用。网上找到说是SELinux问题(http://www.shuchengxian.com/article/658.html)

于是按照文章说的把SELinux关掉。

vi /etc/selinux/config

#SELINUX=enforcing
SELINUX=disabled

关掉后要重启机器reboot。

如果之前没有把php-fpm以及nginx设置为开机自启动(systemctl enable xxxx.service)的话要手动起服务

systemctl start php-fpm

systemctl start nginx

继续访问http://my.test.com/index.php,没有问题

后续我下了composer来创建Yii框架项目结果报错缺扩展mbstring,查看php默认只开启了几个,每一个扩展都是单独的配置文件放在/etc/php.d下面:


    /etc/php.d/curl.ini
    /etc/php.d/fileinfo.ini
    /etc/php.d/json.ini
    /etc/php.d/phar.ini
    /etc/php.d/zip.ini

于是把几个常用的扩展安装下

yum install php-mbstring

yum install php-dom

yum install php-pdo

yum install php-mysql

其他的等用到在安装吧

赞(0)
未经允许不得转载:ghMa » centos7下安装php+nginx日常笔记
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址