Linux下安装nginx

1.whereis nginx

  首先查看服务器上是否已经安装过nginx,如果没有,继续。

2.cd usr/local

3. 安装依赖

   yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

   yum -y install wget httpd-tools vim
   yum -y install zlib zlib-devel
   yum -y install openssl openssl-devel  

 ⚠️:其中openssl, openssl-devel是为了支持https请求

4. 下载tar包(nginx

wget http://nginx.org/download/nginx-1.14.2.tar.gz

5. 解压

tar -xvf nginx-1.14.2.tar.gz

6. 安装(cd nginx-1.13.7)

 mkdir nginx
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_ssl_module
 make && make install(make->编译,make install->安装)

 注意:

  1)运行./configure报错: permission denied,

          说明文件不可执行,需要赋权:chmod +x configure

  2)运行make报错: make:*** No rule to make target `build', needed by `default'.  Stop.

           解决方法:

           !)yum update

           !!)删除现有的nginx解压的文件夹,重新解压

      3) 运行make install报错: 是同一个文件

    ⚠️--conf-path指的是最后的配置文件的位置,不是所在的文件夹,一定要到最后的文件

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

      4)当代理的服务器有https请求时,会报错如下:

         nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/nginx.conf

         原因: 编译时缺少--with-http_ssl_module命令,不支持ssl。

         需要重新编译使得支持ssl.

   !)为避免报错,需要重新编译的时候和上次编译的时候参数一致。

           如果想查看之前安装的时候的命令,可以使用sbin/nginx -V

          

         如图如果有配置,会显示在红色区域中,上图说明是通过自动配置生成。

       !!)如果源码还存在,回到nginx的源码目录下(如果已删除,回到第五步中重新解压),在原来配置的基础上,加上SSL支持参数重新编译:        

1  ./configure --with-http_ssl_module
2 make

      ⚠️ 不能接下来直接make install, 因为会覆盖掉原来的所有配置。

1 cp /usr/local/nginx/sbin/nginx  ~/
2 cp objs/nginx  /usr/local/nginx/sbin/
3 
4 // ~/根目录,第一行是将原来的可执行程序移除
5 // objs/nginx 是新编译出来的nginx可执行程序;第二行命令是将新的可执行程序复制到旧的可执行程序的位置

     !!!)  重新运行./nginx,启动。

     ⚠️ 如果出现以下错误

     

  说明上面的配置的时候conf-path没有设置到具体的文件,而是设置到了文件所在的文件夹。

      !!!!) 如果 -s reload之后还是无效,先./nginx -s stop 然后再启动。

7. 启动

   cd /usr/local/nginx/sbin

   运行./nginx

8.  如果访问不到,请确认服务器的该端口是否开启。

9. nginx.conf配置文件

  server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dist;
            index  index.html index.htm;
        }
  }
  server {
        listen       8082;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /{ //请求静态资源
            root   html/build;
            index  index.html index.htm;
        }
        location /api/ { // 请求接口代理到后端的接口
          proxy_pass http://localhost:8080/;
        }
  } 

10.重启nginx

   kill -HUP pid 。或者 sbin/nginx -s reload

11.查看nginx全部进程

   ps -ef | grep nginx(e:全部进程,f:全格式)

12.查看nginx的错误日志,来查看错误原因。

   logs/error.log

                 

原文地址:https://www.cnblogs.com/lyraLee/p/10696721.html