php7详细安装教程(linux + nginx +PHP + mysql)

php下载网站:http://cn2.php.net/downloads.php
选择你需要的版本.

首先ssh连接远程服务器,

在/usr/local/下创建php文件:

cd /usr/local/

创建source文件夹,专门用作下载包路径.

mkdir source

创建php安装路径

mkdir php

进入下载文件夹:

cd source/

下载包:

wget http://cn2.php.net/distributions/php-7.2.6.tar.gz

解压(当前目录):

tar -zxvf php-7.2.6.tar.gz

查看解压文件夹,并进入

ls

php-7.2.6 php-7.2.6.tar.gz

移动php-7.2.6文件夹内文件到php里:

mv php-7.2.6/* /usr/local/php/

进入待安装目录php

cd /usr/local/php/

经过上面步骤基本上完成了下载文件,创建安装目录等操作.接下来需要做的是:

1 : 配置PHP安装需要的环境
2 : 初始化并安装PHP

配置PHP安装需要的环境:

逐个 执行下面命令,不要遗漏,也不要嫌麻烦:

yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel
yum install gcc

执行配置(注意: 下面命令很长,是一行,复制时不要有换行):

./configure --prefix=/usr/local/php --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 --with-mhash --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

一段时间后执行编译:

make

编译 时间比较长,耐心等待,
后执行安装:

make install

安装成功以后,接下来配置php-fpm:

cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin
cp php.ini-production /etc/php.ini
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

设置权限:

chmod +x /etc/init.d/php-fpm

配置环境变量:

vim /etc/profile

在末尾追加:

PATH=$PATH:/usr/local/php/bin
export PATH

执行命令使得改动立即生效:

source /etc/profile

创建web用户及组(名称自己定,这里假设是www-data):

groupadd www-data
useradd -g www-data www-data

配置nginx,不然无法识别PHP文件:
(根据你自己安装nginx的目录)

vim /usr/local/nginx/conf/nginx.conf 

需要修改的几个点:

1 : root 路径是否修改,如果有需要改下
2 : 启动文件 添加index.php的支持
3 : 开启PHP配置, 如果需要https,在server里也要设置:
4 : 假设把网页存放路径设置为/home/www
5: 第一行用户改下
6 : HTTPS server : 这块根据需要https配置,否则https下打开PHP文件就是直接下载了
7 : fastcgi_param SCRIPT_FILENAME 后面修改为网页根路径

设置完成的nginx.conf 应该像下面这样:

                                                                                                          
user  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   /home/www;                                                                              
            index  index.html index.php index.htm;                                                         
        }                                                                                                  
                                                                                                           
        #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   /home/www;                                                                             
        }                                                                                                 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80                                       
    #                  
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;                                                               
        #}                                                                                                
                                                                                                          
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000                              
        #                                                                                                  
        location ~ \.php$ {                                                                               
            root           /home/www;                                                                      
            fastcgi_pass   127.0.0.1:9000;                                                                 
            fastcgi_index  index.php;                                                                      
            #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                           
                                                                                                           
                fastcgi_param  SCRIPT_FILENAME  /home/www/$fastcgi_script_name;                            
                include        fastcgi_params;                                                             
        #   include        fastcgi.conf;                                                                   
        }                                                                                                  
                          
        # deny access to .htaccess files, if Apache's document root                                        
        # concurs with nginx's one                                                                         
        #                                                                                                  
        #location ~ /\.ht {                                                                               
        #    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 on;                                                                                           
        ssl_certificate      cert/1527654392270.pem;                                                      
        ssl_certificate_key  cert/1527654392270.key;                                                      
        root  /home/www;                                                                                  
    #    ssl_session_cache    shared:SSL:1m;                                                              
        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;                                                                           
                                                                                                            
    #    ssl_ciphers  HIGH:!aNULL:!MD5;                                                                     
    #    ssl_prefer_server_ciphers  on;                                                                     
                                                                                                            
        location / {                                                                                        
            root  /home/www;                                                                              
           index  index.html index.php index.htm;                                                         
        }                                                                                                 
   
       
        location ~ .*\.(php|php7)?$ {                                                                              
            root           /home/www;                                                                     
            fastcgi_pass   127.0.0.1:9000;                                                                
            fastcgi_index  index.php;                                                                     
            #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                          
            #                                                                                             

                fastcgi_param  HTTPS   on;                                                                
                fastcgi_param  SCRIPT_FILENAME  /home/www/$fastcgi_script_name;                           
                include        fastcgi_params;                                                                              
                                                                                                          

          }                                                                                               
    }                                                                                                     


}                                                                                                         

配置完成以后:
启动php-fpm:

/etc/init.d/php-fpm start

如果已经启动,报错的话,需要杀掉进程再执行:
unable to bind listening socket for address ‘127.0.0.1:9000’: Address already in use (98)

查看进程():

netstat -nldp |grep 9000

返回信息:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 20016/php-fpm: mast

杀掉进程():

kill 20016

再次启动(成功返回done):

/etc/init.d/php-fpm start

添加启动项:

chkconfig --add php-fpm

开启3,4,5:

chkconfig php-fpm --level 345 on

重新启动php-fpm:

service php-fpm restart

重新启动nginx (如果失败往下走):

service nginx restart

重新启动nginx

如果没有配置的上面命令执行会失败,使用下面安装路径启动:
/usr/local/nginx/sbin/nginx -s reload

到此差不多完成,最后一步:添加测试文件
用ftp在网站根路径下添加test.php
/home/www/test.php

文件信息:

<?php
phpinfo();
?>

然后再网站打开链接:
http://xxxxxxx/test.php
或者
https://xxxxxxx/test.php

《php7详细安装教程(linux + nginx +PHP + mysql)》 图片.png

    原文作者:健身吧_猿世界
    原文地址: https://www.jianshu.com/p/4b8454a08b12
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞