博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fedora 开发_在Fedora上开发和部署Cookiecutter-Django
阅读量:2519 次
发布时间:2019-05-11

本文共 13463 字,大约阅读时间需要 44 分钟。

fedora 开发

Last we set up a Django Project using Cookiecutter, managed the application environment via Docker, and then deployed the app to Digital Ocean. In this tutorial, we’ll shift away from Docker and detail a development to deployment workflow of a Cookiecutter-Django Project on Fedora 23.

上我们使用Cookiecutter设置Django项目,通过Docker管理应用程序环境,然后将应用程序部署到Digital Ocean。 在本教程中,我们将从Docker转向,并详细介绍Fedora 23上Cookiecutter-Django项目的开发到部署工作流。

cookiecutter django fedora

发展历程 (Development)

Install globally and then generate a bootstrapped Django project:

全局安装 ,然后生成一个引导的Django项目:

1122

This command runs cookiecutter with the repo, allowing us to enter project-specific details. (We named the project and the repo django_cookiecutter_fedora.)

此命令使用库运行cookiecutter,使我们可以输入特定于项目的详细信息。 (我们将项目和仓库命名为django_cookiecutter_fedora。)

NOTE: Check out the section from the previous post for more information on this command as well as the generated project structure.

注意 :请查看上一篇文章的“ 部分,以获取有关此命令以及生成的项目结构的更多信息。

Before we can start our Django Project, we are still left with few steps…

在我们开始Django Project之前,我们还有几个步骤……

数据库设置 (Database Setup)

First, we need to set up Postgres since cookiecutter-django uses it as its default database (see for more info). Follow the steps to set up a Postgres database server, from any on the Internet or just scroll down to the for setting it up on .

首先,我们需要设置Postgres,因为cookiecutter-django将其用作默认数据库(有关更多信息,请参见 )。 请按照以下步骤从Internet上的任何可用设置Postgres数据库服务器,或者向下滚动至以在上进行设置。

NOTE: If you’re on a Mac, check out .

注意 :如果您使用的是Mac,请查看 。

Once the Postgres server is running, create a new database from that shares the same name as your project name:

Postgres服务器运行后,从创建一个新数据库,该数据库与您的项目名称共享相同的名称:

1122

NOTE: There may be some variation in the above command for creating a database based upon your version of Postgres. You can check for the correct command in Postgres’ latest documentation found .

注意 :根据您使用的Postgres版本,上述命令可能会有所不同,用于创建数据库。 您可以在找到的Postgres最新文档中检查正确的命令。

依赖设置 (Dependency Setup)

Next, in order to get your Django Project in a state ready for development, navigate to the root directory, create/activate a virtual environment, and then install the dependencies:

接下来,为了使您的Django项目处于准备开发的状态,请导航至根目录,创建/激活虚拟环境,然后安装依赖项:

11223344

完整性检查 (Sanity Check)

Apply the migrations and then run the local development server:

应用迁移,然后运行本地开发服务器:

112233

Ensure all is well by navigating to in your browser to view the Project quick start page. Once done, kill the development server, initialize a new Git repo, commit, and PUSH to Github.

在浏览器中导航到以查看Project快速入门页,以确保一切正常。 完成后,终止开发服务器,初始化一个新的Git存储库,提交并推送到Github。

部署方式 (Deployment)

With the Project setup and running locally, we can now move on to deployment where we will utilize the following tools:

通过Project设置并在本地运行,我们现在可以继续进行部署,在这里我们将使用以下工具:

  • Postgres
  • Postgres

Fedora 23设置 (Fedora 23 Setup)

Set up a quick droplet, making sure to use a Fedora 23 image. For help, follow tutorial. Make sure you for secure login.

设置一个快速的墨滴,确保使用Fedora 23图像。 要获得帮助,请遵循教程。 确保您用于安全登录 。

Now let’s our server. SSH into the server as root, and then fire the update process:

现在,让我们服务器。 SSH以root身份进入服务器,然后启动更新过程:

1122

非root用户 (Non-Root User)

Next, let’s set up a non-root user so that applications are not run under administrative privileges, which makes the system more secure.

接下来,让我们设置一个非root用户,以使应用程序不会在管理特权下运行,从而使系统更加安全。

As a root user, follow these commands to set up a non-root user:

作为root用户,请遵循以下命令来设置非root用户:

112233445566

In the above snippet we created the new, non-root user user then specified a password for said user. We now need to so that they can run commands that require admin privileges with sudo:

在上面的代码段中,我们创建了新的非root用户,然后为该用户指定了密码。 现在,我们需要以便他们可以使用sudo运行需要管理员特权的sudo

11

Exit from the server and log back in again as the non-root user. Did you notice that the shell prompt changed from a # (pound-sign) to $ (dollar-sign)? This indicates that we are logged in as a non-root user.

从服务器退出,然后以非root用户身份再次登录。 您是否注意到shell提示符从# (英镑符号)更改$ (美元符号)? 这表明我们以非root用户身份登录。

必需的包裹 (Required Packages)

While logged in as the non-root user, download and install the following packages:

以非root用户身份登录后,下载并安装以下软件包:

Note: Below we are giving our non-root user root rights (recommended!). If by any chance you wish to not give the non-root user root rights explicitly, you will have to prepend sudo keyword with every command you execute in the terminal.

注意 :下面我们为非root用户提供root权限(推荐!)。 如果您希望不明确地给予非root用户root权限,则必须在终端中执行的每个命令前面都加上sudo关键字。

1122

Postgres设置 (Postgres Setup)

With the dependencies downloaded and installed, we just need to set up our Postgres server and create a database.

下载并安装依赖项后,我们只需要设置我们的Postgres服务器并创建一个数据库。

Initialize Postgres and then manually start the server:

初始化Postgres,然后手动启动服务器:

1122

Then log in to the Postgres server by switching (su) to the postgres user:

然后通过切换( su )到postgres用户登录到Postgres服务器:

112233

Now create a Postgres user and database required for our project, making sure that the username matches the name of the non-root user:

现在,创建项目所需的Postgres用户和数据库,并确保用户名与非root用户的名称匹配:

112233445566

NOTE: If you need help, please follow the for setting up Postgres on Fedora.

注意 :如果需要帮助,请按照在Fedora上设置Postgres。

Exit psql and return to your non-root user’s shell session:

退出psql并返回到非root用户的shell会话:

1122

When you exit from the postgres session, you will return back to your non-root user prompt. [username@django-cookiecutter-deploy ~]#.

从postgres会话退出时,您将返回到非root用户提示符。 [username@django-cookiecutter-deploy ~]#

Note: Did you notice the # sign at the prompt? This is now appearing because we gave our non-root user the root rights before starting off with setting up our server.

注意 :您是否在提示符处注意到#号? 现在出现这种情况的原因是,在开始设置服务器之前,我们为非root用户授予了root权限。

Configure Postgres so that it starts when the server boots/reboots:

配置Postgres,使其在服务器启动/重启时启动:

1122

项目设置 (Project Setup)

Clone the project structure from your GitHub repo to the /opt directory:

将项目结构从GitHub存储库克隆到/ opt目录:

11

NOTE: Want to use the repo associated with this tutorial? Simply run: sudo git clone https://github.com/realpython/django_cookiecutter_fedora /opt/django_cookiecutter_fedora

注意 :是否要使用与此教程关联的存储库? 只需运行即可: sudo git clone https://github.com/realpython/django_cookiecutter_fedora /opt/django_cookiecutter_fedora

依赖设置 (Dependency Setup)

Next, in order to get your Django Project in a state ready for deployment, create and active a virtualenv inside your project’s root directory:

接下来,为了使您的Django项目处于准备好部署状态,请在项目的根目录内创建并激活virtualenv:

112233

Before activating the virtualenv, give the current, non-root user admin rights (if not given):

在激活virtualenv之前,请赋予当前的非root用户管理员权限(如果未提供):

1122

Unlike with the set up of the development environment from above, before installing the dependencies we need to install all of ’s external libraries. Check out this for more info.

与从上方设置开发环境不同,在安装依赖项之前,我们需要安装所有的外部库。 查看此以获取更多信息。

1122

Next, we need to install one more package in order to ensure that we do not get conflicts while installing dependencies in our virtualenv.

接下来,我们需要再安装一个软件包,以确保在virtualenv中安装依赖项时不会发生冲突。

11

Then run:

然后运行:

11

Again, this will install all the base, local, and production requirements. This is just for a quick sanity check to ensure all is working. Since this is technically the production environment, we will change the environment shortly.

同样,这将安装所有基本,本地和生产要求。 这只是为了快速进行健全性检查,以确保一切正常。 由于从技术上讲这是生产环境,因此我们将在短期内更改环境。

NOTE: Deactivate the virtualenv. Now if you issue an exit command – e.g., exit – the non-root user will no longer have root rights. Notice the change in prompt. That said, the user can still activate the virtualenv. Try it!

注意 :禁用virtualenv。 现在,如果发出退出命令(例如exit ,则非root用户将不再具有root权限。 注意提示中的更改。 也就是说,用户仍然可以激活virtualenv。 试试吧!

健全性检查(取2) (Sanity Check (take 2))

Apply all the migrations:

应用所有迁移:

1122

Now run the server:

现在运行服务器:

11

To ensure things are working fine, just visit the server’s IP address in your browser – e.g., <ip-address or hostname>:8000.

为确保一切正常,只需在浏览器中访问服务器的IP地址即可,例如<ip-address或hostname>:8000。

独角兽设置 (Gunicorn Setup)

Before setting up Gunicorn, we need to make some changes to the production settings, in the /config/settings/production.py module.

在设置Gunicorn之前,我们需要在/config/settings/production.py模块中对生产设置进行一些更改。

Take a look at the production settings , which are the minimal settings needed for deploying our Django Project to a production server.

在查看生产设置,这是将Django项目部署到生产服务器所需的最小设置。

To update these, open the file in VI:

要更新这些文件,请在VI中打开文件:

11

First, select all and delete:

首先,全选并删除:

11

And then copy the new settings and paste them into the now empty file by entering INSERT mode and then pasting. Make sure to update the ALLOWED_HOSTS variable as well (very, very important!) with your server’s IP address or hostname. Exit INSERT mode and then save and exit:

然后复制新设置,并通过进入INSERT模式然后粘贴将其粘贴到现在为空的文件中。 确保也使用服务器的IP地址或主机名更新ALLOWED_HOSTS变量(非常非常重要!)。 退出INSERT模式,然后保存并退出:

11

Once done, we need to add some environment variables to the .bashrc file, since the majority of the config settings come from environment variables within the production.py file.

完成后,由于大多数配置设置来自production.py文件中的环境变量,因此我们需要向.bashrc文件中添加一些环境变量。

Again, use VI to edit this file:

再次使用VI编辑该文件:

11

Enter INSERT mode and add the following:

进入INSERT模式并添加以下内容:

112233445566

Two things to note:

有两件事要注意:

  1. Notice how we updated the DJANGO_SETTINGS_MODULE variable to use the production settings.
  2. It’s a good to change your DJANGO_SECRET_KEY to a string. Do this now if you want.
  1. 注意我们如何更新DJANGO_SETTINGS_MODULE变量以使用生产设置。
  2. DJANGO_SECRET_KEY更改为字符串是一个好 。 如果需要,请立即执行此操作。

Again, exit INSERT mode, and then save and exit VI.

再次退出INSERT模式,然后保存并退出VI。

Now just reload the .bashrc file:

现在,只需重新加载.bashrc文件即可:

11

Ready to test?! Inside the root directory, with the virtualenv activated, execute the gunicorn server:

准备测试了吗? 在root目录中,激活virtualenv,执行gunicorn服务器:

11

This will make our web application, again, serve on <ip-address or hostname>:8000.

这将使我们的Web应用程序再次在<ip-address或hostname>:8000上运行。

Keep in mind that as soon as we log out of our server this command will stop and hence we would no longer be able to serve our web app. So, we have to make our gunicorn server execute as a service so that it can be started, stopped, and monitored.

请记住,一旦我们退出服务器,此命令将停止,因此我们将不再能够提供Web应用程序。 因此,我们必须使我们的gunicorn服务器作为服务执行,以便可以启动,停止和监视它。

Nginx配置 (Nginx Config)

Follow these steps for adding a configuration file for making our Django Project serve via Nginx:

请按照以下步骤添加配置文件,以使我们的Django Project通过Nginx服务:

1122

Add the following, making sure to update the server, server_name, and location for project root:

添加以下内容,确保更新项目根目录的serverserver_namelocation

11223344556677889910101111121213131414151516161717181819192020212122222323242425252626272728282929

Save and exit VI, and then restart the Nginx server:

保存并退出VI,然后重新启动Nginx服务器:

11

That’s it!

而已!

Gunicorn开始脚本 (Gunicorn Start Script)

Now let’s create a Gunicorn start script which will run as an executable, making our bootstrapped Django web application run via the Gunicorn server, routed through Nginx.

现在,让我们创建一个Gunicorn启动脚本,该脚本将作为可执行文件运行,从而使我们引导的Django Web应用程序通过Gunicorn服务器运行,并通过Nginx进行路由。

Within the project root, run:

在项目根目录中,运行:

112233

The contents of the gunicorn_start script can be found . It is divided into 3 significant parts, which are, for the most part, self-explanatory. For any questions, please comment below.

gunicorn_start脚本的内容可以在找到。 它分为3个重要部分,大部分都是不言自明的。 如有任何疑问,请在下面评论。

NOTE: Make sure the USER and GROUP variables match the same user and group for the non-root user.

注意 :确保USERGROUP变量与非root用户匹配相同的用户和组。

Paste the contents into VI, and then save and exit.

将内容粘贴到VI中,然后保存并退出。

Finally, let’s make it executable:

最后,让我们使其可执行:

11

Start the server:

启动服务器:

11

Once again visit the your server’s ip address in the browser and you will see your Django web app running!

再次在浏览器中访问服务器的IP地址,您将看到Django Web应用程序正在运行!

Did you get a 502 Bad gateway error? Just follow these steps and it will probably be enough to make your application work…

您是否收到502错误网关错误? 只需按照以下步骤操作,可能就足以使您的应用程序正常工作……

修改SELinux策略规则 (Modifying SELinux Policy Rules)

112233

Once done, make sure to restart your server via Digital Ocean’s dashboard utility.

完成后,请确保通过Digital Ocean的仪表板实用程序重新启动服务器。

系统化 (Systemd)

To make our gunicorn_start script run as a system service so that, even if we are no longer logged into the server, it still serves our Django web application, we need to create a service.

为了使gunicorn_start脚本作为系统服务运行,以便即使我们不再登录服务器,它仍然可以为Django Web应用程序提供服务,我们需要创建一个服务。

Just change the working directory to /etc/systemd/system, and then create a service file:

只需将工作目录更改为/ etc / systemd / system,然后创建一个服务文件:

1122

Add the following:

添加以下内容:

1122334455667788991010111112121313

Save and exit, and then start the service and enable it:

保存并退出,然后启动该服务并启用它:

11

NOTE: If you encounter an error, run journalctl -xe to see more details.

注意 :如果遇到错误,请运行journalctl -xe以查看更多详细信息。

Finally, enable the service so that it runs forever and restarts on arbitrary shut downs:

最后,启用服务,使其永久运行并在任意关闭时重新启动:

11

健全性检查(最终!) (Sanity Check (final!))

Check for the status of the service:

检查服务状态:

11

Now just visit your server’s IP address (or hostname) and you will see a Django error page. To fix this, run the following command in your project’s root directory (with your virtualenv activated):

现在,只需访问服务器的IP地址(或主机名),您将看到Django错误页面。 要解决此问题,请在项目的根目录中运行以下命令(激活virtualenv):

11

Now you are good to go, and you will see the Django web app running on the web browser with all the static files (HTML/CSS/JS) files working.

现在您一切顺利,您将看到Django网络应用程序在网络浏览器上运行,并且所有静态文件(HTML / CSS / JS)文件都在工作。

翻译自:

fedora 开发

转载地址:http://iwhwd.baihongyu.com/

你可能感兴趣的文章
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
win7(64位)php5.5-Apache2.4-mysql5.6环境安装
查看>>
生成商户订单号/退款单号
查看>>
git使用——分支
查看>>
selenium之多线程启动grid分布式测试框架封装(一)
查看>>
突然的伤感
查看>>
Objective-C 链式编程思想
查看>>
开通了一个微信公众账号,主要想分享一些自己对于行业、技术和产品的思考以及收录精彩内容给读者...
查看>>
留言板
查看>>
c#$用法
查看>>
js通过as完成socket通信
查看>>
关于我书里提到的“挂多个类,使用类的组合”的一些补充
查看>>
JavaScrip中的循环语句
查看>>
IntelliJ IDEA和Webstorm 的下载安装、注册码(永久有效)
查看>>