{"id":5177,"date":"2025-11-06T22:52:12","date_gmt":"2025-11-06T14:52:12","guid":{"rendered":"https:\/\/qaqaq.top\/?p=5177"},"modified":"2025-11-06T22:55:08","modified_gmt":"2025-11-06T14:55:08","slug":"ubuntu%e5%ae%89%e8%a3%85nginx","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=5177","title":{"rendered":"Ubuntu\u5b89\u88c5Nginx"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u7f16\u8bd1\u4f9d\u8d56<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install -y \\\n    build-essential \\\n    libpcre3-dev \\\n    zlib1g-dev \\\n    libssl-dev \\\n    libxml2-dev \\\n    libxslt1-dev \\\n    libgd-dev \\\n    libperl-dev<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u8fd9\u4e9b\u5e93\u7528\u4e8e\u652f\u6301\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pcre<\/code>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff08rewrite \u5fc5\u9700\uff09<\/li>\n\n\n\n<li><code>zlib<\/code>\uff1agzip \u538b\u7f29<\/li>\n\n\n\n<li><code>openssl<\/code>\uff1aHTTPS\/SSL \u652f\u6301<\/li>\n\n\n\n<li>\u5176\u4ed6\uff1a\u53ef\u9009\u6a21\u5757\uff08\u5982 image filter\u3001perl \u6a21\u5757\u7b49\uff09<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u521b\u5efa nginx \u7528\u6237\uff08\u975e root \u8fd0\u884c\uff0c\u66f4\u5b89\u5168\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo useradd -r -s \/sbin\/nologin -d \/var\/cache\/nginx nginx<\/code><\/pre>\n\n\n\n<p><code>-r<\/code>\uff1a\u7cfb\u7edf\u7528\u6237\uff1b<code>-s \/sbin\/nologin<\/code>\uff1a\u7981\u6b62\u767b\u5f55\uff1b<code>-d<\/code>\uff1a\u6307\u5b9a home \u76ee\u5f55\uff08\u65e5\u5fd7\/\u7f13\u5b58\u7528\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4e0b\u8f7d\u5e76\u89e3\u538b Nginx \u6e90\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8fdb\u5165\u4e34\u65f6\u76ee\u5f55\ncd \/tmp\n\n# \u4e0b\u8f7d\u6700\u65b0\u7a33\u5b9a\u7248\uff08\u8bf7\u5230 https:\/\/nginx.org\/en\/download.html \u67e5\u770b\u6700\u65b0\u7248\u672c\uff09\nNGINX_VERSION=1.26.1\nwget https:\/\/nginx.org\/download\/nginx-${NGINX_VERSION}.tar.gz\n\n# \u89e3\u538b\ntar -zxvf nginx-${NGINX_VERSION}.tar.gz\ncd nginx-${NGINX_VERSION}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u914d\u7f6e\u7f16\u8bd1\u9009\u9879\uff08\u5173\u952e\u6b65\u9aa4\uff09<\/h2>\n\n\n\n<p>\u8fd0\u884c <code>.\/configure<\/code> \u8bbe\u7f6e\u5b89\u88c5\u8def\u5f84\u548c\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/configure \\\n  --prefix=\/usr\/local\/nginx \\\n  --sbin-path=\/usr\/sbin\/nginx \\\n  --conf-path=\/etc\/nginx\/nginx.conf \\\n  --error-log-path=\/var\/log\/nginx\/error.log \\\n  --http-log-path=\/var\/log\/nginx\/access.log \\\n  --pid-path=\/run\/nginx.pid \\\n  --lock-path=\/run\/nginx.lock \\\n  --user=nginx \\\n  --group=nginx \\\n  --with-http_ssl_module \\\n  --with-http_v2_module \\\n  --with-http_realip_module \\\n  --with-http_gzip_static_module \\\n  --with-http_stub_status_module \\\n  --with-http_sub_module \\\n  --with-http_secure_link_module \\\n  --with-pcre \\\n  --with-file-aio \\\n  --with-threads \\\n  --with-stream \\\n  --with-stream_ssl_module \\\n  --with-stream_realip_module<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d \u53c2\u6570\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>--prefix<\/code><\/td><td>\u5b89\u88c5\u6839\u76ee\u5f55<\/td><\/tr><tr><td><code>--sbin-path<\/code><\/td><td><code>nginx<\/code> \u547d\u4ee4\u8def\u5f84\uff08\u65b9\u4fbf\u76f4\u63a5\u6267\u884c\uff09<\/td><\/tr><tr><td><code>--conf-path<\/code><\/td><td>\u4e3b\u914d\u7f6e\u6587\u4ef6\u8def\u5f84<\/td><\/tr><tr><td><code>--user\/--group<\/code><\/td><td>\u5de5\u4f5c\u8fdb\u7a0b\u8fd0\u884c\u7528\u6237<\/td><\/tr><tr><td><code>--with-http_ssl_module<\/code><\/td><td>\u542f\u7528 HTTPS\uff08\u5fc5\u9700\uff09<\/td><\/tr><tr><td><code>--with-http_v2_module<\/code><\/td><td>\u652f\u6301 HTTP\/2<\/td><\/tr><tr><td><code>--with-http_stub_status_module<\/code><\/td><td>\u5f00\u542f\u72b6\u6001\u9875\uff08<code>\/nginx_status<\/code>\uff09<\/td><\/tr><tr><td><code>--with-stream<\/code><\/td><td>\u56db\u5c42 TCP\/UDP \u8d1f\u8f7d\u5747\u8861<\/td><\/tr><tr><td><code>--with-pcre<\/code><\/td><td>\u4f7f\u7528\u7cfb\u7edf PCRE \u5e93\uff08\u7528\u4e8e rewrite\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 \u5982\u9700\u6dfb\u52a0\u7b2c\u4e09\u65b9\u6a21\u5757\uff08\u5982 <code>ngx_cache_purge<\/code>\uff09\uff0c\u5728\u6b64\u5904\u52a0 <code>--add-module=\/path\/to\/module<\/code><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u7f16\u8bd1\u5e76\u5b89\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7f16\u8bd1\uff08\u591a\u6838\u52a0\u901f\uff09\nmake -j$(nproc)\n\n# \u5b89\u88c5\uff08\u590d\u5236\u6587\u4ef6\u5230\u6307\u5b9a\u76ee\u5f55\uff09\nsudo make install<\/code><\/pre>\n\n\n\n<p>\u23f1\ufe0f \u7f16\u8bd1\u65f6\u95f4\uff1a\u901a\u5e38 1~5 \u5206\u949f\uff0c\u53d6\u51b3\u4e8e\u670d\u52a1\u5668\u6027\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u9a8c\u8bc1\u5b89\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># \u68c0\u67e5\u7248\u672c\u548c\u6a21\u5757\nnginx -V\n\n# \u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6\u8bed\u6cd5\nsudo nginx -t\n\n# \u8f93\u51fa\u5e94\u7c7b\u4f3c\uff1a\n# nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\n# nginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u914d\u7f6e systemd \u670d\u52a1\uff08\u5f00\u673a\u81ea\u542f\uff09<\/h2>\n\n\n\n<p>\u521b\u5efa\u670d\u52a1\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tee \/etc\/systemd\/system\/nginx.service &lt;&lt;'EOF'\n&#91;Unit]\nDescription=The NGINX HTTP and reverse proxy server\nAfter=network.target remote-fs.target nss-lookup.target\n\n&#91;Service]\nType=forking\nPIDFile=\/run\/nginx.pid\nExecStartPre=\/usr\/sbin\/nginx -t\nExecStart=\/usr\/sbin\/nginx\nExecReload=\/bin\/kill -s HUP $MAINPID\nKillSignal=SIGQUIT\nTimeoutStopSec=5\nKillMode=process\nPrivateTmp=true\n\n&#91;Install]\nWantedBy=multi-user.target\nEOF<\/code><\/pre>\n\n\n\n<p>\u91cd\u8f7d systemd \u5e76\u542f\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo systemctl start nginx\nsudo systemctl enable nginx\nsudo systemctl status nginx<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u9632\u706b\u5899\u653e\u884c\uff08\u5982\u542f\u7528\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 'Nginx Full'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u5e38\u7528\u76ee\u5f55\u786e\u8ba4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7528\u9014<\/th><th>\u8def\u5f84<\/th><\/tr><\/thead><tbody><tr><td>\u4e3b\u914d\u7f6e\u6587\u4ef6<\/td><td><code>\/etc\/nginx\/nginx.conf<\/code><\/td><\/tr><tr><td>\u7ad9\u70b9\u914d\u7f6e\u76ee\u5f55<\/td><td><code>\/etc\/nginx\/conf.d\/<\/code>\uff08\u9700\u5728\u4e3b\u914d\u7f6e\u4e2d include\uff09<\/td><\/tr><tr><td>\u9ed8\u8ba4\u7f51\u9875<\/td><td><code>\/usr\/local\/nginx\/html\/<\/code><\/td><\/tr><tr><td>\u65e5\u5fd7<\/td><td><code>\/var\/log\/nginx\/<\/code><\/td><\/tr><tr><td>PID \u6587\u4ef6<\/td><td><code>\/run\/nginx.pid<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u5efa\u8bae\u5728 <code>nginx.conf<\/code> \u4e2d\u6dfb\u52a0\uff1a<code><br><\/code><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>http {\n    include \/etc\/nginx\/conf.d\/*.conf;\n    ...\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\uff08\u53ef\u9009\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/tmp\nrm -rf nginx-${NGINX_VERSION}*<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u8bbf\u95ee\u6d4b\u8bd5<\/h2>\n\n\n\n<p>\u6d4f\u89c8\u5668\u6253\u5f00\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;\u4f60\u7684\u670d\u52a1\u5668IP<\/code><\/pre>\n\n\n\n<p>\u5e94\u770b\u5230 <strong>\u201cWelcome to nginx!\u201d<\/strong> \u9875\u9762\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c 1. \u5b89\u88c5\u7f16\u8bd1\u4f9d\u8d56 2. \u521b\u5efa nginx \u7528\u6237\uff08\u975e root \u8fd0\u884c\uff0c\u66f4\u5b89\u5168\uff09 -r\uff1a\u7cfb\u7edf\u7528\u6237\uff1b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[129],"tags":[52],"class_list":["post-5177","post","type-post","status-publish","format-standard","hentry","category-nginx-","tag-nginx"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/5177"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5177"}],"version-history":[{"count":3,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/5177\/revisions"}],"predecessor-version":[{"id":5180,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/5177\/revisions\/5180"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}