加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Ubuntu 安装 Nginx

发布时间:2023-05-29 14:37:24 所属栏目:教程 来源:
导读:这一节学习在 Ubuntu 上搭建 Nginx 服务。本次实验环境为 Ubuntu 18.04, Nginx 版本为 1.17.6。

下载源码包并解压
打开终端并运行下面两条命令即可下载并解压 Nginx :
$ wget http://Nginx.org/download/Nginx
这一节学习在 Ubuntu 上搭建 Nginx 服务。本次实验环境为 Ubuntu 18.04, Nginx 版本为 1.17.6。

下载源码包并解压
打开终端并运行下面两条命令即可下载并解压 Nginx :
$ wget http://Nginx.org/download/Nginx..tar.gz
$ tar -xzf Nginx..tar.gz

预装依赖包
Nginx 是完全用 c 语言编写的,所以想要编译 Nginx,必须要有 c 编译器(gcc), 只要系统里有 gcc, Nginx 就可以编译安装。

但是往往我们会用的到 Nginx 的一些重要功能,比如压缩和解压缩功能,这时就必须需要依赖 zlib 库,想在配置文件中使用正则表达式,就必须安装 pcre 库,最后想实现 SSL/TLS 功能,必须安装 openssl 库。

无论是在 Ubuntu 还是 CentOS 系统中都大致如此,只不过管理软件包的工具不一样,依赖库的名称也不一样。在 Ubuntu 系统下,在 Ubuntu 中执行如下命令安装依赖库:

$ sudo apt-get update                # 更新下apt源$ sudo apt-get install gcc           # Nginx必备$ sudo apt-get install make          # 编译安装需要make工具$ sudo apt-get install libz-dev
$ sudo apt-get install libpcre3-dev
$ sudo apt-get install libssl-dev

编译并安装
这样 Nginx 将会安装到默认的 /usr/local/Nginx 目录,可执行文件是 /usr/local/Nginx/sbin/Nginx,默认的配置文件是 /usr/local/Nginx/conf/Nginx.conf。

$ cd Nginx-1.17.6
$ ./configure                # 编译,检查依赖是否正确$ make && sudo make install  # 安装
如果想自定义比如安装目录、编译新的模块或者第三方模块,需要使用相应的参数。

# 参数说明:--prefix=PATH: #指定安装目录--with-xxx_module: #添加某某模块, xxx为对应的模块名称,比如--with-http_ssl_module等,这是针对没有默认编译进Nginx的模块--without-xxx_module: #禁止某模块编译进 Nginx , xxx 为对应模块名称,比如--without-stream_access_module 等,这是针对默认会编译进 Nginx 的模块--add-module=PATH: #添加第三方 Nginx 模块,指定模块路径即可,非常简单# 对于查看具体支持哪些参数选项,包括所有可以加入和禁止编译进Nginx的模块,# 通过如下命令查询:$ ./configure --help# 我们为后续开展测试,多添加几个模块进Nginx,让编译出来的Nginx具备更多功能$ ./configure  --prefix=/root/Nginx            \--with-http_ssl_module                       \--with-http_stub_status_module               \--with-http_gzip_static_module               \--with-stream                                \--with-http_realip_module                    \--with-http_flv_module                       \--with-http_random_index_module              \--with-mail                                  \--with-pcre                                  \# 安装$ make && sudo make install

启动 Nginx 服务
安装完成后,Nginx 的文件默认全在 --prefix 指定的目录中,即 /root/Nginx。编译出的 Nginx 二进制文件在 /root/Nginx/sbin 目录下,默认的配置文件为 /root/Nginx/conf/Nginx.conf。我们可以直接启动 Nginx:

$ cd /root/Nginx/sbin
$ ./Nginx
$ curl http://localhost  # 测试 Nginx 服务是否启动成功
如果最后一步测试,发现返回 403(权限拒绝)的结果,我们可以修改下 Nginx.conf 的配置,将 Nginx.conf 中第一行 user 指令参数设置为 root,然后在重启或者热加载 Nginx 并执行 curl 请求,查看结果。

$ cd /root/Nginx/conf
$ vim Nginx.conf
$ cat Nginx,conf# 指定Nginx以root用户启动user root;...# 这次就能正确返回'Welcome to Nginx'这样的信息了$ curl http://localhost
另外从浏览器上直接请求,也可以看到欢迎页面。
Tips:对于百度、阿里、腾讯这样的云主机,需要事先放通 80 端口,允许外面通过 80 端口访问服务,以及数据从 80 端口出去。

Nginx 服务的操作
$ cd /root/Nginx/sbin# 查看版本信息$ ./Nginx -v# 查看详情,可以看到编译进Nginx中的模块$ ./Nginx -V# stop|quit: 停止服务,区别和windows中的一致 reload:热加载 reopen:重新打开日志文件$ ./Nginx -s stop|reload|reopen  
# -c 指定配置文件,默认就是Nginx安装目录下的conf/Nginx.conf文件$ ./Nginx -c /root/Nginx/conf/Nginx.conf    
# -t 测试配置文件语法是否正常$ ./Nginx -tc /root/Nginx/conf/Nginx.conf
来看看在服务器上执行 Nginx 命令的结果:

$ cd /root/Nginx/sbin
$ ./Nginx -t
Nginx: the configuration file /root/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /root/Nginx/conf/Nginx.conf test is successful
 
$ ./Nginx -V
Nginx version: Nginx/.built by gcc .0 (Ubuntu .-lubuntu1~.)built with OpenSSL .2k-fips   Jan 
TLS SNI support enabled
configure arguments: --prefix=/root/Nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-http_realip_module --with-http_flv_module --with-http_random_index_module --with-mail --with-pcre

这里,我们讲解了在 Ubuntu 系统上搭建 Nginx 服务的完整过程。相比其他互联网组件而言,搭建 Nginx 的过程是非常顺畅的,这极大地方便了学习 Nginx 的用户。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章