fedora 开发_在Fedora上开发和部署Cookiecutter-Django

fedora 开发

Last time 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 cookiecutter globally and then generate a bootstrapped Django project:

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

1
1
2
2

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

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

NOTE: Check out the Local Setup 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 django_cookiecutter_fedora/config/settings/common.py for more info). Follow the steps to set up a Postgres database server, from any good resource on the Internet or just scroll down to the Deployment Section for setting it up on Fedora.

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

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

注意 :如果您使用的是Mac,请查看Postgres.app

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

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

1
1
2
2

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 here.

注意 :根据您使用的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项目处于准备开发的状态,请导航至根目录,创建/激活虚拟环境,然后安装依赖项:

1
1
2
2
3
3
4
4

完整性检查 (Sanity Check)

Apply the migrations and then run the local development server:

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

1
1
2
2
3
3

Ensure all is well by navigating to http://localhost:8000/ 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.

在浏览器中导航到http:// localhost:8000 /以查看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设置并在本地运行,我们现在可以继续进行部署,在这里我们将使用以下工具:

Fedora 23设置 (Fedora 23 Setup)

Set up a quick Digital Ocean droplet, making sure to use a Fedora 23 image. For help, follow this tutorial. Make sure you set up an SSH key for secure login.

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

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

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

1
1
2
2

非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用户:

1
1
2
2
3
3
4
4
5
5
6
6

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

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

1
1

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关键字。

1
1
2
2

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,然后手动启动服务器:

1
1
2
2

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

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

1
1
2
2
3
3

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用户的名称匹配:

1
1
2
2
3
3
4
4
5
5
6
6

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

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

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

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

1
1
2
2

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,使其在服务器启动/重启时启动:

1
1
2
2

项目设置 (Project Setup)

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

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

1
1

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:

1
1
2
2
3
3

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

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

1
1
2
2

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

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

1
1
2
2

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中安装依赖项时不会发生冲突。

1
1

Then run:

然后运行:

1
1

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:

应用所有迁移:

1
1
2
2

Now run the server:

现在运行服务器:

1
1

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 here, 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中打开文件:

1
1

First, select all and delete:

首先,全选并删除:

1
1

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模式,然后保存并退出:

1
1

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编辑该文件:

1
1

Enter INSERT mode and add the following:

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

1
1
2
2
3
3
4
4
5
5
6
6

Two things to note:

有两件事要注意:

  1. Notice how we updated the DJANGO_SETTINGS_MODULE variable to use the production settings.
  2. It’s a good practice to change your DJANGO_SECRET_KEY to a more complex 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文件即可:

1
1

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

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

1
1

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服务:

1
1
2
2

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

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

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29

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

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

1
1

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:

在项目根目录中,运行:

1
1
2
2
3
3

The contents of the gunicorn_start script can be found here. 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:

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

1
1

Start the server:

启动服务器:

1
1

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)

1
1
2
2
3
3

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 systemd service.

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

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

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

1
1
2
2

Add the following:

添加以下内容:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13

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

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

1
1

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:

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

1
1

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

Check for the status of the service:

检查服务状态:

1
1

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):

1
1

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)文件都在工作。

翻译自: https://www.pybloggers.com/2016/01/development-and-deployment-of-cookiecutter-django-on-fedora/

fedora 开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值