我的問題的一點背景......我正在嘗試按照使用 linode 設定 linux apache 服務器的教程(
教程很長,所以我已經分解了這篇文章中的每一步。
我必須遵循的步驟是:
(一切都在 Windows Linux Bash shell 中完成)
創建 Linode 并分析我得到的 IP 地址和 SSH 憑據
到服務器的根連接
ssh第一次進入服務器安裝軟體更新
運行命令
sudo apt-get update && apt-get upgrade設定主機名
hostnamectl set-hostname SERVER_NAME
然后檢查它是否正確執行
hostnameSetting Host File
nano /etc/hostsand then adding the IP address and server name to the file.Adding Limited User
The guy in the video said it's not good practice to use root user because it's easy to mess things up like that so we created a limited user with
adduser USER_NAMEand then gave it sudo permissions withadduser USER_NAME sudoSetting Up SSH Key Based Authentication
In my local machine's bash shell:
ssh keygen -b 4096And deposit that file into my home folder, after that I copy that file over to my server withscp ~/.ssh/id_rsa.pub user@serverip:~/.ssh/authorized_keysthen lasty confirming it was done correcty in the server withls .sshSetting Permissions for SSH Directory
Attach certain permision like
sudo chmod 700 ~/.ssh/andsudo chmod 600 ~/.ssh/*Forbiding Root Login & Password Authentication
sudo nano /etc/ssh/sshh_configto forbit #PermitRootLogin and #PasswordAuthentification and then restart withsudo systemctl restart sshdSetting Up a Firewall
First, I install Uncomplicated Firewall with
sudo apt-get install ufwand then I do the following commands:sudo ufw default allow outgoing,sudo ufw default deny incoming,sudo ufw allow sshandsudo ufw allow 8000. After that, I enable the firewall withsudo ufw enableGenerating requirements.txt File
Here I actually did it a bit different that in the video. I open my VSCODE environment for the project in question, and then I run a
pip freezein the terminal to see if the dependencies are correct, if they are I write the requirements withpip freeze > requirements.txtCopying Django Application on to the Webserver
This step is pretty straightforward. Just
scp -r /folder/ user@serverip:~/and that way the folder project is copied into the web server.Creating Virtual Environment on the Server
First run a
sudo apt-get install python3-pipand thenpip install sudo apt-get install python3-venv. Once that's done, to create my virtual environment I dopython3 -m venv django_project/venvand lastly I activate it bycding into the project's folder and thensource venv/bin/activate.Installing Dependencies
With my virtual environment running I run a
pip install -r requirements.txtChanging Django Settings for Testing the Application on Django Server
Inside the project's folder:
sudo nano django_project/settings.pyand add my server's IP to the allowed hosts list, and add a STATIC_ROOT directory.Collecting Static Files
python manage.py collectstaticWhich could collect about 120 static files, but my project is a somewhat different from the on in the video because I added way more features so it collects about 137 files (in case these details are needed)Testing Application
python manage.py runserver 0.0.0.0:8000and then test to see if there is any problems, there are not (minus some variables I forgot to add because they were in my PC's environment variables) so I move on to the next step.Installing Apache & ModWSGI
sudo apt-get install apache2,sudo apt-get install libapache2-mod-wsgi-py3.Configuring Apache Webserver
Then move into the apache configuration folder
cd /etc/apache2/sites-available/and create a new configuration file based off the default one withsudo cp 000-default.conf django_project.conf, and then I edit withsudo nano django_project.confit and add the following things to it: https://github.com/CoreyMSchafer/code_snippets/blob/master/Django_Blog/snippets/django_project.confEnabling Site Through Apache
Run the following commands:
sudo a2ensite django_project,sudo a2dissite 000-default.confSetting Up File Permissions
Run the following commands:
sudo chown :www-date django_project/db.sqlite3,sudo chmod 664 django_project/db.sqlite3andsudo chown :www-data django_project/. Thensudo chown -R :www-data django_project/media/andsudo chmod -R django_project/media```Creating Configuration File for Hiding Sensitive Information
I won't detail this process, I just make a .json file with some secret information like the SECRET_KEY and the email and password.
Updating Project Settings File
I delete the sentitive information from the settings.py of the project, and instead add those with the .json file (like if it were an environment variable).
Allowing http Traffic
Run the following commands:
sudo ufw delete allow 8000, andsudo ufw allow http/tcp.Restarting the Server & Running the Site
sudo service apache2 restart
請,我會很感激任何幫助。我必須為一個學校專案修復這個錯誤,但我找不到真正修復它的解決方案。我在一些論壇上讀到我只需要再次更改某些權限,我這樣做了,但仍然是同樣的錯誤。這真的讓我發瘋。
uj5u.com熱心網友回復:
你還沒有做
sudo chown :www-data django_project/django_project
這是 wsgi.py 腳本的檔案夾
uj5u.com熱心網友回復:
我認為 Forbidden 被回傳為 Apache2 用戶(www-data)無法訪問您的 HOME 檔案夾,將所有內容移動到另一個路徑,如 /data 或 /var/www 并通過 chmod 命令為 www-data 提供 rx 訪問權限。
uj5u.com熱心網友回復:
至少現在我已經找到了解決方案。我在我的主檔案夾上運行了一個sudo chmod 777,這樣實際上每個檔案都可以訪問。我聽說這個解決方案不推薦,但現在可以了。
我仍然不知道為什么發布的其他解決方案對我不起作用,因為據我了解,為服務器提供樂趣所需的每個檔案都在 django_project 檔案夾中。
不過,我將對其進行更多研究,因為我不知道擁有具有權限的每個檔案有多安全。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425289.html
