猿创征文|linux中安装nginx(图文教程)


Linux中安装nginx有多种方式,这里采用源码包安装方式,实测可用。

一、下载源码包

nginx官网下载源码包。
提示:这里的蓝色字体是超链接,可直接点击跳转至nginx下载页面。

nginx源码包下载页

二、安装依赖库

安装依赖库(如已经安装请忽略),执行以下命令

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

如果不确定是否安装依赖,执行以下命令查看是否安装,执行后有输出结果代表已经安装过了。 命令中双引号" "内的内容可自行替换。

yum list installed | grep "pcre-devel"

输出结果如下图:

是否安装依赖

三、解压安装

1.将源码包上传到指定路径
2.解压

tar -zxvf nginx-1.20.1.tar.gz

参数说明
-x:解压
-z:用 gzip 压缩的文件操作
-v:显示所有过程
-f:使用文件名,注意,这个参数是最后一个参数,后面只能接文件名,不能再加参数。
这里只介绍当前命令用到的选项,其它可自行百度学习。

3.解压完后进入目录

cd nginx-1.20.1

4.安装
源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)。
(1)先执行配置命令

./configure --prefix=/usr/local/nginx

参数说明
其中- -prefix选项是指定安装目录,一会make install 的时候就不用再指定安装路径。
./configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系。

报错解决
这一步可能会出现各种错误,原因是缺少上面第二步说的那几个依赖库,重新执行下安装依赖库的命令就好了。

(2)执行编译命令

make

(3)执行安装命令

make install

这时候,/usr/local下就已经生成了nginx目录。

(4)查看nginx版本信息:

#进入可执行目录sbin
cd /usr/local/nginx/sbin/
#查看nginx版本信息
./nginx -v
#查看nginx版本信息、编译版本、配置参数
./nginx -v

如下图输出,代表成功安装了nginx查看nginx版本信息

四、启动nginx

1.检查配置文件中语法是否正确

/usr/local/nginx/sbin/nginx -t

如下图,是配置文件书写正确的测试结果。如若报错,请检查配置文件(在安装目录/config/nginx.conf)

nginx检查配置文件是否正确

这一步是为了测试配置文件,检查配置文件语法是否正确,然后试图打开文件涉及的配置。这一步成功了,nginx才能启动起来。

2.启动
进入可执行目录sbin

cd /usr/local/nginx/sbin/

启动nginx

#等同于/usr/local/nginx/sbin/nginx,也可使用这个命令/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx 

停止、重启命令(依旧是在可执行目录下执行)

# 停止命令,优雅的停止(不接受新的连接请求,等待旧的连接请求处理完毕再关闭) 配合./nginx命令也可实现重启
./nginx -s stop
# 也是停止命令,快速关闭  配合./nginx命令也可实现重启
./nginx -s quit
# 重启命令,重新加载配置文件
./nginx -s reload

./nginx -s reload 是重新加载配置文件,为了让主进程重新读取配置文件,向主进程发送一个HUP信号,主进程一旦接收到重新加载配置的的信号,它就检查配置文件语法的有效性,然后试图应用新的配置,即打开新的日志文件和新的socket 监听,如果失败,它将回滚配置更改并继续使用旧的配置,如果成功了,它开启新的工作进程,并给旧的工作进程发消息让它们优雅的关闭,旧的工作进程接收到关闭信号后,不再接收新的请求,等当前请求处理完毕后关闭。

3.访问nginx页面

http://IP地址:端口号 ngixn默认端口号是80,可在配置文件中(在安装目录/config/nginx.conf)查看修改。
如下图,就是成功的页面(这个页面的文件在/usr/local/nginx/html/index.html)。

访问nginx静态首页
没成功原因
(1)如果访问不到,请检查nginx端口是否存在,没用任何输出说明不存在。

netstat -nltp | grep 80

(2)防火墙需要关闭,如果不想关闭请直接看第(3)步。

#查看防火墙状态
systemctl status firewalld 
#关闭防火墙
systemctl stop firewalld
#开启防火墙
systemctl start firewalld

(3)如果不想关闭防火墙,则需要添加防火墙对外开放的nginx端口 。

# 添加防火墙对外开放的nginx 80端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
# 重新加载防火墙配置
firewall-cmd --reload
# 查看防火墙开放的所有端口,看看是否有80端口
firewall-cmd --zone=public --list-ports

五、nginx其他配置

1.nginx设置开机自启,要操作的文件是/etc/rc.d/rc.local

#给予执行权限
chmod +x /etc/rc.d/rc.local
#打开文件/etc/rc.local超链接指向了/etc/rc.d/rc.local
vi /etc/rc.d/rc.local
#输入i,光标移动到最后一行上,添加下面的命令(nginx启动命令)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2.nginx配置文件
nginx初始配置文件(/usr/local/nginx/conf/nginx.conf,没做任何改动)内容如下


#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;
        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;
    #    }
    #}

}

配置文件参数说明(有很多,这里只说我自己的理解,如有错误,请指出,谢谢


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#pid        logs/nginx.pid;

events {
    
    
    worker_connections  1024;
}

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


    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
	# ★扫描自定义配置文件(这样就可以在/etc/nginx/下存放多个conf文件,用来区分是哪个项目的,配置文件以server { listen 8081; server_name localhost2; #内容 } 开始,其余参数和这边一样)
	include /etc/nginx/*.conf;

    server {
    
     #一个server相当于一个虚拟主机,可以有多个
        listen       80; #监听的端口号
        server_name  localhost; #服务名

        #charset koi8-r;
        #access_log  logs/host.access.log  main;



        #开启和关闭gzip模式
        gzip on;
        
        #gizp压缩起点,文件大于1k才进行压缩
        gzip_min_length 1k;

        # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
        gzip_comp_level 7;

        # 进行压缩的文件类型。
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/xml text/javascript application/json image/png image/gif image/jpeg;
		
		sendfile            on;
		tcp_nopush          on;
		tcp_nodelay         on;
		keepalive_timeout   65;
		types_hash_max_size 4096;
		client_max_body_size 50m;

		location /onexxxx {
    
    
        	#访问路径为 url/onexxxx 时 会把 /onexxxx 拼接到 proxy_pass
	 		proxy_pass http://127.0.0.1:8080;       
		}	
		location ~* ^/(authenticat|attached|system|qqApi|server|publicapi|appApi|actuator)/ {
    
     #访问路径为这些参数时,会请求8080端口
        	proxy_pass http://127.0.0.1:8080;
        	#### kill cache
        	add_header Last-Modified $date_gmt;
        	add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        	if_modified_since off;
        	expires off;
        	etag off;
    	}

        location / {
    
     #监听的端口号默认的访问位置
            #root   html;
            root   /mnt/zzz/xxx/manager; #自己指定默认访问项目的前端页面
            index  index.html index.htm;
        }
        location ~* ^/(web|manager|screen|img|download|mobile)/ {
    
     #监听的端口号加路径,匹配路径成功,再执行root指向的路径
            #add_header Access-Control-Allow-Origin *;
            try_files $uri $uri/ /index.html;
            root   /mnt/zzz/xxx; #对应的路径,/mnt/zzz/xxx目录下有web|manager|screen|img|download|mobile,接着找index.htm
            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;
        }

    }


}

nginx还有很多知识点,不断学习中。


提示:原创文章,转载请附上原文出处链接。

猜你喜欢

转载自blog.csdn.net/qq_45337268/article/details/126676667