华南农业大学Linux课程综合实验-超详细版(实现用Go、nodejs、python、php读取mysql数据)


一、准备工作

1. 领取阿里云服务器

免费使用一个月ECS共享性n4

购买页面中主要注意的是操作系统,选择自己的操作系统即可。

在这里插入图片描述
如何查看Centos系统版本命令

[root@localhost ~]# cat /etc/redhat-release

2. 服务器初始设置

2.1 设置实例密码

如果找不到这个页面,后面的找不到控制台页面有说明
在这里插入图片描述

2.1.1 找不到控制台页面

如果你现在在这个页面中:阿里云控制台首页
在这里插入图片描述
往下滑找到云产品推荐,点击云服务器ESC
在这里插入图片描述
进入新页面后,点击管理控制台即可找到云服务器。
在这里插入图片描述

2.2 远程登录云服务器

在本地终端运行ssh userName@hostIP远程登录云服务器。userName为云服务器用户名,默认是root,@符号后接云服务器的公网IP地址。(是公网IP,不是内网IP。)

[root@localhost ~]# ssh root@xxx.xx.xxx.xx

2.3 修改云服务器密码

密码太难记?登录完云服务器后可执行passwd命令修改密码

[root@root ~]# passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

2.4 实现自动远程连接

若使用的终端应用是termius,为了不用每次都要手动远程连接和方便传文件,我们可以创建一个newHost
在这里插入图片描述

填上相应信息
在这里插入图片描述

往下滑,找到SSH Agent Forwarding
在这里插入图片描述
选择你要远程连接的Host,之后只需虚拟机上登录Host的用户账号,终端就可以直接连接newHost以实现远程连接了。
在这里插入图片描述




二、部署服务器

1. 安装mariadb(数据库)

由于CentOS7不支持MySQL了,而内部集成了mariadb。安装MySQL的话会和MariaDB的文件冲突,所以直接安装mariadb即可。如果安装过mysql的要删干净才可以下载安装mariadb,可通过这个链接查看是否安装本地mysql

-安装mariadb-server

[root@root ~]# yum install -y mariadb-server

若安装中出现了获取 GPG 密钥失败,需要重新获取

GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/
rpm-gpg/RPM-GPG-KEY-mysql-2022"

运行以下命令

[root@root ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

再重新安装mariadb-server

[root@root ~]# yum install -y mariadb-server

启动服务

[root@root ~]# systemctl start mariadb.service

查看mariadb运行状态

[root@root ~]# systemctl status mariadb.service

在这里插入图片描述
添加到开机启动

[root@root ~]# systemctl enable mariadb.service

安全设置,修改数据库管理员密码

[root@root ~]# mysql_secure_installation
//遇到 Enter current password for root (enter for none):   直接回车。

//设置如下
Set root password? [Y/n] Y                    
New password: 
Re-enter new password: 

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] n

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y

1.1 解决数据库乱码问题

配置MariaDB字符编码。

[root@root ~]# vi /etc/my.cnf

#添加以下内容
[mysqld]
character-set-server=utf8

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysql.server]
default-character-set =utf8

在这里插入图片描述
重启mariadb

[root@root ~]# systemctl restart mariadb

1.2 数据库建表

[root@root ~]# mysql -u root -p
Enter password: 

MariaDB [(none)]> CREATE DATABASE test;
MariaDB [(none)]> use test

MariaDB [test]> CREATE TABLE students(
       stuId VARCHAR(12) NOT NULL,
       stuName VARCHAR(50) NOT NULL,
       PRIMARY KEY (stuId)
      );

可以用show tables;命令查看创建表格是否成功。

MariaDB [test]> show tables;

插入数据到students表中

MariaDB [test]> INSERT INTO students VALUES("yourStuID","yourName");

查看students表是否正确插入数据

MariaDB [test]> SELECT * FROM students;

ctrl+c退出MariaDB。

2. 安装Nginx

安装nginx

[root@root ~]# yum list nginx*
[root@root ~]# yum install -y nginx.x86_64

配置nginx

[root@root ~]# vim /etc/nginx/nginx.conf

#在http里的server里添加
        location ~ \.php$ {
                root   /usr/share/nginx/html;
                fastcgi_pass   127.0.0.1:9000;  #php-fpm默认的端口是9000
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
         }

        location ~ \.go$ {
                proxy_pass   http://127.0.0.1:8000;
        }
        location ~ \.js$ {
                proxy_pass   http://127.0.0.1:8001;
        }
              location  index.py {
                proxy_pass   http://127.0.0.1:8800/;
       }

运行nginx,并查看状态

[root@root ~]# service nginx restart
[root@root ~]# systemctl status nginx

在这里插入图片描述

3. 开启端口

端口描述
80htpp、nginx
8000测试go端口
8001测试nodejs端口
8800测试python端口
9000php-fpm,php

3.1 开启阿里云安全组端口

在这里插入图片描述
进去后表格右上角,点击添加安全组规则,把5个端口都给加上。其中一个示例:

在这里插入图片描述

3.2 开启服务器防火墙端口

[root@root ~]# service firewalld start
[root@root ~]# systemctl status firewalld  #查看开启防火墙是否成功
[root@root ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@root ~]# firewall-cmd --zone=public --add-port=8000/tcp --permanent
[root@root ~]# firewall-cmd --zone=public --add-port=8001/tcp --permanent
[root@root ~]# firewall-cmd --zone=public --add-port=8800/tcp --permanent
[root@root ~]# firewall-cmd --zone=public --add-port=9000/tcp --permanent
[root@root ~]# service firewalld restart   #重启防火墙,不然不生效

[root@root ~]# firewall-cmd --list-ports   #查看开放的端口
80/tcp 8000/tcp 8001/tcp 8800/tcp 9000/tcp



三、四种动态开发语言读取mysql数据

1. Go

这个博客写的很详细:Linux实验:Golang+MySQL部署Web环境
只要将博客里的9090端口改为8000就可以了。

2. nodejs

2.1 安装Nodejs

[root@root ~]# sudo yum install epel-release
[root@root ~]# sudo yum install nodejs
[root@root ~]# node --version
#安装npm
[root@root ~]# cd /root/myLinuxTest/nodejs
[root@root nodejs]# sudo yum install npm --enablerepo=epel
[root@root nodejs]# sudo npm install -g express
[root@root nodejs]# sudo npm install -g express-generator
[root@root nodejs]# npm install mysql

2.2 实现用nodejs读取mysql并显示在网页上

[root@root ~]# vim /root/myLinuxTest/nodejs/index.js
var http=require('http');
var url=require('url');
var mysql=require('mysql');

//创建连接
var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'xxxxxx',
        port     : '3306',
        database : 'test'
});

var sql = 'select * from student';
server = http.createServer(function(request,response) {
    response.writeHead(200,{'Content-Type': 'text/html'});

    connection.query(sql,function(err,result){
        if(err){
                console.log('[select error] -',err.message);
                return;
        }
    response.write(
        '<!DOCTYPE html>'+
        '<html lang="en">'+
        '<head>'+
        '<meta charset="utf-8"/>'+
        '</head>'); 
        response.write("这是Nodejs页面");
        response.write("<table border='1'><tr>");
        response.write("<th>学号</th>");
        response.write("<th>姓名</th></tr>");
        result.forEach(r =>{
                response.write("<tr>");
                response.write("<td>"+r.stuId+"</td>");
                response.write("<td>"+r.stuName+"</td>");
                response.write("</tr>");
                })
     response.end();
        });
});
server.listen(8001);

2.3 nginx配置

[root@wu2 ~]# vim /etc/nginx/nginx.conf	# 增加下方配置。
        location ~/index.js$ {
                proxy_pass   http://127.0.0.1:8001;
        }
        
[root@root ~]# service nginx restart

此时,可以用 http://ip/index.js 访问。

使nodejs项目在服务器上后台运行

[root@root ~]# nohup node /root/myLinuxTest/nodejs/index.js &

3.python

3.1 在pycharm中建flask项目

在这里插入图片描述

3.2 在项目中建立与云服务器连接

在这里插入图片描述
在这里插入图片描述

随便定义一个名字
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
ok~然后
在这里插入图片描述
这样就设置好啦,这里能远程看到linux
在这里插入图片描述
在这里插入图片描述

3.3 实现用python读取mysql并显示在网页上

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from flask import Flask
app = Flask(__name__)
import mysql.connector

@app.route('/')
def hello_world():  # put application's code here
    mydb = mysql.connector.connect(
        host="localhost",
        user="用户名",
        passwd="密码",
        database="数据库名"
    )
    mycursor = mydb.cursor()
    result = mycursor.execute("select * from 表名")
    str = "<h4>这是python页面<h4><table border='1'><tr><th>学号</th><th>姓名</th></tr>"
    for x in mycursor:
        str += "<tr><td>" + x[0] + "</td><td>" + x[1] + "</td></tr>"
    str += "</table>"
    return str

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8800, debug=True)

这里将项目文件上传到linux上
在这里插入图片描述
访问http://ip:8800/即可

使python项目在服务器上后台运行

[root@root python]# python -V
Python 2.7.5
[root@root python]# nohup python2.7 -u /root/python/app.py &

4.php

4.1 php的安装配置

[root@root ~]# mkdir /opt/php
[root@root ~]# cd /opt/php
[root@root php]# wget https://www.php.net/distributions/php-7.4.5.tar.gz
[root@root php]# yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libsqlite3x-devel oniguruma-devel
[root@root php]# tar -zxvf php-7.4.5.tar.gz
[root@root php]# cd php-7.4.5
[root@root php-7.4.5]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --enable-mbstring --enable-ftp --enable-gd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pear --disable-phar --enable-sockets --with-zlib --with-xmlrpc --enable-fpm --enable-xml --enable-sockets --with-zlib --with-iconv --enable-soap --enable-pcntl --enable-cli --with-curl
[root@root php-7.4.5]# make
[root@root php-7.4.5]# make install
[root@root php-7.4.5]# cp php.ini-production /usr/local/php/php.ini
[root@root php-7.4.5]# vim /usr/local/php/php.ini
display_errors = On	# Off改为On。设置让PHP错误信息打印在页面上

[root@root php-7.4.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@root php-7.4.5]# chmod +x /etc/init.d/php-fpm
[root@root php-7.4.5]# cd /usr/local/php/etc
[root@root etc]# cp php-fpm.conf.default php-fpm.conf
[root@root etc]# vi php-fpm.conf
 去掉  ";pid = run/php-fpm.pid"  前面的分号

[root@root etc]# cd php-fpm.d
[root@root php-fpm.d]# cp www.conf.default www.conf
[root@root php-fpm.d]# vi www.conf	# 修改user和group为php。
user = php
group = php

[root@root php-fpm.d]# groupadd php
[root@root php-fpm.d]# useradd -r -g php php
[root@root php-fpm.d]# chkconfig php-fpm on	# 设置开启启动
[root@root php-fpm.d]# cd ~
[root@root ~]# /etc/init.d/php-fpm star

4.2 配置Nginx,测试访问PHP

[root@root ~]# vim /usr/share/nginx/html/index.php	# 写入如下内容
<?php
    phpinfo();
?>

[root@root ~]# vim /etc/nginx/nginx.conf	
        #添加以下内容
        location / {
                root /usr/share/nginx/html;
                index index.php;
        }
        location ~ \.php$ {
                #root           html;
                root   /usr/share/nginx/html;
                fastcgi_pass   127.0.0.1:9000;  #php-fpm默认的端口是9000
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
         }
         
[root@wu2 ~]# service nginx restart

配置后,使用ip云服务器公网ip/index.php可访问以下页面
在这里插入图片描述
ip/index.html 可以访问到下方页面。
在这里插入图片描述

4.4 实现用PHP读取mysql并显示在网页上

[root@root ~]# vim /usr/share/nginx/html/index.php	# 删除原先内容,改为以下内容。
<?php
$mysql_server_name = '127.0.0.1'; // ip
$mysql_username = 'root';         // username
$mysql_password = '66666'; // 设置你的数据库密码
$mysql_database = 'test';   // 设置你的数据库名

$conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //连接数据库
if ($conn -> connect_errno) {
    printf("Connect failed: %s\n", $conn->connect_error);
    exit();
}
//查询
$sql = "select * from student";
$query = $conn->query($sql);

echo "这是PHP页面";
//构造表头
echo "<table border='1'><tr>";
echo "<th>学号</th>";
echo "<th>姓名</th>";
echo "</tr>";
//遍历输出
while($row = $query->fetch_array()){
	echo "<tr>";
	echo "<td>".$row['stuId']."</td>";   //stuId和stuName要和你表里的字段名一致
	echo "<td>".$row['stuName']."</td>";  
	echo "</tr>";
}
//释放结果集+关闭MySQL连接
$query -> free_result();
$conn -> close();
?>

保存后访问http://ip/index.php,

使php项目在服务器上后台运行

[root@root ~]# nohup php /usr/share/nginx/html/index.php &
  • 3
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值