{"id":12197,"date":"2025-04-28T15:53:48","date_gmt":"2025-04-28T22:53:48","guid":{"rendered":"https:\/\/www.xh86.me\/?p=12197"},"modified":"2025-04-28T18:28:07","modified_gmt":"2025-04-29T01:28:07","slug":"ubuntu-24-04%e5%ae%89%e8%a3%85netbox","status":"publish","type":"post","link":"https:\/\/www.xh86.me\/?p=12197","title":{"rendered":"Ubuntu 24.04\u5b89\u88c5NetBox"},"content":{"rendered":"<p>#!\/bin\/bash<\/p>\n<p>set -e<\/p>\n<p># 1. \u66f4\u65b0\u7cfb\u7edf\u5e76\u5b89\u88c5\u4f9d\u8d56<br \/>\nsudo apt update &amp;&amp; sudo apt upgrade -y<br \/>\nsudo apt install -y python3 python3-pip python3-venv python3-dev \\<br \/>\nbuild-essential libpq-dev libffi-dev libssl-dev \\<br \/>\nredis-server postgresql nginx git certbot python3-certbot-nginx ufw<\/p>\n<p># 2. \u8bbe\u7f6e\u65f6\u533a\u4e3a\u6d1b\u6749\u77f6<br \/>\nsudo timedatectl set-timezone America\/Los_Angeles<\/p>\n<p># 3. \u914d\u7f6e\u9632\u706b\u5899\uff0c\u5141\u8bb822\u300180\u3001443\u7aef\u53e3<br \/>\nsudo ufw allow 22\/tcp<br \/>\nsudo ufw allow 80\/tcp<br \/>\nsudo ufw allow 443\/tcp<br \/>\nsudo ufw &#8211;force enable<\/p>\n<p># 4. \u521b\u5efa NetBox \u7cfb\u7edf\u7528\u6237<br \/>\nsudo adduser &#8211;system &#8211;group netbox<\/p>\n<p># 5. \u914d\u7f6e PostgreSQL \u6570\u636e\u5e93<br \/>\nsudo systemctl enable &#8211;now postgresql<br \/>\nsudo -u postgres psql &lt;&lt;EOF<br \/>\nCREATE DATABASE netbox;<br \/>\nCREATE USER netbox WITH PASSWORD &#8216;netbox&#8217;;<br \/>\nALTER ROLE netbox SET client_encoding TO &#8216;utf8&#8217;;<br \/>\nALTER ROLE netbox SET default_transaction_isolation TO &#8216;read committed&#8217;;<br \/>\nALTER ROLE netbox SET timezone TO &#8216;UTC&#8217;;<br \/>\nGRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;<br \/>\nGRANT ALL ON SCHEMA public TO netbox;<br \/>\nALTER DATABASE netbox OWNER TO netbox;<br \/>\nEOF<\/p>\n<p># 6. \u514b\u9686 NetBox \u4ed3\u5e93<br \/>\ncd \/opt<br \/>\nsudo git clone -b v4.2.8 https:\/\/github.com\/netbox-community\/netbox.git<br \/>\nsudo chown -R netbox:netbox \/opt\/netbox<\/p>\n<p># 7. \u521b\u5efa\u5e76\u6fc0\u6d3b Python \u865a\u62df\u73af\u5883<br \/>\ncd \/opt\/netbox<br \/>\nsudo -u netbox python3 -m venv \/opt\/netbox\/venv<br \/>\nsudo -u netbox \/opt\/netbox\/venv\/bin\/pip install &#8211;upgrade pip<br \/>\nsudo -u netbox \/opt\/netbox\/venv\/bin\/pip install -r requirements.txt<\/p>\n<p># 8. \u914d\u7f6e NetBox<br \/>\nsudo cp \/opt\/netbox\/netbox\/netbox\/configuration_example.py \/opt\/netbox\/netbox\/netbox\/configuration.py<\/p>\n<p>SECRET_KEY=$(python3 \/opt\/netbox\/netbox\/generate_secret_key.py)<br \/>\nsudo sed -i &#8220;s|^SECRET_KEY = .*|SECRET_KEY = &#8216;$SECRET_KEY&#8217;|&#8221; \/opt\/netbox\/netbox\/netbox\/configuration.py<br \/>\nsudo sed -i &#8220;s\/ALLOWED_HOSTS = \\[\\]\/ALLOWED_HOSTS = [&#8216;netbox.digitalsystem.net&#8217;]\/&#8221; \/opt\/netbox\/netbox\/netbox\/configuration.py<\/p>\n<p>sudo tee -a \/opt\/netbox\/netbox\/netbox\/configuration.py &gt; \/dev\/null &lt;&lt;EOF<\/p>\n<p>DATABASE = {<br \/>\n&#8216;NAME&#8217;: &#8216;netbox&#8217;,<br \/>\n&#8216;USER&#8217;: &#8216;netbox&#8217;,<br \/>\n&#8216;PASSWORD&#8217;: &#8216;netbox&#8217;,<br \/>\n&#8216;HOST&#8217;: &#8216;localhost&#8217;,<br \/>\n&#8216;PORT&#8217;: &#8216;5432&#8217;,<br \/>\n}<br \/>\nEOF<\/p>\n<p># 9. \u521d\u59cb\u5316\u6570\u636e\u5e93<br \/>\nsudo -u netbox \/opt\/netbox\/venv\/bin\/python \/opt\/netbox\/netbox\/manage.py migrate<\/p>\n<p># 10. \u6536\u96c6\u9759\u6001\u6587\u4ef6<br \/>\nsudo -u netbox \/opt\/netbox\/venv\/bin\/python \/opt\/netbox\/netbox\/manage.py collectstatic &#8211;no-input<\/p>\n<p># 11. \u5148\u914d\u7f6e\u4e34\u65f6 HTTP nginx<br \/>\nsudo tee \/etc\/nginx\/sites-available\/netbox &gt; \/dev\/null &lt;&lt;EOF<br \/>\nserver {<br \/>\nlisten 80;<br \/>\nserver_name netbox.digitalsystem.net;<\/p>\n<p>location \/static\/ {<br \/>\nalias \/opt\/netbox\/netbox\/static\/;<br \/>\n}<\/p>\n<p>location \/ {<br \/>\nproxy_pass http:\/\/127.0.0.1:8001;<br \/>\nproxy_set_header Host \\$host;<br \/>\nproxy_set_header X-Real-IP \\$remote_addr;<br \/>\n}<br \/>\n}<br \/>\nEOF<\/p>\n<p>sudo ln -sf \/etc\/nginx\/sites-available\/netbox \/etc\/nginx\/sites-enabled\/netbox<br \/>\nsudo rm -f \/etc\/nginx\/sites-enabled\/default<br \/>\nsudo systemctl restart nginx<\/p>\n<p># 12. \u7533\u8bf7 Let&#8217;s Encrypt \u8bc1\u4e66<br \/>\nsudo certbot &#8211;nginx -d netbox.digitalsystem.net &#8211;non-interactive &#8211;agree-tos -m xxx@xxx.com &#8211;redirect<\/p>\n<p># 13. \u66f4\u65b0 nginx \u914d\u7f6e\uff0c\u5f3a\u5236 HTTPS<br \/>\nsudo tee \/etc\/nginx\/sites-available\/netbox &gt; \/dev\/null &lt;&lt;EOF<br \/>\nserver {<br \/>\nlisten 80;<br \/>\nserver_name netbox.xxxxxx.net;<br \/>\nreturn 301 https:\/\/\\$host\\$request_uri;<br \/>\n}<\/p>\n<p>server {<br \/>\nlisten 443 ssl;<br \/>\nserver_name netbox.xxxxxxxx.net;<\/p>\n<p>ssl_certificate \/etc\/letsencrypt\/live\/netbox.xxxxxxx.net\/fullchain.pem;<br \/>\nssl_certificate_key \/etc\/letsencrypt\/live\/netbox.xxxxxx.net\/privkey.pem;<\/p>\n<p>location \/static\/ {<br \/>\nalias \/opt\/netbox\/netbox\/static\/;<br \/>\n}<\/p>\n<p>location \/ {<br \/>\nproxy_pass http:\/\/127.0.0.1:8001;<br \/>\nproxy_set_header Host \\$host;<br \/>\nproxy_set_header X-Real-IP \\$remote_addr;<br \/>\n}<br \/>\n}<br \/>\nEOF<\/p>\n<p>sudo systemctl reload nginx<\/p>\n<p># 14. \u914d\u7f6e Gunicorn systemd \u670d\u52a1<br \/>\nsudo tee \/etc\/systemd\/system\/netbox.service &gt; \/dev\/null &lt;&lt;EOF<br \/>\n[Unit]<br \/>\nDescription=NetBox WSGI Service<br \/>\nAfter=network.target<\/p>\n<p>[Service]<br \/>\nType=simple<br \/>\nUser=netbox<br \/>\nGroup=netbox<br \/>\nWorkingDirectory=\/opt\/netbox\/netbox<br \/>\nExecStart=\/opt\/netbox\/venv\/bin\/gunicorn &#8211;workers 3 &#8211;bind 127.0.0.1:8001 netbox.wsgi<br \/>\nRestart=always<\/p>\n<p>[Install]<br \/>\nWantedBy=multi-user.target<br \/>\nEOF<\/p>\n<p># 15. \u542f\u52a8\u5e76\u542f\u7528 NetBox \u670d\u52a1<br \/>\nsudo systemctl daemon-reload<br \/>\nsudo systemctl enable netbox<br \/>\nsudo systemctl restart netbox<\/p>\n<p># 16. \u8bbe\u7f6e\u6bcf\u65e5\u81ea\u52a8\u5907\u4efd\u6570\u636e\u5e93\uff0c\u53ea\u4fdd\u7559\u6700\u8fd130\u5929\u5907\u4efd<br \/>\nsudo tee \/etc\/cron.daily\/netbox_backup &gt; \/dev\/null &lt;&lt;EOF<br \/>\n#!\/bin\/bash<br \/>\nBACKUP_DIR=&#8221;\/opt\/netbox\/backups&#8221;<br \/>\nmkdir -p \\$BACKUP_DIR<br \/>\nPGPASSWORD=&#8221;netbox&#8221; pg_dump -U netbox -h localhost -p 5432 netbox &gt; \\$BACKUP_DIR\/netbox_backup_\\$(date +\\%F).sql<br \/>\nfind \\$BACKUP_DIR -type f -mtime +30 -delete<br \/>\nEOF<br \/>\nsudo chmod +x \/etc\/cron.daily\/netbox_backup<\/p>\n<p># 17. \u8bbe\u7f6e\u6bcf\u65e5\u81ea\u52a8\u7eed\u671f SSL \u8bc1\u4e66\u5e76\u91cd\u8f7d nginx<br \/>\nsudo tee \/etc\/cron.daily\/certbot_renew &gt; \/dev\/null &lt;&lt;EOF<br \/>\n#!\/bin\/bash<br \/>\ncertbot renew &#8211;quiet &#8211;deploy-hook &#8220;systemctl reload nginx&#8221;<br \/>\nEOF<br \/>\nsudo chmod +x \/etc\/cron.daily\/certbot_renew<\/p>\n<p>echo &#8220;NetBox \u5b89\u88c5\u5b8c\u6210\uff01\u8bbf\u95ee https:\/\/netbox.xxxxx.net \uff0822\u548c443\u7aef\u53e3\u5f00\u653e\uff0c\u81ea\u52a8\u7ef4\u62a4\uff0c\u6d1b\u6749\u77f6\u65f6\u95f4\uff09\u3002&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728\u767b\u5f55 NetBox \u65f6\u9047\u5230\u7684 &#8220;Forbidden (403) CSRF verification failed&#8221; \u9519\u8bef\uff0c\u901a\u5e38\u662f\u7531\u4e8e Django \u7684 CSRF \u9632\u62a4\u673a\u5236\u672a\u80fd\u6b63\u786e\u8bc6\u522b\u8bf7\u6c42\u7684\u6765\u6e90\u3002\u8fd9\u5728\u542f\u7528 HTTPS \u6216\u4f7f\u7528\u53cd\u5411\u4ee3\u7406\uff08\u5982 Nginx\uff09\u65f6\u5c24\u4e3a\u5e38\u89c1\u3002\u6839\u636e NetBox \u7684\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u8ba8\u8bba\uff0c\u4ee5\u4e0b\u662f\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u6b65\u9aa4\uff1a (<a href=\"https:\/\/stackoverflow.com\/questions\/70285834\/forbidden-403-csrf-verification-failed-request-aborted-reason-given-for-fail?utm_source=chatgpt.com\">Forbidden (403) CSRF verification failed. Request aborted. Reason &#8230;<\/a>)<\/p>\n<hr \/>\n<h3>\u89e3\u51b3\u65b9\u6848\uff1a\u914d\u7f6e CSRF_TRUSTED_ORIGINS<\/h3>\n<ol>\n<li><strong>\u7f16\u8f91 NetBox \u914d\u7f6e\u6587\u4ef6<\/strong>\n<p>\u6253\u5f00 NetBox \u7684\u914d\u7f6e\u6587\u4ef6 <code>configuration.py<\/code>\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo nano \/opt\/netbox\/netbox\/netbox\/configuration.py\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u6dfb\u52a0 CSRF_TRUSTED_ORIGINS \u8bbe\u7f6e<\/strong>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff0c\u786e\u4fdd\u4f7f\u7528\u60a8\u7684\u5b9e\u9645\u57df\u540d\u66ff\u6362\u793a\u4f8b\u4e2d\u7684\u57df\u540d\uff1a<\/p>\n<pre><code class=\"language-python\">CSRF_TRUSTED_ORIGINS = ['https:\/\/netbox.xxxxxxxx.net']\r\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cDjango 4.0 \u53ca\u4ee5\u4e0a\u7248\u672c\u8981\u6c42\u5728 <code>CSRF_TRUSTED_ORIGINS<\/code> \u4e2d\u5305\u542b\u534f\u8bae\uff08\u5982 <code>http:\/\/<\/code> \u6216 <code>https:\/\/<\/code>\uff09 \u3002<\/li>\n<li><strong>\u91cd\u542f NetBox \u670d\u52a1<\/strong>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u91cd\u542f NetBox \u670d\u52a1\u4ee5\u5e94\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl restart netbox\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u6e05\u9664\u6d4f\u89c8\u5668\u7f13\u5b58\u548c Cookie<\/strong>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u6e05\u9664\u4e0e NetBox \u76f8\u5173\u7684\u7f13\u5b58\u548c Cookie\uff0c\u7136\u540e\u91cd\u65b0\u52a0\u8f7d\u767b\u5f55\u9875\u9762\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\ud83d\udd0d \u5176\u4ed6\u5efa\u8bae<\/h3>\n<ul>\n<li><strong>\u786e\u4fdd ALLOWED_HOSTS \u8bbe\u7f6e\u6b63\u786e<\/strong>\n<p>\u5728 <code>configuration.py<\/code> \u4e2d\uff0c\u786e\u4fdd <code>ALLOWED_HOSTS<\/code> \u5305\u542b\u60a8\u7684\u57df\u540d\uff1a (<a href=\"https:\/\/github.com\/netbox-community\/netbox\/discussions\/9043?utm_source=chatgpt.com\">CSRF Token \u00b7 netbox-community netbox \u00b7 Discussion #9043 &#8211; GitHub<\/a>)<\/p>\n<pre><code class=\"language-python\">ALLOWED_HOSTS = ['netbox.xxxxxx.net']\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u68c0\u67e5 Nginx \u914d\u7f6e<\/strong>\n<p>\u786e\u4fdd Nginx \u914d\u7f6e\u4e2d\u6b63\u786e\u8bbe\u7f6e\u4e86\u4ee3\u7406\u5934\u90e8\uff0c\u4ee5\u4fbf Django \u80fd\u6b63\u786e\u8bc6\u522b\u8bf7\u6c42\u7684\u6765\u6e90\uff1a<\/p>\n<pre><code class=\"language-nginx\">proxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-Proto $scheme;\r\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u8bbe\u7f6e\u6709\u52a9\u4e8e Django \u6b63\u786e\u5904\u7406 CSRF \u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8981\u5728 NetBox \u4e2d\u521b\u5efa\u7ba1\u7406\u5458\u8d26\u6237\uff1a<\/p>\n<hr \/>\n<h3>\u521b\u5efa\u7ba1\u7406\u5458\u8d26\u6237\uff08\u8d85\u7ea7\u7528\u6237\uff09<\/h3>\n<ol>\n<li><strong>\u8fdb\u5165\u865a\u62df\u73af\u5883<\/strong>\n<p>NetBox \u4f7f\u7528 Python \u865a\u62df\u73af\u5883\u8fd0\u884c\u3002\u9996\u5148\uff0c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<pre><code class=\"language-bash\">source \/opt\/netbox\/venv\/bin\/activate\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u8fdb\u5165 NetBox \u9879\u76ee\u76ee\u5f55<\/strong>\n<p>\u5207\u6362\u5230 NetBox \u7684\u7ba1\u7406\u811a\u672c\u6240\u5728\u76ee\u5f55\uff1a<\/p>\n<pre><code class=\"language-bash\">cd \/opt\/netbox\/netbox\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u521b\u5efa\u8d85\u7ea7\u7528\u6237<\/strong>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u6309\u7167\u63d0\u793a\u8f93\u5165\u7528\u6237\u540d\u3001\u90ae\u7bb1\u548c\u5bc6\u7801\uff1a<\/p>\n<pre><code class=\"language-bash\">python3 manage.py createsuperuser\r\n<\/code><\/pre>\n<p>\u793a\u4f8b\u8f93\u51fa\uff1a<\/p>\n<pre><code>Username (leave blank to use 'root'): admin\r\nEmail address: admin@example.com\r\nPassword:\r\nPassword (again):\r\nSuperuser created successfully.\r\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff1a\u7528\u6237\u540d\u4e0d\u80fd\u4e3a <code>root<\/code>\uff0c\u5efa\u8bae\u4f7f\u7528\u5176\u4ed6\u540d\u79f0\uff0c\u5982 <code>admin<\/code>\u3002<\/li>\n<li><strong>\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong>\n<p>\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<pre><code class=\"language-bash\">deactivate\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash set -e # 1. \u66f4\u65b0\u7cfb\u7edf\u5e76\u5b89\u88c5\u4f9d\u8d56 sudo apt update &amp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,3],"tags":[],"class_list":["post-12197","post","type-post","status-publish","format-standard","hentry","category-linux","category-system"],"_links":{"self":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12197"}],"version-history":[{"count":3,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12197\/revisions"}],"predecessor-version":[{"id":12200,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12197\/revisions\/12200"}],"wp:attachment":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}