Using brew to install nginx and configure

Installation process

brew is used here to install the software.

  • install
brew install nginx
  • View the installation information (often used, such as viewing the installation directory, etc.)
sudo brew info nginx


After installation, it mainly depends on where brew installed nginx. The default is to install it to / usr/local/etc/nginx/nginx.conf


Common command record

  • View nginx version
nginx -v
  • Start nginx service
brew services start nginx

After visiting: http://localhost:8080 /, you can see that the startup is successful.

If it is not started successfully, check the following process, ps -ef|grep nginx:

Check whether there are three processes. If the process is not started, promise me to read the error file first! Go to the main configuration file / usr/local/etc/nginx/nginx.conf and turn on the error reporting log function. Trust me, you can read the error reporting log faster than you can check Baidu.

  • Reload nginx
# Promise me that any configuration file of nginx that has been modified a little bit will be reloaded as soon as possible!
# Promise me that any configuration file of nginx that has been modified a little bit will be reloaded as soon as possible!
# Promise me that any configuration file of nginx that has been modified a little bit will be reloaded as soon as possible! Otherwise, you can't make the modified function.

nginx -s reload
  • Shut down nginx service
brew services stop nginx

  • Stop nginx
nginx -s stop

Typical configuration mode

In the / usr/local/etc/nginx/nginx.conf main configuration file, configure a include attribute, as follows:

# Omission...
include servers/*;
# This means that the configuration files in the. servers folder of the current directory will be loaded

Under the path of / usr/local/etc/nginx/servers, a forwarding service is configured, the file is localhost_80.conf, and the content is as follows:

server {
        listen        80;
        server_name  localhost;
        root   "/Users/thoth/program/nginxweb";
        location / {
            index index.php index.html;
            error_page 400 /error/400.html;
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 500 /error/500.html;
            error_page 501 /error/501.html;
            error_page 502 /error/502.html;
            error_page 503 /error/503.html;
            error_page 504 /error/504.html;
            error_page 505 /error/505.html;
            error_page 506 /error/506.html;
            error_page 507 /error/507.html;
            error_page 509 /error/509.html;
            error_page 510 /error/510.html;
            autoindex  off;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

This can be used as your template. You can modify the server name and root.

        server_name  www.test.com;
        root   "/Users/thoth/program/testdir";

Reference resources

https://www.jianshu.com/p/6c7cb820a020
http://jalan.space/2017/01/12/2017-01-13-mac-os-nginx/

Tags: OS X Nginx brew PHP sudo

Posted on Sun, 05 Apr 2020 06:05:43 -0700 by doremi