(2) Installation of Nginx on Mac

Foreword: The power of nginx does not need to be said, so naturally I should have a fun. However, many installations and configurations on the Internet are too awkward. I really want to say something with a quotational nature, but I really have no intention of quoting: The writing of many programmers is really bad. It's really hard to understand, maybe all Chinese are taught by physical education teachers.

Current system environment:

Installation tools:

homebrew (If you haven’t used it, you can click the link to learn more or Baidu by yourself)

step:

  1. Open the terminal, habitual command:

brew update
//结果:Already up-to-date.

  2. The terminal continues to execute the command:

brew search nginx   //查询要安装的软件是否存在

  3. Here we perform one more step of "waste" command, but it will help our subsequent configuration:

brew info nginx

  operation result:

  

  We can see that nginx has not been installed locally (Not installed), the source of nginx (From), Docroot defaults to /usr/local/var/www, in the /usr/local/etc/nginx/nginx.conf configuration file The default port is configured to 8080 so that nginx does not need to add sudo when running, nginx will load all files in the /usr/local/etc/nginx/servers/ directory, and we can start it with the simplest command'nginx' nginx.

  4. The installation is officially started:

brew install nginx

  5. Check the nginx installation directory (whether it is as stated in info): 

open /usr/local/etc/nginx/

    

  If you successfully open the nginx directory, you can also see the servers directory and the configuration file of nginx.conf as mentioned in info (this configuration file will be used later). But we did not find where nginx was installed.

  The terminal continues to execute:

open /usr/local/Cellar/nginx  //其实这个才是nginx被安装到的目录

  

  You will see a folder named after the version number of the currently installed nginx. This is the root directory of the nginx we installed. Enter 1.12.2_1/bin directory, you will see the executable startup file of nginx.

  Similarly, we can also see a shortcut folder named html under the 1.12.2_1/ directory (let's call it that for the time being). Entering this directory, we will find that it points to /usr/local/var/ www directory, this is mentioned in the info information we viewed above (Dcroot)

   6. Start nginx and enter the following command at the terminal:

nginx

  If no error is reported, the startup is successful.
  7. Access verification:

    Open the browser and visit localhost:8080. This is a little different from some online tutorials. Under normal circumstances, you will see the welcome interface of nginx at this step. However, the blogger has encountered a situation of cheating (if you can normal See the nginx welcome interface, you can skip this step directly)

 

   Presumably everyone is familiar with this error, so I won’t explain it any more. Next, I will talk about the reason directly (the reason is only a guess, because there is no way to verify it): the installed nginx will default to html (that is, /usr/local/var) A welcome page file is generated in the /www) directory, and the above situation is that the welcome page file is not generated (as for why it is not generated, it is not known, everything is a guess).

  Before solving this problem, let's first understand the nginx configuration file (nginx.conf):

cat /usr/local/etc/nginx/nginx.conf

  The code to display the configuration file is:

#user  nobody;
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 {
     #侦听8080端口
        listen       8080;
     #定义使用 localhost访问
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
       #定义服务器的默认网站根目录位置
            root   html;
        #定义首页索引文件的名称
            index  index.html index.htm;
        }
     ...
     ...
     ... (注释代码太多,就不全部贴出来了)

    include servers/*;
}

  Through the configuration file, we can see that the default website root directory is html (ie /usr/local/var/www), and the default index files are index.html and index.htm. Now we can find the reason. The root directory is missing the home page index file, so let's create one manually:

cd /usr/local/var/www/                      //进入到www目录下
touch index.html                            //创建一个新的index.html文件
vim index.html                              //编辑该文件 

  Write the following code into the index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>title</title>
</head>
<body>
    <div>
        <h1>我的nginx欢迎页面</h1>
    </div>
</body>
</html>

  Press the esc key, enter: wq to launch, edit and save (I believe everyone will know this, but it is still written in obsessive-compulsive disorder).

  Go back to the browser (localhost:8080) to refresh:

 

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/108754649