CentOS下nginx的安装流程
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>1 基础配置</li><li>2 安装依赖库<ul><li>2.1 安装g++编译器</li><li>2.2 安装pcre库</li><li>2.3 安装zlib库</li><li>2.4 安装OpenSSL库</li></ul></li><li>3 安装nginx<ul><li>3.1 获取nginx包</li><li>3.2 解压nginx文件</li><li>3.3 配置nginx安装选项</li><li>3.4 添加第三方模块</li><li>3.5 编译nginx</li><li>3.6 安装nginx</li></ul></li><li>4 运行nginx服务</li><li>5 访问nginx网址</li><li>6 启动和运行可能遇到的问题</li></ul></div><p></p><h2 id="1-基础配置">1 基础配置</h2>
<p>系统:CentOS Linux release 7.9.2009 (Core)<br>
NginX版本:nginx-1.20.1</p>
<p>查看操作系统名称:</p>
<pre><code class="language-none">cat /etc/centos-release
</code></pre>
<p>查看相关系统信息:</p>
<pre><code class="language-none">uname -a
uname --help 可以查看具体意义
</code></pre>
<h2 id="2-安装依赖库">2 安装依赖库</h2>
<h3 id="21-安装g编译器">2.1 安装g++编译器</h3>
<p>g++编译器——用于编译c++代码。</p>
<blockquote>
<p>解决报错“error: c compiler cc is not found.”</p>
</blockquote>
<p>g++编译器可以处理c++,它也包含gcc编译器,可以处理c代码,安装命令:</p>
<pre><code class="language-none">yum install -y gcc-c++
</code></pre>
<blockquote>
<p>如果只用c的话,似乎可以只安装gcc编译器(用于编译c代码):</p>
<blockquote>
<pre><code class="language-none">yum install -y gcc
</code></pre>
</blockquote>
</blockquote>
<h3 id="22-安装pcre库">2.2 安装pcre库</h3>
<p>pcre库——用C语言编写的正则表达式函数库。</p>
<blockquote>
<p>解决报错“error: the HTTP rewrite module requires the PCRE library.”</p>
</blockquote>
<pre><code class="language-none">yum install -y pcre pcre-devel
</code></pre>
<h3 id="23-安装zlib库">2.3 安装zlib库</h3>
<p>zlib库——压缩/解压缩工具库。</p>
<pre><code class="language-none">yum install -y zlib zlib-devel
</code></pre>
<h3 id="24-安装openssl库">2.4 安装OpenSSL库</h3>
<p>OpenSSL库——SSL协议库。</p>
<blockquote>
<p>解决报错”error: SSL modules require the OpenSSL library.”</p>
</blockquote>
<pre><code class="language-none">yum install -y openssl openssl-devel
</code></pre>
<h2 id="3-安装nginx">3 安装nginx</h2>
<p>从开源nginx(它也有商业版)的官网获取最新版本的nginx下载链接:</p>
<blockquote>
<p>官网:https://nginx.org/<br>
当前(2021-08)最新的Stable版本链接:https://nginx.org/download/nginx-1.20.1.tar.gz</p>
</blockquote>
<h3 id="31-获取nginx包">3.1 获取nginx包</h3>
<p>进入一个目录,这个目录是用来存放安装包,并进行前期配置、编译的目录,并不是实际的安装目录,执行下载命令:</p>
<pre><code class="language-none">wget https://nginx.org/download/nginx-1.20.1.tar.gz
</code></pre>
<blockquote>
<p>如果提示没有wget,需要先安装wget:</p>
<blockquote>
<pre><code class="language-none">yum install wget
</code></pre>
</blockquote>
</blockquote>
<h3 id="32-解压nginx文件">3.2 解压nginx文件</h3>
<pre><code class="language-none">tar -zxvf nginx-1.20.1.tar.gz
</code></pre>
<blockquote>
<p>-z : 使用 gzip 来压缩和解压文件<br>
-x : 从存档展开文件<br>
-v : 详细显示处理的文件<br>
-f : 指定需要处理的文件或设备,这个选项通常是必选的</p>
</blockquote>
<h3 id="33-配置nginx安装选项">3.3 配置nginx安装选项</h3>
<p>进入解压完成的nginx-1.20.1目录:</p>
<pre><code class="language-none">cd nginx-1.20.1
</code></pre>
<p>运行配置文件:</p>
<pre><code class="language-none">./configure --prefix=安装路径
</code></pre>
<p>配置文件可以默认,也可以按照需要附加参数,以此来选择附带的功能和模块,建议至少通过“<code>--prefix=安装目录</code>”指定一个安装目录,否则默认安装会把binary、modules、configure、pid、error、access、temp等各种文件分散到各个系统文件夹中,这里给出一个完整的参考示例:</p>
<pre><code class="language-none">./configure --prefix=/web/webserver/nginx-vod-hls --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-file-aio --with-threads --with-cc-opt="-O3" --with-http_ssl_module --with-openssl-opt=enable --with-http_flv_module --with-http_mp4_module --add-module=../nginx-vod-module-master --add-module=../nginx-rtmp-module-master
</code></pre>
<blockquote>
<p>示例配置中添加了nginx-vod-module与nginx-rtmp-module两个第三方模块,它们是通过外部下载的,不是nginx的基本模块,添加第三方模块的流程可以参考3.4节,如果不需要第三方模块可以去掉<code>--add-module=</code>开头的两句配置</p>
</blockquote>
<p>配置完成会显示配置总结,指明nginx各个文件存放的位置:</p>
<blockquote>
<p>Configuration summary<br>
<code>+</code> using threads<br>
<code>+</code> using system PCRE library<br>
<code>+</code> using system OpenSSL library<br>
<code>+</code> using system zlib library</p>
<p>nginx path prefix: "/web/webserver/nginx-vod-hls"<br>
nginx binary file: "/web/webserver/nginx-vod-hls/sbin/nginx"<br>
nginx modules path: "/web/webserver/nginx-vod-hls/modules"<br>
nginx configuration prefix: "/web/webserver/nginx-vod-hls/conf"<br>
nginx configuration file: "/web/webserver/nginx-vod-hls/conf/nginx.conf"<br>
nginx pid file: "/web/webserver/nginx-vod-hls/logs/nginx.pid"<br>
nginx error log file: "/web/webserver/nginx-vod-hls/logs/error.log"<br>
nginx http access log file: "/web/webserver/nginx-vod-hls/logs/access.log"<br>
nginx http client request body temporary files: "client_body_temp"<br>
nginx http proxy temporary files: "proxy_temp"<br>
nginx http fastcgi temporary files: "fastcgi_temp"<br>
nginx http uwsgi temporary files: "uwsgi_temp"<br>
nginx http scgi temporary files: "scgi_temp"</p>
</blockquote>
<p>关于文件路径的一些解释:</p>
<pre><code class="language-none">nginx path prefix: 安装路径
nginx binary file: 二进制文件路径
nginx modules path: 模块存放路径
nginx configuration prefix: 配置文件路径
nginx configuration file: 配置文件
nginx pid file: 进程文件
nginx error log file: 报错日志文件
nginx http access log file: 访问日志文件
…… ……
nginx http xxxx temporary files: 各种临时文件
</code></pre>
<h3 id="34-添加第三方模块">3.4 添加第三方模块</h3>
<p>添加第三方模块进行共同编译需要指定<code>--add-module</code>选项,在该前缀后跟上第三方模块的路径(示例中是相对路径<code>../nginx-vod-module-master</code>,根据实际情况可能有所不同)。</p>
<blockquote>
<p>nginx-vod-module下载地址:https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz<br>
nginx-rtmp-module下载地址:https://github.com/arut/nginx-rtmp-module/archive/refs/tags/v1.2.2.tar.gz</p>
</blockquote>
<p>与安装nginx一样,进入相应的目录(示例中把模块与nginx源文件夹放在同一层级),进行安装:</p>
<p>获取第三方模块nginx-vod-module</p>
<blockquote>
<p>wget https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz</p>
</blockquote>
<p>解压文件包</p>
<blockquote>
<p>tar -zxvf 1.28.tar.gz</p>
</blockquote>
<p>重命名文件夹(只为了方便使用)</p>
<blockquote>
<p>mv /.../1.28 /.../nginx-vod-module-master</p>
</blockquote>
<p>之后再按照3.3节中的方法在nginx的<code>./configure</code>配置过程中加上<code>--add-module</code>选项,并指定对应的模块路径即可。</p>
<blockquote>
<p>这里注意是在nginx的配置过程中进行指定,而不是进入模块nginx-vod-module的目录去配置。</p>
</blockquote>
<pre><code class="language-none">./configure ... ... --add-module=../nginx-vod-module-master
</code></pre>
<p>在配置过程中,还可以附加上相应的gcc编译命令:</p>
<pre><code class="language-none">CFLAGS="-Wno-unused-but-set-variable" ./configure ... ...
</code></pre>
<h3 id="35-编译nginx">3.5 编译nginx</h3>
<p>按照之前指定的配置来编译nginx,执行命令:</p>
<pre><code class="language-none">make
</code></pre>
<h3 id="36-安装nginx">3.6 安装nginx</h3>
<p>安装时会将nginx安装到之前指定的安装路径,执行命令:</p>
<pre><code class="language-none">make install
</code></pre>
<h2 id="4-运行nginx服务">4 运行nginx服务</h2>
<p>安装完成即可启动nginx服务,注意需要指定二进制文件的路径,或者进入二进制文件所在的目录,执行不带参数的nginx命令即可:</p>
<pre><code class="language-none">./nginx
</code></pre>
<p>可以用以下命令查看是否有nginx进程在运行:</p>
<pre><code class="language-none">ps -ef | grep nginx
</code></pre>
<p>直接关闭nginx服务:</p>
<pre><code class="language-none">./nginx -s stop
</code></pre>
<p>优雅地关闭nginx服务(nginx 主进程会等待 worker 进程完成当前用户请求的处理):</p>
<pre><code class="language-none">./nginx -s quit
</code></pre>
<p>验证nginx配置文件是否正确,用于修改conf配置文件后:</p>
<pre><code class="language-none">./nginx -t
</code></pre>
<p>重载nginx服务,用于修改conf配置文件后重载:</p>
<pre><code class="language-none">./nginx -s reload
</code></pre>
<p>重新打开日志文件:(刷新日志文件)</p>
<pre><code class="language-none">./nginx -s reopen
</code></pre>
<p>使用指定的配置文件运行nginx:</p>
<pre><code class="language-none">./nginx -c filename
</code></pre>
<h2 id="5-访问nginx网址">5 访问nginx网址</h2>
<p>如果是在本机运行,可以访问127.0.0.1或者localhost,默认80端口:</p>
<pre><code class="language-none">http://127.0.0.1/
http://localhost/
</code></pre>
<p>如果是在服务器运行,可以访问服务器ip,默认80端口(可省略):</p>
<pre><code class="language-none">http://ServerIP:80/
</code></pre>
<p>如果显示nginx欢迎界面则表示nginx运行成功。</p>
<h2 id="6-启动和运行可能遇到的问题">6 启动和运行可能遇到的问题</h2>
<p>pid错误</p>
<blockquote>
<p>描述:<br>
<strong>nginx: open() "/xxxx/nginx.pid" failed.</strong><br>
原因:<br>
进程占用导致,一个相同nginx进程正在运行。<br>
解决:<br>
结束旧的进程后,新进程才能启动。</p>
<blockquote>
<p>关闭nginx:./nginx -s stop<br>
直接关闭进程:kill -9 processid</p>
</blockquote>
</blockquote>
<p>无法访问nginx网址</p>
<blockquote>
<p>描述:<br>
<strong>配置都正确,但nginx网址无法访问。</strong><br>
原因:<br>
可能是防火墙没有配置需要开放的端口。<br>
解决:<br>
配置防火墙开放端口号。</p>
<p>CentOS 7 默认的防火墙是firewall:</p>
<blockquote>
<p>查看防火墙状态:<br>
firewall-cmd --state</p>
<p>查看开放的端口:<br>
firewall-cmd --list-ports<br>
firewall-cmd --list-all</p>
<p>单独查看8081端口:<br>
firewall-cmd --zone=public --query-port=8081/tcp</p>
<p>开放8081端口:<br>
firewall-cmd --zone=public --add-port=8081/tcp --permanent</p>
<p>关闭8081端口:<br>
firewall-cmd --zone=public --remove-port=8081/tcp --permanent</p>
<blockquote>
<p>–zone #作用域<br>
–add-port=8081/tcp #添加端口,格式为:端口/通讯协议<br>
–permanent #永久生效,没有此参数重启后失效</p>
</blockquote>
<p>端口操作完成,都需要重载防火墙才生效:<br>
firewall-cmd --reload</p>
</blockquote>
<p>CentOS 6 默认的防火墙是iptables</p>
<blockquote>
<p>开放端口(修改iptables文件)<br>
vim /etc/sysconfig/iptables<br>
添加以下信息(使用“端口号”开放单个端口,使用“端口号:端口号”,可以开放范围内的所有端口)<br>
-A INPUT -m state -state NEW -m tcp -p tcp -dport 端口号 -j ACCEPT</p>
<p>重启防火墙即可生效<br>
service iptables restart</p>
<p>打开或关闭防火墙<br>
chkconfig iptables on/off ——永久<br>
service iptables start/stop ——临时,重启系统后失效</p>
</blockquote>
</blockquote><br><br>
来源:https://www.cnblogs.com/seedoubleu/p/15645055.html
頁:
[1]