【LalaLuna笔记】Nginx基础配置(1)

nginx启动命令是什么

第一种方法:进入sbin目录下执行以下命令:

启动nginx的命令为
/usr/local/nginx/sbin/nginx

停止nginx的命令为
/usr/local/nginx/sbin/nginx -s stop

重启nginx的命令为
/usr/local/nginx/sbin/nginx -s reload

第二种方法:配置systemctl之后的启动方式

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx

准备工作,从备份生成nginx.cof

1.之前的操作修改了 nginx.conf,重新生成一个 nginx.conf

mv nginx.conf nginx_1.conf
# 此时logs文件夹里就没有 nginx.conf了

cp nginx.conf.default nginx.conf
# 使用备份文件 nginx.conf.default

vim nginx.conf

2.目录结构

/home/www
├── conf.d  
├── myweb 
    ├── 404.html
    ├── server1
    │   ├── location1 
    │   │   └── index_sr1_location1.html
    │   ├── location2
    │   │   └── index_sr1_location2.html
    │   └── logs
    └── server2
        ├── location1
        │   └── index_sr2_location1.html
        ├── location2
        │   └── index_sr2_location2.html
        └── logs

3.修改 nginx.conf

user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认就是on,所以注释掉了

events {
    accept_mutex on;
    multi_accept on;
    worker_connections 1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout    65;
    log_format server1 '==========> server1 access log format';
    log_format server2 '==========> server2 access log format';
    include /home/www/conf.d/*.conf;
    
    server {
        listen 80;
        server_name localhost;
        access_log logs/server.log;
        location / {
            root /home/www/html;
            index index.html index.htm;
        }
        location /get_text {
            access_log logs/text.log;
            default_type text/html;
            return 200 "<h1>This is text</h1>";
        }
        location /get_json {
            default_type application/json;
            return 200 '{"name": "Tom", "age": 18}';
        }
    }
}

4.分别配置server1和server2的conf

vim /home/www/conf.d/server1.conf
server {
    listen 8081;
    server_name localhost;
    access_log  /home/www/myweb/server1/logs/access.log server1;
    location /server1/location1 {
        root /home/www/myweb;
        index index_sr1_location1.html;
    }
    location /server1/location2 {
        root /home/www/myweb;
        index index_sr1_location2.html;
    }
    
    error_page 404 /404.html
    location = /404.html {
        root /home/www/myweb;
        index 404.html;
    }
}
vim /home/www/conf.d/server2.conf
server {
    listen 8082;
    server_name localhost;
    access_log  /home/www/myweb/server2/logs/access.log server2;
    location /server2/location1 {
        root /home/www/myweb;
        index index_sr2_location1.html;
    }
    location /server2/location1 {
        root /home/www/myweb;
        index index_sr2_location2.html;
    }
    
    error_page 404 /404.html
    location = /404.html {
        root /home/www/myweb;
        index 404.html;
    }
}

Nginx配置成系统服务

(1)在 /usr/lib/systemd/system 目录下添加nginx.service

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
wantedBy=default.target

(2)添加完成后如果有权限问题

chmod 755 /usr/lib/systemd/system/nginx.service

(3)使用

启动:systemctl start nginx
停止:systemctl stop nginx
重启:systemctl restart nginx
重新加载配置文件:systemctl reload nginx
查看nginx状态:systemctl status nginx
开机启动:systemctl enable nginx # Linux服务器重启后,自动运行nginx

Nginx命令配置到系统命令

(1)修改/etc/profile文件

vim /etc/profile
# 最后一行加上
export PATH=$PATH:/usr/local/nginx/sbin

(2)使之立即生效

source /etc/profile

(3)执行nginx命令

nginx -V
posted @ 2022-08-25 18:07  LalaLuna  阅读(44)  评论(0编辑  收藏  举报