nginx安装

发布时间 2023-12-28 14:13:24作者: 莫逆追风

Nginx

安装nginx服务

1、下载gz包

[root@iZbp12a5l01zppu6gcybreZ nginx]# wget https://nginx.org/download/nginx-1.24.0.tar.gz

2、解压

[root@iZbp12a5l01zppu6gcybreZ nginx]# tar -zxvf nginx-1.24.0.tar.gz

3、文件内容

[root@iZbp12a5l01zppu6gcybreZ nginx]# cd nginx-1.24.0
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# ll
total 836
drwxr-xr-x 6 1001 1001   4096 Dec 11 08:14 auto
-rw-r--r-- 1 1001 1001 323312 Apr 11  2023 CHANGES
-rw-r--r-- 1 1001 1001 494234 Apr 11  2023 CHANGES.ru
drwxr-xr-x 2 1001 1001   4096 Dec 11 08:14 conf
-rwxr-xr-x 1 1001 1001   2611 Apr 11  2023 configure
drwxr-xr-x 4 1001 1001   4096 Dec 11 08:14 contrib
drwxr-xr-x 2 1001 1001   4096 Dec 11 08:14 html
-rw-r--r-- 1 1001 1001   1397 Apr 11  2023 LICENSE
drwxr-xr-x 2 1001 1001   4096 Dec 11 08:14 man
-rw-r--r-- 1 1001 1001     49 Apr 11  2023 README
drwxr-xr-x 9 1001 1001   4096 Dec 11 08:14 src

4、环境依赖安装

# gcc环境
[root@iZbp12a5l01zppu6gcybreZ nginx]# yum install -y gcc
# pcre
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# yum install -y pcre pcre-devel
# zlib
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# yum install -y zlib zlib-devel

5、nginx构建安装

[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# ./configure --prefix=/usr/local/nginx
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# make && make install

nginx安装完成,在 /usr/local/nginx 目录下即可看到安装的nginx目录

6、启动nginx

[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# cd /usr/local/nginx/sbin
# 启动 
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# ./nginx
# 停止
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# ./nginx -s stop
# 重启
[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# ./nginx -s reload

nginx文件内容

[root@iZbp12a5l01zppu6gcybreZ nginx]# ll
drwxr-xr-x 2 root root 4096 Dec 11 09:17 conf
drwxr-xr-x 2 root root 4096 Dec 11 09:17 html
drwxr-xr-x 2 root root 4096 Dec 11 09:17 logs
drwxr-xr-x 2 root root 4096 Dec 11 09:17 sbin

配置文件解析

[root@iZbp12a5l01zppu6gcybreZ conf]# cat nginx.conf

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

安装nginx系统服务

1、写配置文件

[root@iZbp12a5l01zppu6gcybreZ nginx-1.24.0]# vim /usr/lib/systemd/system/nginx.service

文件内容

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.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 -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
wantedBy=multi-user.target

2、重新加载系统服务

[root@iZbp12a5l01zppu6gcybreZ conf]# systemctl daemon-reload

3、启动服务

[root@iZbp12a5l01zppu6gcybreZ conf]# systemctl start nginx

4、查看nginx服务是否启动成功

[root@iZbp12a5l01zppu6gcybreZ conf]# systemctl status nginx
● nginx.service - nginx - web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset: disabled)
   Active: active (running) since Mon 2023-12-11 10:40:30 CST; 1min 48s ago
  Process: 7257 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 7255 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 7259 (nginx)
    Tasks: 2
   Memory: 952.0K
   CGroup: /system.slice/nginx.service
           ├─7259 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─7260 nginx: worker process

Dec 11 10:40:30 iZbp12a5l01zppu6gcybreZ systemd[1]: Starting nginx - web server...
Dec 11 10:40:30 iZbp12a5l01zppu6gcybreZ nginx[7255]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf sy...is ok
Dec 11 10:40:30 iZbp12a5l01zppu6gcybreZ nginx[7255]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test i...ssful
Dec 11 10:40:30 iZbp12a5l01zppu6gcybreZ systemd[1]: Started nginx - web server.
Hint: Some lines were ellipsized, use -l to show in full.

[root@iZbp12a5l01zppu6gcybreZ conf]# ps -ef | grep nginx
root      7259     1  0 10:40 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    7260  7259  0 10:40 ?        00:00:00 nginx: worker process
root      7365 20894  0 10:42 pts/0    00:00:00 grep --color=auto nginx

5、开机服务自启动

[root@iZbp12a5l01zppu6gcybreZ conf]# systemctl enable nginx.service