Linux安装Nginx(错过你都要后悔没看过这么详细的安装教程)

 

1、安装所需环境

//安装gcc 
yum install gcc-c++   

//安装PCRE pcre-devel 
yum install -y pcre pcre-devel  
 
//安装zlib 
yum install -y zlib zlib-devel   

//安装Open SSL 
yum install -y openssl openssl-devel

2、创建nginx目录并下载安装包

//进入usr/local目录 
cd /usr/local   

//创建nginx目录 
mkdir nginx   

//进入nginx目录 
cd nginx   

//下载 tar 包 
wget http://nginx.org/download/nginx-1.20.1.tar.gz   

//解压tar 
tar -xvf nginx-1.20.1.tar.gz

3、安装nginx

//进入nginx目录 
cd /usr/local/nginx/nginx-1.20.1 

//执行命令 
./configure --prefix=/usr/local/nginx


//执行 make命 令(要是执行不成功请检查最开始安装的四个有没有安装成功) 
make 

//执行 make install命令 
make install

4、启动nginx

//进入 bn/usr/local/nginx/sbin 目录,输入./nginx 即可启动nginx 
./nginx 

//关闭nginx 
./nginx -s quit 或者 ./nginx -s stop 

//重启nginx 
./nginx -s reload 

//查看nginx进程 
ps -ef|grep nginx

5、设置开启自启动

//编辑 
vim /etc/rc.local 

//最底部增加这一行 
/usr/local/nginx/sbin/nginx

5、使用命令进入 /usr/local/nginx/conf 找到 nginx.conf 文件

cd /usr/local/nginx/conf 找到nginx.conf

6、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 {
    # 使用 epoll 事件驱动
	#use epoll;

	# 最大连接数
    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 发送模式
    #tcp_nopush     on;
    #tcp_nodelay on;

	# 长连接超时时间
    keepalive_timeout  65;

    # 请求头
    #client_header_buffer_size 32k;
    #large_client_header_buffers 4 32k;

    # 请求体
    #client_body_buffer_size  128k;
    client_max_body_size  500m;

    # 设置 gzip
    #gzip  on;
    #gzip_disable "msie6";
    #gzip_vary on;
    #gzip_proxied any;
    #gzip_comp_level 6;
    #gzip_buffers 16 8k;
    #gzip_http_version 1.1;
    #gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
    # 代理服务器相关设置
    #proxy_connect_timeout  75;
    #proxy_read_timeout  75;
    #proxy_send_timeout  100;


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /dist;#前端文件存放路径
	    try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        
        location /prod-api/ {
            # 添加反向代理服务器
            proxy_pass  http://127.0.0.1:8080/;
            #proxy_redirect off;
	        #proxy_pass_header  Set-Cookie;
	        proxy_set_header Host $http_host;
	        proxy_set_header X-Real-Ip $remote_addr;
	        proxy_set_header REMOTE-HOST $remote_addr;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

	        # 添加请求头
	        #add_header Access-Control-Allow-Origin * always;
	        #add_header Access-Control-Allow-Credentials 'true' always;
	        #add_header Access-Control-Allow-Methods 'PUT, GET, POST, DELETE, OPTIONS' always;
	        #add_header Access-Control-Allow-Headers * always;

	        # 处理 OPTION 请求
	        #if ($request_method = 'OPTIONS') {
	        #	return 204;
	        #}
   	    }

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

}

7、重启nginx

1、进入nginx安装目录sbin下,输入命令./nginx -s reload 即可

2、使用 ps ef | grep nginx 查找nginx进程,用 kill -9 进程号 杀死该进程

      进入nginx安装目录sbin下,输入命令./nginx 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨_客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值