Nginx 部署安装


Nginx 安装

环境:阿里云 CentOS7 个人服务器

1. 下载解压


下载地址:http://nginx.org/en/download.html
选择 Linux 最新稳定版下载,浏览器下载很慢,建议复制下载链接迅雷下载

2. 上传到服务器


将前面下载完成的压缩包上传到服务器的/usr/local/yangguang/software/nginx

3. 配置

  • 第一步:./configure

可以看到有一个configure文件,我们通过这个进行默认的配置,执行以下命令

chmod +x configure
./configure

注意:如果此时报错缺乏C compiler cc is not found,则需要先安装 c 编译器

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

yum -y install pcre-devel openssl openssl-devel gd-devel 

报错:./configure: error: the HTTP rewrite module requires the PCRE library.

解决方法:

yum -y install pcre

报错:./configure: error: SSL modules require the OpenSSL library.

解决方法:

yum -y install openssl openssl-devel

报错:nginx: [emerg] getpwnam(“nginx”) failed

useradd -s /sbin/nologin -M nginx
  • 第二步:make
  • 第三步:make install

4. 启动和配置conf.d


安装完成后,我们用whereis命令查找nginx文件

[root@zsr nginx-1.18.0]# whereis nginx nginx: /usr/bin/nginx /usr/local/nginx

然后我们进入到/usr/local/nginx目录,可以查看到相关文件

[root@zsr nginx-1.18.0]# cd /usr/local/nginx/ 
[root@zsr nginx]# ls
conf  html  logs  sbin

我们再进入到/sbin目录,可以看到nginx可执行程序,我们直接执行进行启动

[root@zsr conf]# cd ./sbin 
[root@zsr sbin]# ls nginx
[root@zsr sbin]# ./nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

可以看到这里报错了,这是因为端口占用的问题,nginx 默认 80 端口启动,我们可以查以下80端口的占用情况

COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
AliYunDun  1179 root   22u  IPv4  17524      0t0  TCP iZ2ze3zdx4jq8v6hetjjuxZ:39786->100.100.30.25:http (ESTABLISHED)
nginx     11106 root   11u  IPv4 206832      0t0  TCP *:http (LISTEN)
nginx     11107  www   11u  IPv4 206832      0t0  TCP *:http (LISTEN)

可以看到被三个进程占用,这时我们可以结束这三个进程,也可以通过修改配置文件修改nginx的启动端口

这里我们修改默认的 80 端口,修改 nginx 目录下的/conf/nginx_conf文件,这里修改为 90 端口

[root@zsr conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@zsr conf]# vim 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;
    #    }
    #}
    include conf.d/*.conf;
    }

在conf.d文件夹创建8087端口代理

  [root@zsr nginx]# cd conf
  [root@zsr conf]# mkdir conf.d 
  [root@zsr conf]# vim dev.conf

server {
    listen 8087;
    server_name localhost;
    location / {
       proxy_set_header X-real-ip $remote_addr;
       proxy_set_header Host $host:$server_port;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_redirect off;
       proxy_pass http://127.0.0.1:8080;
       proxy_connect_timeout 10;
       proxy_read_timeout 30;
   }

   error_page 500 502 503 504 /50x.html;
   location = /50x.html {
       root html;
   }
}

然后再次启动

[root@zsr conf]# cd ../sbin/ 
[root@zsr sbin]# ./nginx -s start

没有任何返回信息则代表启动成功,我们通过服务器的公网IP:90访问测试,记得要打开阿里云 ECS 安全组的 90 端口以及防火墙的 90 端口!

#防火墙开放90端口 firewall-cmd --permanent --add-port=90/tcp
#重启防火墙(修改配置后要重启防火墙) firewall-cmd --reload

yg9538 2022年9月17日 16:54 1038 收藏文档