美文网首页
Nginx php-fpm 配置thinkphp5 虚拟机

Nginx php-fpm 配置thinkphp5 虚拟机

作者: 黑哥_2c57 | 来源:发表于2019-05-18 20:46 被阅读0次

    公司一直再用apache做web服务器,由于nginx的热度,我一直想切换到nginx, 最近学习了一下nginx的配置, 就在自己的电脑上测试把公司的项目切换到nginx上, 我使用的是deepin linux, php7.1, 这里记录下过程,加深印象,与大家一同学习。

    1.配置/etc/hosts
    新增一行

     127.0.0.1  mysite.cn
    
    1. 配置php-fpm
      如果没装,执行 sudo apt install php7.1-fpm 即可安装
      安装后 找到www.conf, 我的在 /etc/php/7.1/fpm/pool.d/www.conf
      vi /etc/php/7.1/fpm/pool.d/www.conf

    第23,24行改为

    user = nginx
    group = nginx
    

    第36行改为

    listen = /var/run/php/php7.1-fpm.sock
    

    这3行 前面有分号的,都要将分号去掉
    保存后可检查配置文件语法是否正确
    php-fpm7.1 -t

    1. 创建 /var/run/php/php7.1-fpm.sock
      touch /var/run/php/php7.1-fpm.sock
      chown nginx:nginx /var/run/php/php7.1-fpm.sock
      chmod 0660 /var/run/php/php7.1-fpm.sock

    2. 创建 nginx 虚拟机
      vi /usr/local/nginx/conf/nginx.conf
      在 http{} 花括号内 新建一行 include vhost/*; //表示引入vhost下所有的配置文件

    user  nginx;
    worker_processes  4;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  65535;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        server {
            listen       8000;
            server_name  localhost;
    
            location / {
                expires 30m;
                root   html;
                index  index.html index.htm;
                }
    
                error_page   500 502 503 504 404  /50x.html;
                location = /50x.html {
                root   html;
        }
      include vhosts/*;
    }
    
    

    我放在最后 倒数第二行的

    在/usr/local/nignx/conf下 新建vhosts文件夹和 mysite.cn.conf虚拟机配置文件
    sudo mkdir vhosts
    sudo touch vhosts/mysite.cn.conf
    sudo vi vhosts/mysite.cn.conf

    server {
            listen 8080;
            server_name mysite.cn;
            root /home/lj/work/mysite/public;
            index index.php index.html;
    
            location / {  //配置rewrite
                if (!-e $request_filename) {
                    rewrite ^(.*)$ /index.php?s=/$1 last;
                }
            }
            location ~ \.php$ {  //配置php
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                include fastcgi.conf;
                include fastcgi_params;
            }
        }
    

    检查 nginx配置文件语法
    /usr/local/nginx/sbin/nginx -tc /usr/local/nginx/conf/nginx.conf

    重启php-fpm php7.1-fpm restart
    重启nginx /usr/local/nginx/sbin/nginx -s reload

    浏览器通过访问 http://mysite.cn:8080
    大功告成!

    相关文章

      网友评论

          本文标题:Nginx php-fpm 配置thinkphp5 虚拟机

          本文链接:https://www.haomeiwen.com/subject/jnatzqtx.html