linux安裝nginx及常用命令
阿新 • • 發佈:2021-08-07
下載nginx的linux壓縮包
上傳到linux伺服器
判斷linux上是否有nginx
[root@VM-4-6-centos /]# whereis nginx
[root@VM-4-6-centos /]#
[root@VM-4-6-centos /]# nginx
-bash: nginx: command not found
解壓 nginx-1.18.0.tar.gz 壓縮包
[root@VM-4-6-centos home]# tar -zxvf nginx-1.18.0.tar.gz
安裝
# 第一步:進入解壓的nginx目錄 [root@VM-4-6-centos home]# cd nginx-1.18.0 [root@VM-4-6-centos nginx-1.18.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src # 第二步:自動配置 [root@VM-4-6-centos nginx-1.18.0]# ./configure checking for OS + Linux 3.10.0-1160.11.1.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found # 第三步:執行make命令 [root@VM-4-6-centos nginx-1.18.0]# make make -f objs/Makefile make[1]: Entering directory `/home/nginx-1.18.0' cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_log.o \ src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_palloc.o \ src/core/ngx_palloc.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_array.o \ src/core/ngx_array.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ # 第四步:執行 make install 命令 [root@VM-4-6-centos nginx-1.18.0]# make install make -f objs/Makefile install make[1]: Entering directory `/home/nginx-1.18.0' test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx' test -d '/usr/local/nginx/sbin' \ || mkdir -p '/usr/local/nginx/sbin' test ! -f '/usr/local/nginx/sbin/nginx' \ || mv '/usr/local/nginx/sbin/nginx' \ '/usr/local/nginx/sbin/nginx.old' cp objs/nginx '/usr/local/nginx/sbin/nginx' test -d '/usr/local/nginx/conf' \ || mkdir -p '/usr/local/nginx/conf' # 第五步:驗證是否安裝 [root@VM-4-6-centos nginx-1.18.0]# whereis nginx nginx: /usr/local/nginx [root@VM-4-6-centos nginx-1.18.0]# cd /usr/local/nginx # 進入安裝 nginx 目錄 [root@VM-4-6-centos nginx]# ll total 16 drwxr-xr-x 2 root root 4096 Aug 7 20:08 conf drwxr-xr-x 2 root root 4096 Aug 7 20:08 html drwxr-xr-x 2 root root 4096 Aug 7 20:08 logs drwxr-xr-x 2 root root 4096 Aug 7 20:08 sbin # 一般存放的執行檔案 [root@VM-4-6-centos nginx]# cd sbin [root@VM-4-6-centos sbin]# ls nginx [root@VM-4-6-centos sbin]# ./nginx # 執行 nginx [root@VM-4-6-centos sbin]# cd .. [root@VM-4-6-centos nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@VM-4-6-centos nginx]# cd conf # 進入 nginx 配置檔案目錄 [root@VM-4-6-centos conf]# ll total 68 -rw-r--r-- 1 root root 1077 Aug 7 20:08 fastcgi.conf -rw-r--r-- 1 root root 1077 Aug 7 20:08 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Aug 7 20:08 fastcgi_params -rw-r--r-- 1 root root 1007 Aug 7 20:08 fastcgi_params.default -rw-r--r-- 1 root root 2837 Aug 7 20:08 koi-utf -rw-r--r-- 1 root root 2223 Aug 7 20:08 koi-win -rw-r--r-- 1 root root 5231 Aug 7 20:08 mime.types -rw-r--r-- 1 root root 5231 Aug 7 20:08 mime.types.default -rw-r--r-- 1 root root 2656 Aug 7 20:08 nginx.conf # nginx 配置檔案 -rw-r--r-- 1 root root 2656 Aug 7 20:08 nginx.conf.default -rw-r--r-- 1 root root 636 Aug 7 20:08 scgi_params -rw-r--r-- 1 root root 636 Aug 7 20:08 scgi_params.default -rw-r--r-- 1 root root 664 Aug 7 20:08 uwsgi_params -rw-r--r-- 1 root root 664 Aug 7 20:08 uwsgi_params.default -rw-r--r-- 1 root root 3610 Aug 7 20:08 win-utf [root@VM-4-6-centos conf]# cat nginx.conf # 檢視 nginx 配置檔案 #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 { listen 80; # 開放的埠號 80 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
現在就可以訪問80埠號對nginx進行訪問了
nginx常用命令
cd /usr/local/nginx/sbin/
./nginx # 啟動
./nginx -s stop # 停止
./nginx -s quit # 安全退出
./nginx -s reload # 重新載入配置檔案
ps aux|grep nginx # 檢視nginx程序