Ubuntu 安装 Nginx
这一节学习在 Ubuntu 上搭建 服务。本次实验环境为 Ubuntu 18.04, 版本为 1.17.6(截止到 2019 年 12 月 12 日最新版本)。
1. 下载源码包并解压
打开终端并运行下面两条命令即可下载并解压 :
$ wget http://.org/download/..tar.gz $ tar -xzf ..tar.gz
2. 预装依赖包
是完全用 c 语言编写的,所以想要编译 ,必须要有 c 编译器(gcc), 只要系统里有 gcc, 就可以编译安装。
但是往往我们会用的到 的一些重要,比如压缩和解压缩,这时就必须需要依赖 zlib 库,想在中使用正则表达式,就必须安装 pcre 库,最后想实现 SSL/TLS ,必须安装 openssl 库。
无论是在 Ubuntu 还是 CentOS 系统中都大致如此,只不过管理软件包的工具不一样,依赖库的也不一样。在 Ubuntu 系统下,在 Ubuntu 中执行如下命令安装依赖库:
$ sudo apt-get update # 更新下apt源$ sudo apt-get install gcc # 必备$ sudo apt-get install make # 编译安装需要make工具$ sudo apt-get install libz-dev $ sudo apt-get install libpcre3-dev $ sudo apt-get install libssl-dev
3. 编译并安装
傻瓜式,使用认安装配置,以及认安装模块。这样 将会安装到认的 /usr/local/ 目录,可执行是 /usr/local//sbin/,认的是 /usr/local//conf/.conf。
$ cd -1.17.6 $ ./conure # 编译,检查依赖是否正确$ make && sudo make install # 安装
如果想比如安装目录、编译新的模块或者第三方模块,需要使用相应的参数。
# 参数说明:--prefix=PATH: #指定安装目录--with-xxx_module: #某某模块, xxx为对应的模块,比如--with-http_ssl_module等,这是针对没有认编译进的模块--without-xxx_module: #某模块编译进 , xxx 为对应模块,比如--without-stream_access_module 等,这是针对认会编译进 的模块--add-module=PATH: #第三方 模块,指定模块路径即可,非常简单# 对于查看具体哪些参数选项,所有可以加入和编译进的模块,# 通过如下命令:$ ./conure --help# 我们为后续开展测试,多几个模块进,让编译出来的具备更多$ ./conure --prefix=/root/ \--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
4. 启动 服务
安装完成后, 的认全在 --prefix 指定的目录中,即 /root/。编译出的 二进制在 /root//sbin 目录下,认的为 /root//conf/.conf。我们可以直接启动 :
$ cd /root//sbin $ ./ $ curl http://localhost # 测试 服务是否启动成功
如果最后一步测试,发现返回 (权限拒绝)的结果,我们可以下 .conf 的配置,将 .conf 中第一行 user 指令参数设置为 root,然后在重启或者热加载 并执行 curl 请求,查看结果。
$ cd /root//conf $ vim .conf $ cat ,conf# 指定以root启动user root;...# 这次就能正确返回'Welcome to '这样的信息了$ curl http://localhost
另外从浏览器上直接请求,也可以看到欢迎。
Tips:对于、阿里、腾讯这样的云主机,需要事先放通 80 端口,允许外面通过 80 端口访问服务,以及数据从 80 端口出去。
5. 服务的操作
$ cd /root//sbin# 查看版本信息$ ./ -v# 查看详情,可以看到编译进中的模块$ ./ -V# stop|quit: 停止服务,区别和windows中的一致 reload:热加载 reopen:重新打开日志$ ./ -s stop|reload|reopen # -c 指定,认就是安装目录下的conf/.conf$ ./ -c /root//conf/.conf # -t 测试语法是否正常$ ./ -tc /root//conf/.conf
来看看在服务器上执行 命令的结果:
$ cd /root//sbin $ ./ -t : the conuration file /root//conf/.conf is ok : conuration file /root//conf/.conf test is successful $ ./ -V version: /.built by gcc .0 (Ubuntu .-lubuntu1~.)built with OpenSSL .2k-fips Jan TLS SNI support enabled conure arguments: --prefix=/root/ --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
6. 安装操作视频演示
7. 小结
这里,我们讲解了在 Ubuntu 系统上搭建 服务的完整过程。相比其他互联网组件而言,搭建 的过程是非常顺畅的,这极大地方便了学习 的,不用再被部署服务搞得焦头烂额。此外,这里还简单介绍了几个 命令行参数,这些会在后面经常用到。