技术笔记 - My cloud dairy2: Nginx server basic instructions |我的云日 2: Nginx 的服务器基本操作

in #cn7 years ago (edited)

English version (Chinese version is in following part 中文版在第二部分)

In my last blog My cloud dairy1: Deploy a Nginx server on ECS Ubuntu, I talked about how to deploy Nginx server on ECS. In this blog, I will introduce some basic instructions of Nginx server (ECS OS: Ubuntu 16.04.2):
1). Server operations(Start, shutdown and reload)
2). Binding static home page

1). Server operations(Start, shutdown and reload)

1.a). Start Nginx server

In the test part of my last blog, I used an example of starting nginx server:

nginx -c /etc/nginx/nginx.conf

Syntax of starting Nginx server:

nginx -c {path}/nginx configuration file

If the configuration file is the default configuration file of Nginx server, you can directly use:

nginx 

1.b). Shutdown Nginx server

1.b.1). Method 1:Use Nginx command:

nginx -s stop

1.b.2). Method 2:Use Linux command:

Nginx server works as a process in Linux. So we can shutdown Nginx server by terminating Nginx server process.

First step, find Nginx process ID.

ps -ef | grep nginx

ps command:show running process
-e parameter:show all running process
-f parameter:show process information
grep command is used for filtering the content with certain string(in our case "nginx") from the result of pscommand

In the result, there are 4 fields which are useful for us:
The first field indicates process name, e.g. root in following example;
The second field indicates process ID, e.g. 11961 in following example;
The third field indicates process id of parent process, e.g. 1 in following example;
The last field indicates process information, e.g. nginx: master process nginx -c nginx.conf in following example.
Example:

root 11961 1 0 09:32 ? 00:00:00 nginx: master process nginx -c nginx.conf

Second step, terminate Nginx process.

We will use Linux command kill. For command kill, there are three possible parameters to terminate process:

QUIT (parameter id 3)

kill -QUIT master process ID or kill -3 master process ID

Description: terminate process with process ID(in this case, master process ID).

KILL (parameter id 9)

kill -KILL process ID or kill -9 process ID

Description: Force process with process ID(in this case, master process ID and all working process IDs) to terminate.

TERM (parameter id 15)

kill -TERM master process ID or kill -15 master process ID

Description: terminate process with process ID(in this case, master process ID).

1.c). Reload Nginx Server

When we make modification on Nginx configuration file, to make it take effect on server, we need to reload Nginx configuration file.

1.c.1). Method1:Use Nginx command

nginx -s reload


We can find that, during reload, the master process ID doesn't change but the working process ID changes.

1.c.2). Method 2:Use Linux command kill:

kill -HUP master process ID

2). Binding static home page

2.a). Create another directory for server

In most case, I create a new directory to store server files instead of using the default Nginx configuration directory.

cd ~
mkdir nginx
cd nginx
mkdir test
cd test

2.b). Create home page

mkdir html
cd html
vi index.html

Type following html codes into index.html by vi to create a home page:

<html>
<head>
     <title>Test</title>
</head>

<body>
     <p>This is a test html file.</p>
</body>

</html>

2.c). Create configuration file

First,go back to parent directory

cd ..

Then create configuration file nginx.conf by vi

vi nginx.conf

Type in following codes, save and quit vi.

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

    server{
       listen 80;

       server_name localhost;

       location / {
          root {your_current_path}/html;
          index  index.html;
       }
    }
}

Few words on server related codes:

    server{
       listen 80;
       server_name localhost;
       location / {
          root {your_current_path}/html;
          index  index.html;
       }
    }

listen 80; indicates that server will listen to port 80
server_name localhost; indicates that server name is localhost, you can also change localhost to server IP address or your own domain.

location / {
      root {your_current_path}/html;
      index  index.html;
}

indicates where your home page is.
root indicates the directory path of your home page, in the example above, your need to change {your_current_path}to test directory we created in 2.a);
index indicates home page file name.

To guarantee the correctness of server configuration file nginx.conf we create, we can use the following command to verify it:

nginx -t -c {path}/nginx.conf

{path} is the absolute path of the directory which stores nginx.conf. If you get a similar result as mine, it means your configuration file passes the verification. You can load it on your server without any problem.

2.d). Test

Step 1. Start server.

nginx -c {path}/nginx.conf

{path} is the absolute path of the directory which stores nginx.conf.

Step 2. Open your web browser, type in the address of your server, e.g. 127.0.0.1.


If the home page we created in 2.b) could be displayed, it means the static home page is bound successfully.

If you have any questions or doubts, free feel to leave me a message.

2017 Oct 26


中文版本

在上一篇文章 技术笔记 - 我的云日记1: 在 ECS Ubuntu 上配置 Nginx 服务器 中我介绍了如何在ECS上部署 Nginx 服务器。我会在在接下来的两篇文章中介绍 Nginx 服务器的一些基础操作(操作系统 Ubuntu 16.04.2):
1). 服务器操作 (启动,关闭与重载)
2). 绑定服务器静态主页

1). 服务器操作 (启动,关闭与重载)

1.a). 启动 Nginx 服务器

上一篇文章中的测试部分使用了启动 Nginx 服务器的指令:

nginx -c /etc/nginx/nginx.conf

这里启动 Nginx 服务器的语法是

nginx -c {path}/nginx配置文件

如果nginx的配置文件地址是系统默认地址,可以直接使用

nginx 

1.b). 关闭 Nginx 服务器

1.b.1). 方法1:使用Nginx命令

nginx -s stop

1.b.2). 方法2:使用Linux命令

Nginx 的服务器其实是运行在服务器上的一个进程。所以只要终止该进程就可以关闭Nginx服务器了。

第一步 查找Nginx服务器的进程号

ps -ef | grep nginx

ps 指令:显示系统中正在执行的进程信息
-e 参数:显示所有相关进程
-f 参数:显示进程相关信息
grep 指令用于在ps指令的执行结果中过滤出含有指定字符串(这里为nginx)的内容

程序运行结果中,有四个字段是我们在以后操作中需要使用的字段:
第一字段表示进程名,如下例中 root
第二字段表示进程ID,如下例中 11961
第三字段表示该进程的父进程ID,如下例中 1
最后一个字段表示该进程描述, 如下例中 nginx: master process nginx -c nginx.conf

root 11961 1 0 09:32 ? 00:00:00 nginx: master process nginx -c nginx.conf

我们关闭服务器时所需要终止的是Nginx的主进程,也就是进程描述中含有master process字样的进程。

第二步 终止Nginx进程

这里要使用到Linux的 kill 指令,在不同情况下我们可以选择不同的kill指令参数执行终止进程命令。主要使用的参数有三种:

QUIT (变量代码 3)

kill -QUIT 主进程ID 或 kill -3 主进程ID

指令描述 彻底终止指定进程号的进程(此处为主进程ID)。它期望接收进程清除自给的状态并退出,同时生成内存转储

KILL (变量代码 9)

kill -KILL 进程ID 或 kill -9 进程ID

指令描述 强制退出

注意:因为是强制退出所以要指定终止主进程(master process)ID和所有工作进程(worker process)ID

TERM (变量代码 15)

kill -TERM 主进程ID 或 kill -15 主进程ID

指令描述:彻底终止指定进程ID的进程(此处为主进程ID)。它期望接收进程清除自给的状态并退出

1.c). 重载 Nginx 服务器

当我们对 Nginx 的配置文件进行了修改,为了使其生效,我们需要重载 Nginx 的配置文件才能使修改生效。

1.c.1). 方法1:使用Nginx命令

nginx -s reload


我们可以看到,在每次重载(reload)指令执行过后,Nginx 的主进程ID不变,工作进程的ID则发生变化。

1.c.2). 方法2:使用Linux的 kill 指令:

kill -HUP 主进程ID

2). 绑定服务器静态主页

2.a). 建立服务器文件夹

由于个人习惯,一般情况情况下,我并不使用Nginx默认的存储路径,而是新建一个文件夹用于存储服务器的相关文件。

cd ~
mkdir nginx
cd nginx
mkdir test
cd test

2.b). 新建主页

mkdir html
cd html
vi index.html

用vi在index.html输入以下html代码,来建立我们的测试主页:

<html>
<head>
     <title>Test</title>
</head>

<body>
     <p>This is a test html file.</p>
</body>

</html>

这样我们的测试主页就创建完毕了。

2.c). 创建服务器配置文件

首先,我们先返回上一级文件夹

cd ..

然后,我们打开vi编辑器新建nginx.conf

vi nginx.conf

输入以下内容,保存并退出。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

    server{
       listen 80;

       server_name localhost;

       location / {
          root {your_current_path}/html;
          index  index.html;
       }
    }
}

由于网络上有很多关于Nginx配置文件的说明,这里我就不多做赘述了。这里特别说明一下跟服务器有关的这一段代码:

    server{
       listen 80;
       server_name localhost;
       location / {
          root {your_current_path}/html;
          index  index.html;
       }
    }

listen 80; 表示该服务器监听80端口;
server_name localhost; 表示服务器的名字(浏览器中的访问地址)为localhost。服务器的 IP 地址或者如果你有自己的域名都可以在此处填写。

location / {
      root {your_current_path}/html;
      index  index.html;
}

指示你的主页地址,root后指示文件夹路径,上例中{your_current_path}需要修改为之前我们在2.a)中创建的test文件夹的绝对地址;index后指示静态主页的文件名称。

至此为止,服务器的静态主页就配置完毕了。

这里额外提一句:为了确保我们所生成的配置文件nginx.conf正确,可以使用以下指令进行验证:

nginx -t -c {path}/nginx.conf

此处{path}为系统中存放nginx.conf文件夹的绝对路径。如果你的运行结果与我相似,说明验证通过,可以部署到服务上了。

2.d). 测试

启动服务器

nginx -c {path}/nginx.conf

此处{path}为系统中存放nginx.conf文件夹的绝对路径。
现在可以打开浏览,输入服务其地址,如 127.0.0.1。

如果能正常显示出我们之前建立的index.html内容则说明静态主页已经绑定成功。

如果你对以上的内容有任何问题或疑问,欢迎在下面给我留言。

2017年10月26日


以上内容由steemit作者 @breathewind 原创发布。
All the contents above are published by Steemit author @breathewind.

转载请注明出处。
Please indicate the source when you reblog or reuse.

如果您对我的内容感兴趣,不妨试试这些文章:
If you like my post, the following posts may be interested for you as well:

技术笔记 - My cloud dairy1: Deploy a Nginx server on ECS Ubuntu |我的云日记1: 在 ECS Ubuntu 上配置 Nginx 服务器
游记 - 意大利阿尔巴松露节之旅 | A trip on Alba White Truffle Fair
瞎想 - 如果世界不是客观的而是主观的呢?

感谢您的阅读 !
Thanks for your reading !

欢迎您我对我点赞,评论或者订阅 @breathewind
Please feel free to upvote, comment and follow me @breathewind


Sort:  

Calling @originalworks :)
img credz: pixabay.com
Nice, you got a 4.0% @minnowbooster upgoat, thanks to @breathewind
Want a boost? Minnowbooster's got your back!

The @OriginalWorks bot has determined this post by @breathewind to be original material and upvoted(2%) it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

好专业,技术小白看不懂我想对那些技术男肯定有帮助。

我也就是随便写写,想给自己留个笔记: )

Coin Marketplace

STEEM 0.19
TRX 0.12
JST 0.028
BTC 65342.69
ETH 3543.20
USDT 1.00
SBD 2.39