下载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进程
简单配置反向代理和负载均衡
http {
//http配置
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream mytest{ //名称任意
//负载均衡配置
server 127.0.0.1:8080 weight=1; //权重
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://mytest; //反向代理
}
location /admin {
//不同的服务器
}
}