Deploy Django App Shared Hosting.

WBOY
Release: 2024-08-25 06:02:09
Original
368 people have browsed it

description

On this occasion, I will share how to deploy a django application to shared hosting. To make things easier, I use the DirectAdmin panel. If you use cpanel or another panel, I don't think it will be much different. Make sure you already have a domain and buy a shared hosting service anywhere, I will not limit you to using certain services.

The version ofrequirementsused in the project

  • python version 3.9.18
  • Django version 4.2

Steps

Ensure shared hosting services run as they should.

Before entering the deployment method, make sure the shared hosting service is running as it should.

Deploy Django App Shared Hosting.
In practice this time, I will use the domain artinetix.cloud (adjust to your domain name), to ensure the service runs normally, if we access the domain URL a page will appear like the following.

Deploy Django App Shared Hosting.

Create a sub-domain

Create a sub-domain to differentiate one application URL from another, one domain can be used for many sub-domains. For example, if the domain used is artinetix.cloud then the sub-domain that will be created will be something like django_tutor.artinetix.cloud. You can read more about sub-domains on internet sources to better understand sub-domains.

  • Open the dashboard page
  • Look for the Subdomain-management
  • menu

Deploy Django App Shared Hosting.

  • Make sure you have selected the domain that will be used as the base (red circle) and add a new subdomain (blue circle)

Deploy Django App Shared Hosting.

  • Just use the default document root, I will create a sub-domain with the name django-tutor

Deploy Django App Shared Hosting.

  • Click add sub-domain, if the sub-domain has been successfully created it will appear in the table.

Deploy Django App Shared Hosting.

  • Try visiting the sub-domain address, is it accessible and displays a page like the following?

Deploy Django App Shared Hosting.

Don't worry, because we haven't setup SSL so we can access using the https protocol, not just http. You can click advance and continue to the address to see if the url works.

Deploy Django App Shared Hosting.

The image above indicates that the URL is now accessible. The next step is SSL setup, so that the web is safer from attacks from outside parties.

SSL setup

  • Open the ssl certificates page.

Deploy Django App Shared Hosting.

  • Open the menu to generate a new SSL.

Deploy Django App Shared Hosting.

  • Scroll down and you will find your URL address (both without www and with wwww) that is not checked. Check the URL address that will generate SSL (red circle) and make sure force SSL redirect is also checked (blue circle) then click save.

Deploy Django App Shared Hosting.

At the top of the web page, a loading animation line will appear indicating that the process is running. If the process is successful, a notification will appear as follows.

Deploy Django App Shared Hosting.

There are SSL generated subdomain names (with and without www) circled in blue.

  • After refreshing the page, congratulations! now your subdomain can be accessed more safely.

Deploy Django App Shared Hosting.

Preparing Django App

I won't focus on how to setup the djangoenvironmentfrom scratch to shorten the article. Prepare a django app that has been developed and can run normally in the localenvironment.

In practice this time I will use the default django first (still in the initial settingsgenerate) using django 4.2
Deploy Django App Shared Hosting.
When you run a django project and a page like the one in the image above appears, that means django is running normally.

  • file to be used Not all files have to be stored on the server, adjust it to your project. On this occasion, I only used the .gitignore python template on github.

file treeof the project used in the article (in the sense that this file will go to the server, not the file that has been included in the .gitignore list) is as follows.

rreee

Create a zip containing yourdjangoproject files before placing them on the server.

  • Managefiles on the panel Enter the file manager menu

Deploy Django App Shared Hosting.

Navigate to domains folder > (url of your subdomain) > public html, in the public html folder contains the index.html file. That file is the file that was displayed when we created the subdomain earlier.

Deploy Django App Shared Hosting.

Delete the cgi-bin folder and index.html file, upload the zip project we created earlier and don't forget to extract it. For now, the steps in thissectionare enough, let's move on to creating a django environment.

  • Create a python app environment In the dashboard panel, clicksetup python app.

Deploy Django App Shared Hosting.

select create python app and enter the configuration as follows, for the application root it can be adjusted because when placing the file we have placed the python app in the public_html folder so that the configuration is like in the image below. If it is appropriate, clickcreateto create an environment on the server.

Deploy Django App Shared Hosting.
After successfullysetup python appand visiting the URL above, it will only display python it works. This happens because we haven'timportedthe django project and the environment doesn't have django installed.

  • install django in the environment copy the command that I have circled in red by clicking on the command text.

Deploy Django App Shared Hosting.

enter the terminal

Deploy Django App Shared Hosting.

In the terminal, paste the previous code by right clicking and pasting.

If it is successful, please follow the following steps.

  1. Enter the command pip list to see what packages have been installed.
  2. check whether we are in the correctdirectorywith the ls command, make sure the requirements.txt file is there.
  3. Install all packages using the requirements.txt file, with the command pip install -r requirements.txt

Installing django on theenvirontmentis complete, we will move on to the file manager to edit the passenger_wsgi file.

  • Edit the django passenger_wsgi file Import the django application from the django project name folder (the folder contains the settings.py file) by editing the passenger_wsgi file, this file is in the same directory as requirements.txt, in other words the root of our django app.

passenger.py

rreee

note: django_tutor adjusts to the project name of the respective django app

  • settings.pyファイルを編集する本番の設定ファイルの構成は、開発段階の時とは異なります。settings.pyファイル(プロジェクト名フォルダーにあります)を次のように編集します。
  1. DEBUG = True を DEBUG = False に変更します
  2. ホストがアプリにアクセスできるようにします。使用されているドメイン名に応じて、ALLOWED_HOSTS = [] は ALLOWED_HOSTS = ['django-tutor.artinetix.cloud', 'www.django-tutor.artinetix.cloud'] になります。
  3. ファイルの最後に、静的ファイル (css、js、画像など) をコピーする次のコード行を追加します。 STATIC_ROOT = BASE_DIR / 'public'

[ダッシュボード] パネル > [Django] に移動して、Django アプリを再起動します。 Python アプリのセットアップ >アクション列の「再起動」をクリックします。

Deploy Django App Shared Hosting.

URL にアクセスしてみると、次のようなページが表示されますか?

Deploy Django App Shared Hosting.

URL の末尾に admin をドメイン名/管理者として追加して、管理セクションにアクセスしてみてください。そうすると、多かれ少なかれ以下のようなページが表示されます。

Deploy Django App Shared Hosting.

注意してみると冗談のように見えます。これは、CSS、JS、およびその他の静的ファイルが Django プロジェクトにコピーされていないためです。これを行うには、次の手順に注意してください。

  1. Pythonアプリのセットアップページに移動します。
  2. 目的の actionpython アプリの編集メニューをクリックします。
  3. コマンドをコピーして環境に入ります。Deploy Django App Shared Hosting.
  4. ターミナルページに入り、コマンドを貼り付けます。
  5. 環境に正常に入ることができたら、python manage.pycollectstatic コマンドを実行して、CSS や JS などの静的ファイルを settings.py の STATIC_ROOT パスに従ってフォルダーに移動します。
上記の手順を正常に実行すると、アプリのルート フォルダーに CSS および JS ファイル (Django で使用される静的ファイル) を含むパブリック フォルダーが作成されるはずです。 URL (url/admin) の後ろに admin を追加して、django アプリの管理者 URL にアクセスしてみてください。表示がまだ以下のような場合、コンソールを確認すると、「MIME タイプが実行可能ではありません」というエラーが表示されます。次に、whitenoise パッケージを django アプリに追加する必要があります。次の URL Whitenoise ドキュメントを参照してください。サーバーでコードを変更するたびに必ず再起動することを忘れないでください :).

Deploy Django App Shared Hosting.MIME タイプ エラーです。

Deploy Django App Shared Hosting.

これは、DirectAdmin を使用して

共有ホスティングに Django アプリをデプロイする方法です。実際には、デプロイ以外にも、データベースのセットアップと環境変数の追加など、やるべきことがまだたくさんあります。また、この方法は私が通常行っている手動の方法なので、複雑に見えることにも注意する必要があります。デプロイメントプロセスを簡単にするために、通常は github アクション経由で ci と cd を使用できます。私からはこれで十分です。提案や建設的な批判など、何か伝えたいことがあれば、コメント欄にコメントしてください。以上です、ありがとうございます

The above is the detailed content of Deploy Django App Shared Hosting.. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!