{"id":772,"date":"2017-05-14T15:47:51","date_gmt":"2017-05-14T07:47:51","guid":{"rendered":"http:\/\/192.168.1.20\/?p=772"},"modified":"2017-10-13T11:27:59","modified_gmt":"2017-10-13T03:27:59","slug":"{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e9{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}98{47dd41ec6bdcbd9a214c783a3735392b82ea6d1d918378c5a53e0640702d813","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=772","title":{"rendered":"\u963f\u91cc\u4e91\u670d\u52a1\u5668 Ubuntu16.04\uff0832\u4f4d\uff09\u4e0bApache2.4.13\u90e8\u7f72Django1.11.1"},"content":{"rendered":"<h2>1. \u73af\u5883\u5b89\u88c5<\/h2>\n<p>1\u3001\u5b89\u88c5Django\u548cpython\uff08\u4e00\u822c\u5df2\u7ecf\u9ed8\u8ba4\u5b89\u88c5\u597d\u4e86python3.5\u548cpython2.7\uff09<\/p>\n<p>sudo apt-get install <a class=\"replace_word\" title=\"Python\u77e5\u8bc6\u5e93\" href=\"http:\/\/lib.csdn.net\/base\/python\" target=\"_blank\" rel=\"noopener noreferrer\">Python<\/a><\/p>\n<p>pip install django<\/p>\n<p>\u91cd\u8981\u63d2\u5165<\/p>\n<p>py2\u548cpy3\u5171\u5b58\u72b6\u6001\u4e0bpip\u5206\u522b\u5b89\u88c5django<\/p>\n<pre>apt-get install python3\r\napt-get install python3-pip\r\npip3 install xxx<\/pre>\n<p>2\u3001\u5b89\u88c5Apache<\/p>\n<p>sudo apt-get install apache2<\/p>\n<p>3) \u00a0 \u00a0\u5b89\u88c5WSGI\u6a21\u5757<\/p>\n<p>sudo apt-get install libapache2-mod-wsgi<\/p>\n<h2>2.\u5efa\u7acbDjango\u6587\u4ef6<\/h2>\n<p>&nbsp;<\/p>\n<p>1)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5c06Django\u5de5\u7a0b\u5efa\u7acb\u5728\/var\/www\/html\u4e0b<\/p>\n<p>django-admin.py startproject project\uff08django\u9879\u76ee\u540d\uff09<\/p>\n<p>django-admin.py startapp app\uff08app\u540d\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>2)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4fee\u6539\/Website\/Website\/settings.py\u6587\u4ef6<\/p>\n<p>\u5728ALLOWED_HOSTS\u91cc\u6dfb\u52a0\u2019cupspjk.com\u2019\uff0c\u2019127.0.0.1\u2019\uff0c\u2019192.168.10.115\u2019\uff08\u5982\u679c\u57df\u540d\u89e3\u6790\u65f6\u4e3b\u673a\u8bb0\u5f55\u6ca1\u6709www\u5c31\u4e0d\u5199www\uff09<img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/img.blog.csdn.net\/20161202203626724?watermark\/2\/text\/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\/gravity\/SouthEast\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>3.\u8ba9Apache\u627e\u5230Django\u6587\u4ef6<\/h2>\n<p>&nbsp;<\/p>\n<p>1)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\/etc\/apache2\/sites-available\/\u4e0b\u65b0\u5efacupspjk.conf\u914d\u7f6e\u6587\u4ef6<\/p>\n<p style=\"padding-left: 30px;\">&lt;VirtualHost *:80&gt;<br \/>\nServerName www.sxfu.ren<br \/>\n#ServerAlias other.com<br \/>\nServerAdmin alexdamocles@qq.com<\/p>\n<p style=\"padding-left: 30px;\">Alias \/media\/ \/var\/www\/html\/project\/media\/<br \/>\nAlias \/static\/ \/var\/www\/html\/project\/static\/<\/p>\n<p style=\"padding-left: 30px;\">&lt;Directory \/var\/www\/html\/project\/media&gt;<br \/>\nRequire all granted<br \/>\n&lt;\/Directory&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;Directory \/var\/www\/html\/project\/static&gt;<br \/>\nRequire all granted<br \/>\n&lt;\/Directory&gt;<\/p>\n<p style=\"padding-left: 30px;\">WSGIScriptAlias \/ \/var\/www\/html\/project\/project\/wsgi.py<br \/>\n# WSGIDaemonProcess ziqiangxuetang.com python-path=\/var\/www\/html\/project:\/home\/tu\/.virtualenvs\/blog\/lib\/python2.7\/site-packages<br \/>\n# WSGIProcessGroup ziqiangxuetang.com<\/p>\n<p style=\"padding-left: 30px;\">&lt;Directory \/var\/www\/html\/project\/project&gt;<br \/>\n&lt;Files wsgi.py&gt;<br \/>\nRequire all granted<br \/>\n&lt;\/Files&gt;<br \/>\n&lt;\/Directory&gt;<br \/>\n&lt;\/VirtualHost&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u4f60\u7684apache\u7248\u672c\u53f7\u662f 2.2.x\uff08\u67e5\u770b\u7248\u672c\u547d\u4ee4\uff1aapachectl\u00a0-v\uff09<\/p>\n<p>\u7528\u4e0b\u9762\u7684\u4ee3\u66ff \u00a0Require\u00a0all\u00a0granted<\/p>\n<pre class=\"brush:bash;toolbar:false\" style=\"padding-left: 30px;\">Order\u00a0deny,allow\r\nAllow\u00a0from\u00a0all<\/pre>\n<p><strong>\u5907\u6ce8\uff1a\u628a\u4e0a\u9762\u914d\u7f6e\u6587\u4ef6\u4e2d\u8fd9\u4e24\u53e5\u7684\u5907\u6ce8\u53bb\u6389\uff0c\u53ef\u4ee5\u4f7f\u7528 virtualenv \u6765\u90e8\u7f72\u7f51\u7ad9\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u53ea\u5199\u4e00\u4e2a \/home\/tu\/blog<\/strong><\/p>\n<pre class=\"brush:python;toolbar:false\">\u00a0\u00a0\u00a0\u00a0#\u00a0WSGIDaemonProcess\u00a0ziqiangxuetang.com\u00a0python-path=\/home\/tu\/blog:\/home\/tu\/.virtualenvs\/blog\/lib\/python2.7\/site-packages\r\n\u00a0\u00a0\u00a0\u00a0#\u00a0WSGIProcessGroup\u00a0ziqiangxuetang.com\r\n\r\n\r\n\r\n<\/pre>\n<h2>4. \u4fee\u6539wsgi.py\u6587\u4ef6<\/h2>\n<p><strong>\u6ce8\u610f\uff1a\u4e0a\u9762\u5982\u679c\u5199\u4e86 WSGIDaemonProcess \u7684\u8bdd\uff0c\u8fd9\u4e00\u6b65\u53ef\u4ee5\u8df3\u8fc7\uff0c\u5373\u53ef\u4ee5\u4e0d\u4fee\u6539 wsgi.py \u6587\u4ef6\u3002<\/strong><\/p>\n<p>\u4e0a\u9762\u7684\u914d\u7f6e\u4e2d\u5199\u7684 WSGIScriptAlias\u00a0\/\u00a0\/home\/tu\/blog\/blog\/wsgi.py<\/p>\n<p>\u5c31\u662f\u628aapache2\u548c\u4f60\u7684\u7f51\u7ad9project\u8054\u7cfb\u8d77\u6765\u4e86<\/p>\n<pre class=\"brush:python;toolbar:false\">import\u00a0os\r\nfrom\u00a0os.path\u00a0import\u00a0join,dirname,abspath\r\n\r\nPROJECT_DIR\u00a0=\u00a0dirname(dirname(abspath(__file__)))#3\r\nimport\u00a0sys\u00a0#\u00a04\r\nsys.path.insert(0,PROJECT_DIR)\u00a0#\u00a05\r\n\r\nos.environ[\"DJANGO_SETTINGS_MODULE\"]\u00a0=\u00a0\"blog.settings\"\u00a0#\u00a07\r\n\r\nfrom\u00a0django.core.wsgi\u00a0import\u00a0get_wsgi_application\r\napplication\u00a0=\u00a0get_wsgi_application()<\/pre>\n<p>\u7b2c 3\uff0c4\uff0c5 \u884c\u4e3a\u65b0\u52a0\u7684\u5185\u5bb9\uff0c\u4f5c\u7528\u662f\u8ba9\u811a\u672c\u627e\u5230django\u9879\u76ee\u7684\u4f4d\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u5728sitename.conf\u4e2d\u505a\uff0c\u7528WSGIPythonPath,\u60f3\u4e86\u89e3\u7684\u81ea\u884c\u641c\u7d22, \u7b2c 7 \u884c\u5982\u679c\u4e00\u53f0\u670d\u52a1\u5668\u6709\u591a\u4e2adjango project\u65f6\u4e00\u5b9a\u8981\u4fee\u6539\u6210\u4e0a\u9762\u90a3\u6837\uff0c\u5426\u5219\u8bbf\u95ee\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u7f51\u7ad9\u4e92\u76f8\u4e32\u7684\u60c5\u51b5\uff0c\u5373\u8bbf\u95eeA\u7f51\u7ad9\u5230\u4e86B\u7f51\u7ad9\uff0c\u4e00\u4f1a\u513f\u6b63\u5e38\uff0c\u4e00\u4f1a\u513f\u53c8\u4e0d\u6b63\u5e38\uff08\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528 mod_wsgi daemon \u6a21\u5f0f,<a title=\"Delegation To Daemon Process\" href=\"https:\/\/code.google.com\/p\/modwsgi\/wiki\/QuickConfigurationGuide#Delegation_To_Daemon_Process\" target=\"_blank\" rel=\"noopener noreferrer\">\u70b9\u51fb\u8fd9\u91cc\u67e5\u770b<\/a>\uff09<\/p>\n<p>&nbsp;<\/p>\n<h2>5. \u8bbe\u7f6e\u76ee\u5f55\u548c\u6587\u4ef6\u6743\u9650<\/h2>\n<p>\u4e00\u822c\u76ee\u5f55\u6743\u9650\u8bbe\u7f6e\u4e3a 755\uff0c\u6587\u4ef6\u6743\u9650\u8bbe\u7f6e\u4e3a 644<\/p>\n<p>\u5047\u5982\u9879\u76ee\u4f4d\u7f6e\u5728 \/home\/tu\/zqxt \uff08\u5728zqxt \u4e0b\u9762\u6709\u4e00\u4e2a manage.py\uff0czqxt \u662f\u9879\u76ee\u540d\u79f0\uff09<\/p>\n<pre class=\"brush:bash;toolbar:false\">cd\u00a0\/home\/tu\/\r\nsudo\u00a0chmod\u00a0-R\u00a0644\u00a0zqxt\r\nsudo\u00a0find\u00a0zqxt\u00a0-type\u00a0d\u00a0-exec\u00a0chmod\u00a0755\u00a0\\{\\}\u00a0\\;<\/pre>\n<p><strong>apache \u670d\u52a1\u5668\u8fd0\u884c\u7528\u6237\u53ef\u4ee5\u5728 \/etc\/apache2\/envvars \u6587\u4ef6\u91cc\u9762\u6539\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u503c\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u66f4\u6539\u6210\u81ea\u5df1\u7684\u5f53\u524d\u7528\u6237\uff0c\u8fd9\u6837\u7684\u8bdd\u6743\u9650\u95ee\u9898\u5c31\u7b80\u5355\u5f88\u591a\uff0c\u4f46\u5728\u670d\u52a1\u5668\u4e0a\u63a8\u8350\u6709 www-data \u7528\u6237\uff0c\u66f4\u5b89\u5168\u3002\u4ee5\u4e0b\u662f\u9ed8\u8ba4\u8bbe\u7f6e\uff1a<\/strong><\/p>\n<pre class=\"brush:plain;toolbar:false\">#\u00a0Since\u00a0there\u00a0is\u00a0no\u00a0sane\u00a0way\u00a0to\u00a0get\u00a0the\u00a0parsed\u00a0apache2\u00a0config\u00a0in\u00a0scripts,\u00a0some\r\n#\u00a0settings\u00a0are\u00a0defined\u00a0via\u00a0environment\u00a0variables\u00a0and\u00a0then\u00a0used\u00a0in\u00a0apache2ctl,\r\n#\u00a0\/etc\/init.d\/apache2,\u00a0\/etc\/logrotate.d\/apache2,\u00a0etc.\r\n\r\nexport\u00a0APACHE_RUN_USER=www-data\r\nexport\u00a0APACHE_RUN_GROUP=www-data\r\n\r\n<\/pre>\n<h3><strong>\u4e0a\u4f20\u6587\u4ef6\u5939\u6743\u9650<\/strong><\/h3>\n<p>media \u6587\u4ef6\u5939\u4e00\u822c\u7528\u6765\u5b58\u653e\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\uff0cstatic \u4e00\u822c\u7528\u6765\u653e\u81ea\u5df1\u7f51\u7ad9\u7684js\uff0ccss\uff0c\u56fe\u7247\u7b49\uff0c\u5728settings.py\u4e2d\u7684\u76f8\u5173\u8bbe\u7f6e<\/p>\n<p>STATIC_URL \u4e3a\u9759\u6001\u6587\u4ef6\u7684\u7f51\u5740 STATIC_ROOT \u4e3a\u9759\u6001\u6587\u4ef6\u7684\u6839\u76ee\u5f55\uff0c<\/p>\n<p>MEDIA_URL \u4e3a\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\u5939\u7684\u6839\u76ee\u5f55\uff0cMEDIA_URL\u4e3a\u5bf9\u5e94\u7684\u8bbf\u95ee\u7f51\u5740<\/p>\n<p>\u5728settings.py\u4e2d\u8bbe\u7f6e\uff1a<\/p>\n<pre class=\"brush:plain;toolbar:false;\">#\u00a0Static\u00a0files\u00a0(CSS,\u00a0JavaScript,\u00a0Images)\r\n#\u00a0https:\/\/docs.djangoproject.com\/en\/dev\/howto\/static-files\/\r\nSTATIC_URL\u00a0=\u00a0'\/static\/'\r\nSTATIC_ROOT\u00a0=\u00a0os.path.join(BASE_DIR,'static')\r\n\r\n#\u00a0upload\u00a0folder\r\nMEDIA_URL\u00a0=\u00a0'\/media\/'\r\nMEDIA_ROOT\u00a0=\u00a0os.path.join(BASE_DIR,'media')<\/pre>\n<p>\u5728 Linux \u670d\u52a1\u5668\u4e0a\uff0c\u7528\u6237\u4e0a\u4f20\u76ee\u5f55\u8fd8\u8981\u8bbe\u7f6e\u7ed9 www-data \u7528\u6237\u7684\u5199\u6743\u9650\uff0c\u4e0b\u9762\u7684\u65b9\u6cd5\u6bd4\u8f83\u597d\uff0c\u4e0d\u5f71\u54cd\u539f\u6765\u7684\u7528\u6237\u7684\u7f16\u8f91\u3002<\/p>\n<p>\u5047\u5982\u4e0a\u4f20\u76ee\u5f55\u4e3a zqxt\/media\/uploads \u6587\u4ef6\u5939,\u8fdb\u5165media\u6587\u4ef6\u5939\uff0c\u5c06 uploads \u7528\u6237\u7ec4\u6539\u4e3awww-data\uff0c\u5e76\u4e14\u8d4b\u4e88\u8be5\u7ec4<strong>\u5199\u6743\u9650<\/strong>:<\/p>\n<pre class=\"brush:bash;toolbar:false\">cd\u00a0media\/\u00a0#\u00a0\u8fdb\u5165media\u6587\u4ef6\u5939\r\nsudo\u00a0chgrp\u00a0-R\u00a0www-data\u00a0uploads\r\nsudo\u00a0chmod\u00a0-R\u00a0g+w\u00a0uploads<\/pre>\n<p><strong>\u5907\u6ce8\uff1a<\/strong>\u8fd9\u4e24\u6761\u547d\u4ee4\uff0c\u6bd4\u76f4\u63a5\u7528sudo chown -R www-data:www-data uploads \u597d\uff0c\u56e0\u4e3a\u4e0b\u9762\u7684\u547d\u4ee4\u4e0d\u5f71\u54cd\u6587\u4ef6\u539f\u6765\u6240\u5c5e\u7528\u6237\u7f16\u8f91\u6587\u4ef6\uff0cfedora\u7cfb\u7edf\u5e94\u8be5\u4e0d\u7528\u8bbe\u7f6e\u4e0a\u9762\u7684\u6743\u9650\uff0c\u4f46\u662f\u4e2a\u4eba\u5f3a\u70c8\u63a8\u8350\u7528ubuntu,\u9664\u975e\u4f60\u5bf9linux\u975e\u5e38\u719f\u6089\uff0c\u4f60\u81ea\u5df1\u9009\u62e9\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<strong>sqlite3\u6570\u636e\u5e93<\/strong>\uff0c\u8fd8\u4f1a\u63d0\u793a Attempt to write a readonly database,\u540c\u6837\u8981\u7ed9www-data\u5199\u6570\u636e\u5e93\u7684\u6743\u9650<\/p>\n<p>\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\u7684\u4e0a\u4e00\u7ea7\uff0c\u6bd4\u5982project\u76ee\u5f55\u4e3a \/home\/tu\/blog \u90a3\u5c31\u8fdb\u5165 \/home\/tu \u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff08\u548c\u4fee\u6539\u4e0a\u4f20\u6587\u4ef6\u5939\u7c7b\u4f3c\uff09<\/p>\n<pre class=\"brush:bash;toolbar:false\">sudo\u00a0chgrp\u00a0www-data\u00a0blog\r\nsudo\u00a0chmod\u00a0g+w\u00a0blog\r\nsudo\u00a0chgrp\u00a0www-data\u00a0blog\/db.sqlite3\u00a0\u00a0#\u00a0\u66f4\u6539\u4e3a\u4f60\u7684\u6570\u636e\u5e93\u540d\u79f0\r\nsudo\u00a0chmod\u00a0g+w\u00a0blog\/db.sqlite3<\/pre>\n<p><strong>\u5907\u6ce8\uff1a<\/strong>\u4e0a\u9762\u7684\u4e0d\u8981\u52a0 -R ,-R\u662f\u66f4\u6539\u5305\u62ec\u6240\u6709\u7684\u5b50\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff0c\u8fd9\u6837\u4e0d\u5b89\u5168\u3002\u4e2a\u4eba\u5efa\u8bae\u53ef\u4ee5\u4e13\u95e8\u5f04\u4e00\u4e2a\u6587\u4ef6\u5939,\u7528\u5b83\u6765\u653esqlite3\u6570\u636e\u5e93\uff0c\u7ed9\u8be5\u6587\u4ef6\u5939www-data\u5199\u6743\u9650\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u9879\u76ee\u7ed9\u5199\u6743\u9650\uff0c\u6709\u4e9b\u6587\u4ef6\u53ea\u8981\u8bfb\u7684\u6743\u9650\u5c31\u591f\u4e86\uff0c\u7ed9\u5199\u6743\u9650\u4f1a\u9020\u6210\u4e0d\u5b89\u5168\u3002<\/p>\n<h2>6. \u6fc0\u6d3b\u65b0\u7f51\u7ad9<\/h2>\n<pre class=\"brush:bash;toolbar:false\">sudo\u00a0a2ensite\u00a0sitename\u00a0\u6216\u00a0sudo\u00a0a2ensite\u00a0sitename.conf<\/pre>\n<p>\u5982\u679c\u987a\u5229\uff0c\u8fd9\u6837\u7f51\u7ad9\u5c31\u642d\u5efa\u6210\u529f\uff0c\u8bbf\u95ee\u4f60\u7684\u7f51\u5740\u8bd5\u8bd5\u770b\uff0c\u5982\u679c\u51fa\u73b0\u95ee\u9898\u5c31\u63a5\u7740\u770b\u4e0b\u9762\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u4e9b\u9519\u8bef<\/p>\n<p>1\u3001500\u9519\u8bef<\/p>\n<p>django\u4e00\u76f4\u62a5500\u9519\u8bef\uff0c\u4efb\u4f55\u7f51\u9875\u90fd\u662f<\/p>\n<p>\u67e5\u770bapache2\u7684\u9519\u8bef\u65e5\u5fd7\u663e\u793a<\/p>\n<p>Target WSGI script \u00a0 \u00a0 \u00a0x\/x\/x\/xx\/ \u00a0 \u00a0cannot be loaded as Python module.<\/p>\n<p>WSGI\u6a21\u5757\u4e0d\u80fd\u52a0\u8f7d\u5230python\u6a21\u5757\u4e4b\u4e2d<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u73af\u5883\u5b89\u88c5 1\u3001\u5b89\u88c5Dja&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-jishu"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/772"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=772"}],"version-history":[{"count":0,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/772\/revisions"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}