博客部署002-centos安装nginx

1、centos 如何安装nginx?

在CentOS系统上安装Nginx的过程相对直接,通常可以通过系统自带的Yum包管理器来安装。以下是安装Nginx的最新稳定版的步骤:

1.1 更新系统软件包
   

在安装Nginx之前,首先确保系统软件包是最新的,运行以下命令:
  

   sudo yum update

1.2. 安装EPEL存储库(如果尚未安装)

对于CentOS,有时需要额外安装EPEL(Extra Packages for Enterprise Linux)扩展存储库,以便获取Nginx软件包。运行:

sudo yum install epel-release

1.3. 安装Nginx

使用Yum命令安装Nginx:

 sudo yum install nginx

1.4. 启动Nginx服务并设置开机启动

安装完成后,启动Nginx服务,并设置为开机自动启动:

   sudo systemctl start nginx
   sudo systemctl enable nginx

1.5. 验证安装

检查Nginx是否安装成功并正在运行,可以查看服务状态:

sudo systemctl status nginx

或者访问服务器IP地址,如果能看到Nginx的欢迎页,那就说明Nginx已成功安装并运行:

curl http://localhost 或者在浏览器中打开 http://localhost 或 http://服务器IP地址

2、centos 如何重启nginx?

在 CentOS 系统中重启 Nginx 服务有多种方法,以下是几种常见的重启方式:

方法 1:使用 systemd 服务管理器(推荐)

在较新的 CentOS 版本中(如 CentOS 7 及以后版本),Nginx 服务通常通过 systemd 进行管理。您可以使用以下命令重启 Nginx:

# 启动
sudo systemctl start nginx

# 重启

sudo systemctl restart nginx

方法 2:使用 service 命令

sudo service nginx restart

注意事项

  • 请确保 Nginx 已经正确安装并且已经在系统中注册为服务。
  •  如果 Nginx 是通过第三方仓库或者手动编译安装,其启动脚本可能不在上述默认位置,这时需根据实际安装位置调用相应命令。
  • 以上命令均需要 root 权限才能执行。

3、如何查看nginx运行状态?

在Linux查看Nginx运行状态有不同的方法:

3.1 使用systemctl命令(适用于Systemd管理的系统,如CentOS 7+,Ubuntu 15.04+等)

sudo systemctl status nginx

这条命令将会显示Nginx服务的状态,包括是否正在运行,最近的启动和停止时间,以及任何相关的错误信息。

3.2 使用ps命令查看进程状态:

ps aux | grep nginx

这条命令会列出所有与nginx相关的进程,通过观察输出判断nginx主进程是否存在,可以了解nginx是否在运行。

4、启动nginx报错: Failed to start The nginx HTTP and reverse proxy server.

启动Nginx报错“Failed to start The nginx HTTP and reverse proxy server.”表示Nginx未能成功启动HTTP服务器和反向代理服务。为了解决这个问题,首先需要定位错误的原因。以下是一些常见的故障排查步骤:

4.1 查看错误日志

Nginx的错误日志通常存放在 `/var/log/nginx/error.log` (Linux系统)或安装目录下的 `logs` 子目录中(Windows系统)。通过查看错误日志可以获得具体的错误信息,有助于确定问题所在。

 示例命令:

 tail -f /var/log/nginx/error.log

4.2 配置文件检查🌟

确保Nginx的配置文件没有错误。可以使用Nginx内置的配置文件测试工具进行测试:

sudo nginx -t

如果配置文件有错误,它会显示出错的地方。

5、我浏览器可以访问http://121.43.75.xxx:80。 这个,但是不确定能不能访问8080端口,我用什么命令可以测试8080端口是否是通的?这样可以排除防火墙的问题?

5.1 使用 Telnet🌟

1telnet 121.43.75.xxx 8080

运行上述命令后,如果端口开放并且服务可用,你会建立到该端口的连接。如果端口关闭或者防火墙阻止了访问,通常会在几秒钟内没有任何响应,或者显示类似“无法打开到主机的连接”的错误信息。

5.2 使用 nc (netcat)

1nc -vz 121.43.75.xxx 8080

如果是阿里云,去开通ACL 8080端口 

6、 nginx.conf简单配置案例demo

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

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

    server {
        listen       8080;
        server_name  _;
        root         /var/www/client;
        index index.html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
            proxy_pass http://localhost:8083/;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

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

    server {
        listen       8081;
        server_name  _;
        root         /var/www/admin;
        index index.html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
            proxy_pass http://localhost:8083/;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

  • 36
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值