MAC 安装Nginx 记录

23 篇文章 0 订阅
10 篇文章 0 订阅

brew install nginx

~ $ brew install nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.17.6.catalina.bottl
==> Downloading from https://akamai.bintray.com/8c/8ccb0756081f9fc73296ba3a3e68d
######################################################################## 100.0%
==> Pouring nginx-1.17.6.catalina.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.17.6: 25 files, 2MB
~ $ nginx -h
nginx version: nginx/1.17.6
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.17.6/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

 

打开:http://127.0.0.1:8080

查看服务安装情况

#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
#测试配置是否有语法错误
nginx -t如果提示pid丢失的话,就用这句话 nginx -c "/usr/local/etc/nginx/nginx.conf"


 

结合nginx服务器运行多版本php

php5.6的目录下etc/php-fpm.conf

末尾添加:

listen = 127.0.0.1:9056

 

php7.3的目录下etc/php-fpm.conf

末尾添加:

listen = 127.0.0.1:9073

 

nginx.conf文件中http{ ... include servers/*; },自带有include servers/*;

在servers目录下,创建*.conf文件,以下实例为web.conf

server {
 listen 80;
 server_name sk.com;

 # 设定网站根目录
 root /web/sk;
 # 网站默认首页
 index index.php index.html index.htm;

 # PHP 支持
 location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9056;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
 }
}

再建立一个: 

server {
 listen 80;
 server_name yt.com;

 # 设定网站根目录
 root /web/yt/public;
 # 网站默认首页
 index index.php index.html index.htm;

 # PHP 支持
 location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9073;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
 }
}

也可以在~/.bash_profile文件中添加一个命令别名,注意=号两边不能有空格:

alias php-fpm56='~/.phpbrew/php5.6.40/sbin/php-fpm'

启动php-fpm

运行命令: sudo php-fpm56,这样就在9056端口启动了5.6版本的fpm进程

例如我的是:

phpbrew use 5.6.40

phpbrew fpm start

 

重启nginx服务

nginx -s reload

 

这样就能不同网站运行不同版本php

 

其他:

 

netstat -an |grep 9056

ps -ef | grep php

ps aux |grep php-fpm

重启php-fpm:kill -USR2 42891

 

遇到问题:

2019/12/23 01:29:57 [error] 17552#0: *458 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: open_basedir restriction in effect. File(/web/sk/0.php) is not within the allowed path(s): (/www/llj/sk/:/tmp/:/proc/) in Unknown on line 0

PHP message: PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

Unable to open primary script: /web/sk/0.php (Operation not permitted)" while reading response header from upstream, client: 127.0.0.1, server: skw.com, request: "GET /0.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9056", host: "skw.com"

参考了:nginx FastCGI错误Primary script unknown解决办法

配置LNMP环境出现错误:Primary script unknown" while reading response header from upstream

https://segmentfault.com/q/1010000013451481

 

捣鼓了很久,调增了很多次配置和权限,

结果发现是根目录下的.user.ini的问题


2019/12/23 01:34:49 [error] 17552#0: *461 kevent() reported about an closed connection (54: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: skw.com, request: "GET /0.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9056", host: "skw.com"


502 Bad Gateway
nginx/1.17.6

NGINX 反向代理错误:104: Connection reset by peer

https://www.jianshu.com/p/5c57ea8efae3

https://www.ucloud.cn/yun/40542.html  

跟踪php-fpm:sudo dtruss -a -n php-fpm,这个不错

输出了问题所在,和php phpinfo.php输出的问题一样:

root $ php a.php 
PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in ~/web/sk/shuike/shuikeEcshop/a.php on line 2
dyld: lazy symbol binding failed: Symbol not found: _XpmLibraryVersion
  Referenced from: ~/.phpbrew/php/php-5.6.40/lib/php/extensions/no-debug-non-zts-20131226/gd.so
  Expected in: flat namespace

dyld: Symbol not found: _XpmLibraryVersion
  Referenced from: ~/.phpbrew/php/php-5.6.40/lib/php/extensions/no-debug-non-zts-20131226/gd.so
  Expected in: flat namespace

Abort trap: 6

看来是我编译的扩展有问题

具体参照:https://blog.csdn.net/weixin_39894818/article/details/87753201

phpbrew ext install gd -- --with-gd=shared \
    --with-png-dir=/usr/local/opt/lib \
    --with-jpeg-dir=/usr/local/opt/jpeg \
    --with-freetype-dir=/usr/local/opt/freetype  \
    --enable-gd-native-ttf
 

php编译的时候使用:

phpbrew install --mirror=https://www.php.net 5.6 +default +mysql +fpm +gd=shared,/usr +apxs2=/usr/local/bin/apxs +openssl

 

最终配置:

nginx。conf


#user  nobody;
worker_processes  1;

error_log  /usr/local/etc/nginx/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.php index.html 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   html;
        }

        # 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           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 ~ /\.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_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;
    #    }
    #}
    include servers/*;
}

tp配置

server {
 listen 80;
 server_name yt.com;

 # 设定网站根目录
 root /web/yt/public;
 # 网站默认首页
 index index.php index.html index.htm;

 location / {
	if (!-e $request_filename) {
		rewrite  ^(.*)$  /index.php?s=$1  last;
		break;
	}
 }

 # PHP 支持
 location ~ [^/]\.php(/|$) {
     fastcgi_pass 127.0.0.1:9073;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
 }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
    }

}

ecshop:

server {
 listen 80;
 server_name sk.com;
 root /web/sk;
 index index.php index.html index.htm;

 location / {
 }

 location ~ [^/]\.php(/|$) {
    fastcgi_pass 127.0.0.1:9056;
    fastcgi_index index.php;
    fastcgi_split_path_info  ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_connect_timeout 300; 
     fastcgi_send_timeout 300; 
     fastcgi_read_timeout 300;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 4 256k;
     fastcgi_busy_buffers_size 256k;
     fastcgi_temp_file_write_size 256k;
 }

}

 

 


其他参考:

https://blog.csdn.net/weixin_34376562/article/details/91394747

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值