Nginx服务器.md 3.0 KB

Centos 下安装Nginx

1.安装所需的依赖

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

  • gcc 安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

    yum install -y gcc-c++
    
  • PCRE pcre-devel 安装 PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

    yum install -y pcre pcre-devel
    
  • zlib 安装 zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

    yum install -y zlib zlib-devel
    
  • OpenSSL 安装 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

    yum install -y openssl openssl-devel
    
  • 也可以一键安装多个依赖(过程可能稍微有些慢)

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

2.安装nginx

  • 下载nginx压缩包 http://nginx.org/download/
  • 将压缩包上传到服务器
  • 解压

    tar -zxvf nginx-1.18.0.tar.gz -C /usr/local
    
  • 安装

    cd /usr/local/nginx-1.18.0/
    ./configure 
    make
    make install
    

安装成功后,会自动生成一个nginx目录

3.启动nginx

  • 测试配置文件

    /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
  • 启动

    /usr/local/nginx/sbin/nginx 
    
  • 停止

    /usr/local/nginx/sbin/nginx -s stop
    
  • 重启

    /usr/local/nginx/sbin/nginx -s reload
    
  • 查看进程命令

    ps -ef | grep nginx
    root     30764     1  0 17:28 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
    nobody   30767 30764  0 17:28 ?        00:00:00 nginx: worker process
    root     30775 20039  0 17:29 pts/0    00:00:00 grep --color=auto nginx
    
  • 验证

访问 http://10.18.226.167/

  • 将nginx目录添加到环境变量中,就不用上面这么麻烦了

vi /etc/profile ,在文件末尾添加

  #nginx
  export PATH=/usr/local/nginx/sbin/:$PATH

Windows下nginx启动和关闭

启动:执行 nginx.exe 或者 start nginx

关闭:nginx -s stop 或  nginx -s quit(完整有序的停止nginx)

重启:nginx -s reload

配置

全面了解 Nginx 主要应用场景