Yuan Chuang Solicitation | Install nginx in Linux (graphic tutorial)


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

1. Download the source package

Download the source package from the nginx official website .
提示:这里的蓝色字体是超链接,可直接点击跳转至nginx下载页面。

nginx source package download page

2. Install dependent libraries

Install dependent libraries (ignore if already installed), execute the following command

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

If you are not sure whether to install the dependency, execute the following command to check whether it is installed. After execution, the output results indicate that it has been installed. The content inside the double quotes " " in the command can be replaced by itself.

yum list installed | grep "pcre-devel"

The output result is as follows:

Whether to install dependencies

3. Unzip and install

1. Upload the source package to the specified path
2. Decompress

tar -zxvf nginx-1.20.1.tar.gz

Parameter description
-x: decompress
-z: operate on files compressed by gzip
-v: display all processes
-f: use the file name, note that this parameter is the last parameter, and only the file name can be followed by the file name, and no additional parameters can be added.
Here only the options used in the current command are introduced, others can be learned by Baidu.

3. Enter the directory after decompression

cd nginx-1.20.1

4. Installation
源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)。
(1) Execute the configuration command first

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

Parameter description
Among them, the - -prefix option is to specify the installation directory, and you don't need to specify the installation path when you make install later.
./configure is the first step of source code installation. Its main function is to configure the software to be installed and check whether the current environment meets the dependencies of the software to be installed.

Error resolution
There may be various errors in this step. The reason is that the dependent libraries mentioned in the second step above are missing. Re-execute the command to install the dependent libraries.

(2) Execute the compile command

make

(3) Execute the installation command

make install

At this time, the nginx directory has been generated under /usr/local.

(4) View nginx version information:

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

The output as shown in the figure below means that nginx has been successfully installedView nginx version information

4. Start nginx

1. Check whether the syntax in the configuration file is correct

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

As shown in the figure below, it is the test result of the correct writing of the configuration file. If an error is reported, please check the configuration file (in the installation directory/config/nginx.conf)

nginx checks whether the configuration file is correct

This step is to test the configuration file, check whether the syntax of the configuration file is correct, and then try to open the configuration involved in the file. This step is successful, nginx can start up.

2. Start
to enter the executable directory sbin

cd /usr/local/nginx/sbin/

start 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 is to reload the configuration file. In order for the main process to re-read the configuration file, a HUP signal is sent to the main process. Once the main process receives the signal to reload the configuration, it checks the validity of the syntax of the configuration file. properties, and then try to apply the new configuration, which opens a new log file and a new socket to listen to, if it fails, it rolls back the configuration changes and continues to use the old configuration, if it succeeds, it starts a new worker process, and gives The old worker process sends a message to shut them down gracefully. After receiving the shutdown signal, the old worker process will no longer receive new requests, and will close after the current request is processed.

3. Visit the nginx page

http://IP address: port number As shown in the figure ngixn默认端口号是80,可在配置文件中(在安装目录/config/nginx.conf)查看修改。
below , it is the successful page (the file of this page is in /usr/local/nginx/html/index.html).

Visit nginx static home page
没成功原因
(1) If it cannot be accessed, please check whether the nginx port exists, and no output indicates that it does not exist.

netstat -nltp | grep 80

(2) The firewall needs to be closed. If you don’t want to close it, please go to step (3) directly.

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

(3) If you do not want to close the firewall, you need to add the nginx port that the firewall opens to the outside world.

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

Five, nginx other configuration

1.nginx is set to boot automatically, the file to be operated is /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 configuration file
nginx initial configuration file (/usr/local/nginx/conf/nginx.conf, 没做任何改动) content is as follows


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

}

Configuration file parameter description ( 有很多,这里只说我自己的理解,如有错误,请指出,谢谢)


#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 still has a lot of knowledge points and is constantly learning.


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

Guess you like

Origin blog.csdn.net/qq_45337268/article/details/126676667