一、条件:在已经安装了Ubuntu22.04LTS的虚拟机上且网络已经调通的情况下开始搭建;
二、步骤:
1、更新数据源,命令:~$ sudo apt update
2、安装apache命令:~$ sudo apt install apache2
提示信息:........
Need to get 2,135 kB of archives.
After this operation, 8,486 kB of additional disk space will be used.
Do you want to continue? [Y/n] y【输入y回车,开始安装】
3、验证apache安装:浏览器中输入ip出现下图:
4、安装MySQL命令:~$ sudo apt install mysql-server
提示信息:........
Need to get 28.6 MB of archives.
After this operation, 240 MB of additional disk space will be used.
Do you want to continue? [Y/n] y【输入y回车,开始安装】
5、验证MySQL安装:~$ mysql -uroot -p(回车后,出现下图)
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
原因:是因为auth_socket的验证类型引起的。
6、解决MySQL的root密码步骤:
1)通过sudo进入mysql,命令:~$ sudo mysql(回车后,出现下图)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2)进入mysql库:mysql > use mysql;(回车后,出现下图)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
3)改密码(新密码暂定【mysql】):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysql';(回车后,出现下图)
Query OK, 0 rows affected (0.00 sec)
4)查看user表,命令:mysql> select user, authentication_string,plugin from mysql.user;(回车后,查看数据表的最后一行root,见下图)
root *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA mysql_native_password
说明已经将密码改成功了!
5)刷新:mysql> flush privileges;
6)退出:exit;
7)重启:~$ sudo service mysql restart
8)重新登录:~$ mysql -uroot -pmysql(登录成功如下图)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
7、安装Python:由于Ubuntu22.04已经默认安装了Python,通过命令查看版本:~$ python3 -V(大V,见下图)
Python 3.10.4
8、更新:~$ sudo apt-get update
9、安装Python管理软件包pip:命令:~$ sudo apt install python3-pip(回车后,见下图,***有时需要8-9反复安装多次才能成功***)
Need to get 71.1 MB of archives.
After this operation, 238 MB of additional disk space will be used.
Do you want to continue? [Y/n] y【输入y回车,开始安装】
10、安装Python django (Python Web 框架),命令:~$ sudo pip3 install django(回车后,见下图)
Defaulting to user installation because normal site-packages is not writeable
Collecting django
Downloading Django-4.0.5-py3-none-any.whl (8.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 KB 66.6 kB/s eta 0:00:00
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.2 django-4.0.5 sqlparse-0.4.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviourcommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
11、新建用户test,准备在test用户下新键项目,命令:~$ sudo adduser test
12、安装tree(查看项目时使用),命令:~$ sudo apt install tree
13、使用test新用户登录,然后创建demo项目:~$ django-admin startproject demo(没有回显即为成功!)
14、查看刚刚创建的demo项目,命令:~$ tree demo(回车后,见下图2)
15、修改demo项目下的settings.py文件中的ALLOWED_HOSTS参数,命令:~$ vim demo/demo/settings.py
将这行,改成:ALLOWED_HOSTS=['*']
16、运行:~/demo$ python3 manage.py runserver 0.0.0.0:8000&
[1] 1275
test@ub22:~/demo$ Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 01, 2022 - 13:28:17
Django version 4.0.4, using settings 'demo.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
17、查看效果:在浏览器地址栏输入ip:80000,(见下图3):
18、查看项目文件发生了哪些变换,命令:~$ tree,见下图4:
19、创建Django版的Hello World:利用django框架编写Hello World!步骤:
1)在demo主模块下创建视图文件views.py文件(做的是处理业务逻辑的操作),命令:~/demo/demo$ vim views.py(内容如下图5:)
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello World!")
2)定义视图对应的URL,修改urls.py,命令:~/demo/demo$vim urls.py(内容如下图6:)
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('',views.hello,name='hello'),
path('admin/', admin.site.urls),
]
最后demo项目结构图:
3)运行:~/demo$python3 manage.py runserver 0.0.0.0:8000&(有时运行时会出错,见下图)
解决方法:执行一下命令:~/demo$ python3 manage.py migrate
4)服务启动后在浏览器中输入:ip:8000(效果如下图:)