351 lines
484 KiB
HTML
351 lines
484 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html><head><title>Linux 下源代码编译与安装实战</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="Linux 下源代码编译与安装实战"/><meta property="og:description" content="Linux 下源代码编译与安装实战."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="Linux 下源代码编译与安装实战."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
|
|||
|
var _hmt = _hmt || [];
|
|||
|
(function() {
|
|||
|
var hm = document.createElement("script");
|
|||
|
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
|
|||
|
var s = document.getElementsByTagName("script")[0];
|
|||
|
s.parentNode.insertBefore(hm, s);
|
|||
|
})();
|
|||
|
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
|
|||
|
(function() {
|
|||
|
window.dataLayer = window.dataLayer || [];
|
|||
|
function gtag() {
|
|||
|
window.dataLayer.push(arguments);
|
|||
|
}
|
|||
|
gtag('js', new Date());
|
|||
|
gtag('config', 'G-MHMEL0F832');
|
|||
|
})();
|
|||
|
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Schibsted Grotesk:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Technology/OperatingSystem/Linux/2.安装/Linux-下源代码编译与安装实战"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9
|
|||
|
<p>编译和安装源代码是 Unix 和 Unix-like 系统中一个非常重要且基本的技能。通过这种方法,你不仅可以获得软件的最新版本,而且还可以根据自己的需求和偏好定制软件。在本指南中,我们将通过一个具体实例(Nginx 软件包)来详细讲解源代码编译和安装的全过程。</p>
|
|||
|
<pre><code class="mermaid">graph LR;
|
|||
|
A[开始] --> B[获取源代码];
|
|||
|
B --> C[安装编译依赖];
|
|||
|
C --> D[解压源代码];
|
|||
|
D --> E[配置];
|
|||
|
E --> F[编译];
|
|||
|
F --> G[安装];
|
|||
|
G --> H[测试];
|
|||
|
H --> I[运行和管理];
|
|||
|
I --> J[清理];
|
|||
|
J --> K[配置 Systemd 服务];
|
|||
|
K --> L[结束];
|
|||
|
|
|||
|
</code></pre>
|
|||
|
<h2 id="第一步准备工作">第一步:准备工作<a aria-hidden="true" tabindex="-1" href="#第一步准备工作" class="internal"> §</a></h2>
|
|||
|
<h3 id="1-获取源代码">1. 获取源代码<a aria-hidden="true" tabindex="-1" href="#1-获取源代码" class="internal"> §</a></h3>
|
|||
|
<p>在开始编译和安装过程之前,首先需要获取软件的源代码。这可以通过多种途径实现,例如直接从官方网站下载源代码压缩包,或使用版本控制系统(如 git)来克隆源代码仓库。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 wget 命令下载源代码压缩包</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">wget</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">http://example.com/software-x.y.z.tar.gz</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 或使用 git 克隆源代码仓库</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">clone</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">https://github.com/example/software.git</span></span></code></pre></div>
|
|||
|
<h3 id="2-安装编译依赖">2. 安装编译依赖<a aria-hidden="true" tabindex="-1" href="#2-安装编译依赖" class="internal"> §</a></h3>
|
|||
|
<p>在编译源代码之前,通常需要安装一些依赖库和工具。这些依赖包括编译器(如 gcc)和其他必要的库。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">update</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">build-essential</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libpcre3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libpcre3-dev</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">zlib1g</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">zlib1g-dev</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">openssl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">libssl-dev</span></span></code></pre></div>
|
|||
|
<h2 id="第二步配置与编译"><strong>第二步:配置与编译</strong><a aria-hidden="true" tabindex="-1" href="#第二步配置与编译" class="internal"> §</a></h2>
|
|||
|
<h3 id="1-解压源代码">1. 解压源代码<a aria-hidden="true" tabindex="-1" href="#1-解压源代码" class="internal"> §</a></h3>
|
|||
|
<p>如果源代码是压缩包形式,你需要解压它以访问源代码文件。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 tar 命令解压源代码压缩包</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">tar</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">xvf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">software-x.y.z.tar.gz</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 进入源代码目录</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">software-x.y.z</span></span></code></pre></div>
|
|||
|
<h3 id="2-configure-配置">2. Configure 配置<a aria-hidden="true" tabindex="-1" href="#2-configure-配置" class="internal"> §</a></h3>
|
|||
|
<p>配置是准备编译的重要步骤,通常通过运行 <code>configure</code> 脚本来完成。这个脚本会检测你的系统环境和参数,并准备好编译过程。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 运行 configure 脚本来配置软件包</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">./configure</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--prefix=/usr/local</span></span></code></pre></div>
|
|||
|
<p>常用的 <code>configure</code> 选项及其描述:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>描述</th></tr></thead><tbody><tr><td><code>--prefix=PREFIX</code></td><td>指定安装路径,例如 <code>/usr/local</code></td></tr><tr><td><code>--enable-FEATURE</code></td><td>启用某个特性</td></tr><tr><td><code>--disable-FEATURE</code></td><td>禁用某个特性</td></tr><tr><td><code>--with-PACKAGE</code></td><td>包含某个包的支持</td></tr><tr><td><code>--without-PACKAGE</code></td><td>不包含某个包的支持</td></tr><tr><td><code>--help</code></td><td>显示所有可用的 <code>configure</code> 选项</td></tr><tr><td><code>--version</code></td><td>显示程序版本和 <code>configure</code> 脚本信息</td></tr><tr><td><code>--quiet</code>, <code>--silent</code></td><td>禁止显示多余的输出</td></tr><tr><td><code>--libdir=DIR</code></td><td>指定库文件的安装目录</td></tr><tr><td><code>--includedir=DIR</code></td><td>指定头文件的安装目录</td></tr><tr><td><code>--mandir=DIR</code></td><td>指定 man 手册页面的安装目录</td></tr><tr><td><code>--sysconfdir=DIR</code></td><td>指定系统配置文件的安装目录</td></tr><tr><td><code>--localstatedir=DIR</code></td><td>指定本地状态数据(如日志文件)的安装目录</td></tr><tr><td><code>--enable-shared</code></td><td>启用或禁用共享库的构建</td></tr><tr><td><code>--enable-static</code></td><td>启用或禁用静态库的构建</td></tr><tr><td><code>--enable-debug</code></td><td>启用调试模式</td></tr><tr><td><code>--enable-optimizations</code></td><td>启用代码优化</td></tr><tr><td><code>--host=HOST</code></td><td>指定主机类型(用于交叉编译)</td></tr><tr><td><code>--build=BUILD</code></td><td>指定构建系统类型</td></tr></tbody></table>
|
|||
|
<h3 id="3-make-编译">3. Make 编译<a aria-hidden="true" tabindex="-1" href="#3-make-编译" class="internal"> §</a></h3>
|
|||
|
<p>配置完成后,我们可以开始编译源代码了。这一步通常通过 <code>make</code> 命令来完成,它根据 Makefile 文件中的指令进行源代码编译。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 make 命令来编译源代码</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">make</span></span></code></pre></div>
|
|||
|
<h4 id="常用目标">常用目标<a aria-hidden="true" tabindex="-1" href="#常用目标" class="internal"> §</a></h4>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>目标</th><th>描述</th></tr></thead><tbody><tr><td><code>all</code></td><td>编译所有的目标文件和链接成一个可执行程序(默认目标)</td></tr><tr><td><code>install</code></td><td>将编译好的程序和相关文件安装到指定的目录</td></tr><tr><td><code>clean</code></td><td>清理编译过程中产生的临时文件和目标文件</td></tr><tr><td><code>distclean</code></td><td>除了执行 <code>clean</code> 目标的操作外,还会清理配置文件</td></tr><tr><td><code>test</code> 或 <code>check</code></td><td>运行程序的测试套件</td></tr><tr><td><code>uninstall</code></td><td>从指定目录中移除之前通过 <code>make install</code> 安装的文件</td></tr></tbody></table>
|
|||
|
<h4 id="常用选项">常用选项<a aria-hidden="true" tabindex="-1" href="#常用选项" class="internal"> §</a></h4>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>描述</th></tr></thead><tbody><tr><td><code>-j [N]</code></td><td>允许同时执行 N 个作业(如果未指定 N,则自动选择基于可用 CPU 核心的数量)</td></tr><tr><td><code>-k</code> 或 <code>--keep-going</code></td><td>即使有错误发生也继续尽可能多的编译</td></tr><tr><td><code>--silent</code> 或 <code>-s</code></td><td>禁止显示执行的命令</td></tr><tr><td><code>--always-make</code> 或 <code>-B</code></td><td>无论目标文件是否最新,都重新构建所有目标</td></tr><tr><td><code>-f FILE</code> 或 <code>--file=FILE</code></td><td>指定一个非标准名称的 Makefile 文件来使用</td></tr><tr><td><code>--debug</code></td><td>为调试 <code>make</code> 而显示调试信息</td></tr><tr><td><code>-n</code> 或 <code>--just-print</code></td><td>显示但不执行命令</td></tr><tr><td><code>-q</code> 或 <code>--question</code></td><td>无输出模式,用来检查指定目标是否已经是最新的</td></tr><tr><td><code>-C DIRECTORY</code></td><td>在执行 <code>make</code> 命令前,先切换到指定的目录</td></tr></tbody></table>
|
|||
|
<h2 id="第三步安装与测试"><strong>第三步:安装与测试</strong><a aria-hidden="true" tabindex="-1" href="#第三步安装与测试" class="internal"> §</a></h2>
|
|||
|
<h3 id="1-安装">1. 安装<a aria-hidden="true" tabindex="-1" href="#1-安装" class="internal"> §</a></h3>
|
|||
|
<p>编译完成后,下一步是将软件包安装到系统中。这也是通过运行 <code>make</code> 命令完成的,但常配合 <code>install</code> 目标来将文件安装到正确的位置。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 make 命令来安装软件包</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">sudo make install</span></span></code></pre></div>
|
|||
|
<h3 id="2-测试">2. 测试<a aria-hidden="true" tabindex="-1" href="#2-测试" class="internal"> §</a></h3>
|
|||
|
<p>某些软件包提供了测试步骤,允许你在安装之前测试以确保一切正常。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 make 命令来测试软件包</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">make</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">test</span></span></code></pre></div>
|
|||
|
<h2 id="第四步后续管理和清理"><strong>第四步:后续管理和清理</strong><a aria-hidden="true" tabindex="-1" href="#第四步后续管理和清理" class="internal"> §</a></h2>
|
|||
|
<h3 id="1-运行和管理">1. 运行和管理<a aria-hidden="true" tabindex="-1" href="#1-运行和管理" class="internal"> §</a></h3>
|
|||
|
<p>安装完成后,你可以使用相应的命令来启动、停止或重载服务。例如,在 Nginx 的情况下,你可以使用以下命令:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 启动 Nginx 服务</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 停止 Nginx 服务</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">stop</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 重新加载 Nginx 配置</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">reload</span></span></code></pre></div>
|
|||
|
<h3 id="2-清理">2. 清理<a aria-hidden="true" tabindex="-1" href="#2-清理" class="internal"> §</a></h3>
|
|||
|
<p>最后,为了保持系统整洁,你可以选择清理编译时生成的临时文件。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 make 命令来清理编译生成的文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">make</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">clean</span></span></code></pre></div>
|
|||
|
<h2 id="第五步配置-systemd-服务">第五步:配置 Systemd 服务<a aria-hidden="true" tabindex="-1" href="#第五步配置-systemd-服务" class="internal"> §</a></h2>
|
|||
|
<p>如果你希望你的新安装的服务(例如 Nginx)更好地集成到系统中,可以考虑为它创建一个 Systemd 服务单元文件。这样,你就可以用 Systemd 来管理这个服务了。</p>
|
|||
|
<h3 id="1-创建-systemd-服务单元文件">1. 创建 Systemd 服务单元文件<a aria-hidden="true" tabindex="-1" href="#1-创建-systemd-服务单元文件" class="internal"> §</a></h3>
|
|||
|
<p>首先,我们需要创建一个新的 Systemd 服务单元文件。打开一个文本编辑器并创建一个新文件,通常位于 <code>/etc/systemd/system/</code> 目录,并以 <code>.service</code> 扩展名结尾。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nano</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/etc/systemd/system/nginx.service</span></span></code></pre></div>
|
|||
|
<h3 id="2-编辑服务单元文件">2. 编辑服务单元文件<a aria-hidden="true" tabindex="-1" href="#2-编辑服务单元文件" class="internal"> §</a></h3>
|
|||
|
<p>在这个文件中,我们将定义服务的各种属性,包括如何启动和停止服务。以下是一个简单的 Nginx Systemd 服务单元文件示例:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">[Unit]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">Description</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">The</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">NGINX</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">HTTP</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">and</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">reverse</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">proxy</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">server</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">After</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">network.target</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">[Service]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">Type</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">forking</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">PIDFile</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">/usr/local/nginx/logs/nginx.pid</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">ExecStartPre</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">-t</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">ExecStart</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">ExecReload</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">-s</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">reload</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">ExecStop</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">/usr/local/nginx/sbin/nginx</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">-s</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">stop</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">PrivateTmp</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-constant);">true</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">[Install]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">WantedBy</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string);">multi-user.target</span></span></code></pre></div>
|
|||
|
<h3 id="3-重新加载-systemd">3. 重新加载 Systemd<a aria-hidden="true" tabindex="-1" href="#3-重新加载-systemd" class="internal"> §</a></h3>
|
|||
|
<p>创建或修改任何 Systemd 服务单元文件后,你需要运行以下命令来重新加载 Systemd,使其识别新的或更改的服务单元文件:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">daemon-reload</span></span></code></pre></div>
|
|||
|
<h3 id="4-启动和启用服务">4. 启动和启用服务<a aria-hidden="true" tabindex="-1" href="#4-启动和启用服务" class="internal"> §</a></h3>
|
|||
|
<p>现在,你可以使用 <code>systemctl</code> 命令来启动 Nginx 服务,并将其设置为在系统启动时自动启动:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">start</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nginx</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">enable</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nginx</span></span></code></pre></div>
|
|||
|
<h3 id="5-状态和日志">5. 状态和日志<a aria-hidden="true" tabindex="-1" href="#5-状态和日志" class="internal"> §</a></h3>
|
|||
|
<p>你可以使用以下命令来检查服务的状态和日志:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 检查服务状态</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">systemctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nginx</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 查看服务日志</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">journalctl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-u</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nginx</span></span></code></pre></div></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#概述" data-for="概述">概述</a></li><li class="depth-0"><a href="#第一步准备工作" data-for="第一步准备工作">第一步:准备工作</a></li><li class="depth-1"><a href="#1-获取源代码" data-for="1-获取源代码">1. 获取源代码</a></li><li class="depth-1"><a href="#2-安装编译依赖" data-for="2-安装编译依赖">2. 安装编译依赖</a></li><li class="depth-0"><a href="#第二步配置与编译" data-for="第二步配置与编译">第二步:配置与编译</a></li><li class="depth-1"><a href="#1-解压源代码" data-for="1-解压源代码">1. 解压源代码</a></li><li class="depth-1"><a href="#2-configure-配置" data-for="2-configure-配置">2. Configure 配置</a></li><li class="depth-1"><a href="#3-make-编译" data-for="3-make-编译">3. Make 编译</a></li><li class="depth-0"><a href="#第三步安装与测试" data-for="第三步安装与测试">第三步:安装与测试</a></li><li class="depth-1"><a href="#1-安装" data-for="1-安装">1. 安装</a></li><li class="depth-1"><a href="#2-测试" data-for="2-测试">2. 测试</a></li><li class="depth-0"><a href="#第四步后续管理和清理" data-for="第四步后续管理和清理">第四步:后续管理和清理</a></li><li class="depth-1"><a href="#1-运行和管理" data-for="1-运行和管理">1. 运行和管理</a></li><li class="depth-1"><a href="#2-清理" data-for="2-清理">2. 清理</a></li><li class="depth-0"><a href="#第五步配置-systemd-服务" data-for="第五步配置-systemd-服务">第五步:配置 Systemd 服务</a></li><li class="depth-1"><a href="#1-创建-systemd-服务单元文件" data-for="1-创建-systemd-服务单元文件">1. 创建 Systemd 服务单元文件</a></li><li class="depth-1"><a href="#2-编辑服务单元文件" data-for="2-编辑服务单元文件">2. 编辑服务单元文件</a></li><li class="depth-1"><a href="#3-重新加载-systemd" data-for="3-重新加载-systemd">3. 重新加载 Systemd</a></li><li class="depth-1"><a href="#4-启动和启用服务" data-for="4-启动和启用服务">4. 启动和启用服务</a></li><li class="depth-1"><a href="#5-状态和日志" data-for="5-状态和日志">5. 状态和日志</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Journal","collapsed":true},{"path":"Journal/2024","collapsed":true},{"path":"Journal/2024/W33","collapsed":true},{"path":"Obsidian","collapsed":true},{"path":"Obsidian/Templates","collapsed":true},{"path":"Personal","collapsed":true},{"path":"Personal/Blog","collapsed":true},{"path":"Personal/Blog/2018","collapsed":true},{"path":"Personal/Blog/2020","collapsed":true},{"path":"Personal/Blog/202
|
|||
|
function toggleCallout() {
|
|||
|
const outerBlock = this.parentElement;
|
|||
|
outerBlock.classList.toggle(`is-collapsed`);
|
|||
|
const collapsed = outerBlock.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
|
|||
|
outerBlock.style.maxHeight = height + `px`;
|
|||
|
let current = outerBlock;
|
|||
|
let parent = outerBlock.parentElement;
|
|||
|
while (parent) {
|
|||
|
if (!parent.classList.contains(`callout`)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
const collapsed2 = parent.classList.contains(`is-collapsed`);
|
|||
|
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
|
|||
|
parent.style.maxHeight = height2 + `px`;
|
|||
|
current = parent;
|
|||
|
parent = parent.parentElement;
|
|||
|
}
|
|||
|
}
|
|||
|
function setupCallout() {
|
|||
|
const collapsible = document.getElementsByClassName(
|
|||
|
`callout is-collapsible`
|
|||
|
);
|
|||
|
for (const div of collapsible) {
|
|||
|
const title = div.firstElementChild;
|
|||
|
if (title) {
|
|||
|
title.removeEventListener(`click`, toggleCallout);
|
|||
|
title.addEventListener(`click`, toggleCallout);
|
|||
|
const collapsed = div.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? title.scrollHeight : div.scrollHeight;
|
|||
|
div.style.maxHeight = height + `px`;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
document.addEventListener(`nav`, setupCallout);
|
|||
|
window.addEventListener(`resize`, setupCallout);
|
|||
|
</script><script type="module">
|
|||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
});
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
});
|
|||
|
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|