如何在Linux安裝Python+JDK+Mysql+Tomcat+Django以及簡單的跑一個java web專案和一個簡單的Django專案

雪亦田發表於2024-06-22

Linux 搭建Java web平臺

一,Linux安裝JDK

①下載安裝包 解壓

[root@wwww ~]# tar -zxvf jdk-linux-x64.tar.gz

②移動到usr/local/java目錄下

image-20240621220407553

③安裝完成配置環境變數

vi /etc/profile  

最後如下兩行即可

image-20240621220733620

JAVA_HOME=/usr/local/java/jdk1.8  # 這是你放的jdk安裝路徑

CLASSPATH=$JAVA_HOME/lib/

PATH=$PATH:$JAVA_HOME/bin

export PATH JAVA_HOME CLASSPATH

環境變數重新載入一下

source /etc/profile

image-20240621220847791

安裝完成

二,Linux安裝Tomcat

解壓

[root@wwww ~]# tar -zxvf apache-tomcat-8.5.35.tar.gz 

image-20240621221255704

# 檢視命令執行情況
ps -ef|grep tomcat

本操作在再關閉防火牆的情況寫執行,如沒有關閉新增規則:

firewall-cmd --zone=public --add-port=8080/tcp --permanent

啟動命令 前提在tomcat目錄的bin目錄下

[root@wwww bin]# ./startup.sh

停止命令 前提在tomcat目錄的bin目錄下

[root@wwww bin]# ./shutdown.sh

image-20240621221011669

安裝完成

三,Mysql安裝

yum安裝

yum install mysql
# 如果必須要安裝MySQL,首先必須新增mysql社群repo,執行命令:
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

yum install mysql-server 
# 啟動mysql
systemctl start mysqld
# 初始化mysql安全  設定密碼
mysql_secure_installation

# 也可以設定開機自啟
systemctl enable mysqld

image-20240621225145795

資料庫安裝完成

使用命令如下:

mysql> create database db;
mysql> use db;

mysql> create table user(id int,name varchar(20));

mysql> insert into user values (1,"zhe shi yan shi de");
mysql> insert into user values (2,"Liming");

四,手搓簡單的java web專案

為了實現連線資料庫,將jar包放在tomcat的lib目錄下(jar在目錄中)

①先建立資料

image-20240621231221765

②寫入jsp檔案

在tomcat目錄的webapps下建立html/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>資料庫連線演示</title>
</head>
<body>
    <h2>Demo</h2>
    
    <%-- Import necessary Java classes --%>
    <%@ page import="java.sql.*" %>
    <% 
        // JDBC driver name and database URL
        String jdbcUrl = "jdbc:mysql://localhost:3306/db";  // 修改為你的資料庫名稱 下使用者名稱&密碼同改
        String dbUser = "root";
        String dbPassword = "123456";
        
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try {
            // Register JDBC driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // Open a connection
            conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
            
            // Execute a query
            stmt = conn.createStatement();
            String sql = "SELECT * FROM user";
            rs = stmt.executeQuery(sql);
            
            // Display data from the result set
            out.println("<table border='1'>");
            out.println("<tr><th>ID</th><th>Name</th></tr>");
            
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
       
                out.println("<tr>");
                out.println("<td>" + id + "</td>");
                out.println("<td>" + name + "</td>");
                
                out.println("</tr>");
            }
            
            out.println("</table>");
            
        } catch (SQLException | ClassNotFoundException e) {
            out.println("Error: " + e.getMessage());
        } finally {
            // Close resources
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                out.println("Error closing resources: " + e.getMessage());
            }
        }
    %>
</body>
</html>


image-20240621233210987

完成

Linux搭建Django

一,安裝python3.6.8

無腦命令安裝

mkdir /usr/local/python3
cd /usr/local/python3
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar xvf Python-3.6.8.tgz

安裝工具

yum -y install vim unzip net-tools && yum -y install wget && yum -y install bzip2 && yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel && yum -y install libglvnd-glx && yum -y install gcc gcc-c++

make && make install

yum install epel-release

yum -y install gcc

yum install python3-pip

yum install python3-wheel

image-20240622001019962

二,安裝Django

pip3 install Django==2.1.2

建立命令如下:

django-admin startproject myproject
cd myproject

python manage.py runserver

python manage.py startapp myapp

編寫myapp配置如下:

第一次執行需要對資料庫進行初始化

python3 manage.py migrate

新建應用myapp的view.py

from django.http import HttpResponse
 
def home(request):
    return HttpResponse("Hello, World!")

image-20240622010901822

新建應用myapp的urls.py

from django.urls import path
from .views import home
 
urlpatterns = [
    path('', home, name='home'),
]

image-20240622011051894

在myproject中設定url路徑

from django.urls import include, path
 
urlpatterns = [
    path('myapp/', include('myapp.urls')),
]

image-20240622011606633

最終執行結果:

image-20240622013253596

完成

小問題:在執行此平臺時 使用命令python3 manage.py runserver僅有本地可訪問,如想區域網訪問則如下開啟即可

python3 manage.py runserver 0.0.0.0:8000

image-20240622014358899

當然對映的路徑不唯一,也有admin路徑

image-20240622014620904

完成

相關文章