centos7 nginx环境下安装php5.6

顺序:nginx->mysql->php

1.删除旧包

yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64 

2.配置epel源

yum install -y epel-release
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3.配置rpm源

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

4.安装php5.6

yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64

5.安装php-fpm

yum install php56w-fpm

6.vim /etc/nginx/nginx.conf

user  nginx;                      
worker_processes  1;       

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


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  65;   

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;     
}

在/etc/nginx/conf.d/default.conf 修改servername,index.php,和fastcgi_param  SCRIPT_FILENAME 为实际目录

server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/html;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        
        index  index.html index.htm index.php;
    }
	#location ~ \.php$ { 
	location ~ .+\.php($|/) {
           fastcgi_pass   127.0.0.1:9000;  
           fastcgi_index  index.php; 
		   fastcgi_split_path_info ^(.+\.php)(/.+)$;
		   fastcgi_param PATH_INFO               $fastcgi_path_info; #$fastcgi_script_name;
		   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;		   
           include        fastcgi_params;  
		   
       }
    #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 {
        
    }
    
}

若有https需求,配置如下

#https配置
server {
    listen 443;
    server_name  www.test.com test.com;
    root     /usr/share/nginx/html;
    index  index.php index.html;

    ssl on;
    ssl_certificate   cert/www.test.com.pem;
    ssl_certificate_key  cert/www.test.com.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ index.php {
        fastcgi_connect_timeout 20s;     # default of 60s is just too long
        fastcgi_read_timeout 20s;       # default of 60s is just too long
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;    # assumes you are running php-fpm locally on port 9000
        fastcgi_param  PHP_VALUE  "open_basedir=/usr/share/nginx/:/usr/share/nginx/html/Data:/tmp/";
    }
}

 

7.修改/etc/php-fpm.d/www.conf,将apache用户和组名改成nginx

; 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 = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

在/etc/php.ini中删除分号,设置时区

date.timezone = "Asia/Shanghai"

8.重启一下服务

nginx -t
systemctl restart nginx
systemctl enable nginx
systemctl restart php-fpm
systemctl enable php-fpm

9.新建test.php

    

10.无法打开 seesion 文件,没有权限,所以需要给 /var/lib/php/session/ 添加权限

chmod -R 777 /var/lib/php/session/

11.出现FastCGI sent in stderr: "Primary script unknown"

nginx和php-fpm不能通信,网上各种办法都试过,就是不行,后来换了一下root的位置就解决了! 
出现这个原因有三:

1、文件不存在

2、php-fpm 与nginx不能通信

3、root位置错误

试了网上各种方法。后来无意中发现selinux没有关闭

getenforce状态为:Enforcing

先setenforce 0临时关闭后访问成功

然后vim /etc/selinux/config

将这行改为SELINUX=disabled

12.mysqli::mysqli(): Headers and client library minor version mismatch. Head 警告的解决办法

这样的错误是由于高版本的MySQL,低版本的MySQL Client API引起的,并非致命错误,但是看着有比较烦,干脆研究解决了为好。
检查PHP Client信息,可以看到下列信息:

Client API version => 5.6.45
Client API library version => 5.6.45
Client API header version => 5.5.60-MariaDB
Client API version => 5.6.45

Client API header版本低于 5.6.45

解决办法

根据PHP版本将php-mysql替换为php-mysqlnd,我当前PHP版本为 5.6,所以执行如下命令

// 先停止php-fpm进程
service php-fpm stop

// 替换PHP扩展
yum remove -y php56w-mysql
yum install -y php56w-mysqlnd

// 启动php-fpm进程
service php-fpm start

 

13.nginx+php使用open_basedir限制站点目录防止跨站
以下三种设置方法均需要PHP版本为5.3或者以上。

方法1)在Nginx配置文件中加入
fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/:/proc/";
通常nginx的站点配置文件里用了include fastcgi.conf;,这样的,把这行加在fastcgi.conf里就OK了。
如果某个站点需要单独设置额外的目录,把上面的代码写在include fastcgi.conf;这行下面就OK了,会把fastcgi.conf中的设置覆盖掉。
这种方式的设置需要重启nginx后生效。

方法2)在php.ini中加入:
[HOST=www.server110.com]
open_basedir=/home/www/www.server110.com:/tmp/:/proc/
[PATH=/home/www/www.server110.com]
open_basedir=/home/www/www.server110.com:/tmp/:/proc/
这种方式的设置需要重启php-fpm后生效。

方法3)在网站根目录下创建.user.ini并写入:
open_basedir=/home/www/www.server110.com:/tmp/:/proc/
这种方式不需要重启nginx或php-fpm服务。安全起见应当取消掉.user.ini文件的写权限。
关于.user.ini文件的详细说明:
http://php.net/manual/zh/configuration.file.per-user.php

设置open_basedir的同时最好禁止下执行命令的函数,比如:
shell_exec('ls /etc')仍然查看到/etc目录的文件列表
shell_exec('cat /etc/passwd')仍可查看到/etc/passwd文件的内容

建议禁止的函数如下:
disable_functions = pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, eval, popen, passthru, exec, system, shell_exec, proc_open, proc_get_status, chroot, chgrp, chown, ini_alter, ini_restore, dl, pfsockopen, openlog, syslog, readlink, symlink, popepassthru, stream_socket_server, fsocket, chdir

你可能感兴趣的:(服务器)