在Termux搭建Web伺服器環境,以下部分記錄Ngnix,MariaDB,Nodejs安裝與配置過程
安裝Nginx
$ pkg install nginx
# nginx版本
$ nginx -v
nginx version: nginx/1.24.0
# 測試nginx配置檔案
$ nginx -t
nginx: the configuration file /data/data/com.termux/files/usr/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /data/data/com.termux/files/usr/etc/nginx/nginx.conf test is successful
運作nginx并在浏覽器中檢視測試網站`Welcoe to nginx!`
$ nginx
$ pgrep nginx
11866
11867
手機上通路`http://127.0.0.1:8080` 或者使用手機IP+端口号通路
$ ifconfig wlan0
nginx基礎指令
# 重新開機
$ nginx -s reload
# 關閉
$ nginx -s stop
$ nginx -s quit
# 強制殺程序
$ kill -9 `pgrep nginx`
安裝MariaDB
$ pkg install mariadb
$ mysql --version
mysql Ver 15.1 Distrib 10.11.2-MariaDB, for Android (aarch64) using EditLine wrapper
安裝完成的歡迎提示
To start mariadbd at boot time you have to copy
support-files/mariadb.service to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following command:
'/data/data/com.termux/files/usr/bin/mariadb-secure-installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at https://mariadb.com/kb
You can start the MariaDB daemon with:
cd '/data/data/com.termux/files/usr' ; /data/data/com.termux/files/usr/bin/mariadb-safe --datadir='/data/data/com.termux/files/usr/var/lib/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/data/data/com.termux/files/usr/share/mysql/mysql-test' ; perl mariadb-test-run.pl
Please report any problems at https://mariadb.org/jira
The latest information about MariaDB is available at https://mariadb.org/.
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
登入root賬号并設定root密碼
# 登入mysql并查詢目前登入使用者
$ mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.11.2-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.002 sec)
# 切換資料表
MariaDB [(none)]> 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
# 設定root密碼
MariaDB [mysql]> set password for 'root'@'localhost' = password('000000');
Query OK, 0 rows affected (0.095 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.005 sec)
MariaDB [mysql]> quit;
Bye
之後使用密碼登入,設定MySQL遠端登入
$ mysql -u root -p
MariaDB [(none)]> grant all on *.* to root@'%' identified by '000000' with grant option;
Query OK, 0 rows affected (0.083 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.005 sec)
沒有意外的話,使用本機上的資料庫通路軟體即可登入
啟動與停止MySQL服務
# 背景啟動mysql服務
$ nohup mysqld &
[1] 12192
nohup: ignoring input and appending output to 'nohup.out'
# 檢視mysql服務
$ ps aux|grep mysql
u0_a117 12192 0.4 3.1 521652 89296 pts/1 S<l 1970 0:00 mysqld
u0_a117 12224 1.0 0.0 9372 1588 pts/1 S<+ 1970 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox mysql
# 停止mysql服務
$ kill -9 `pgrep mysql`
[1] + 12192 killed nohup mysqld
安裝Nodejs
$ pkg install nodejs-lts
$ npm -v
9.3.1
$ node -v
v18.14.1
測試安裝Hexo并釋出到Nginx
# 設定國内鏡像加快通路速度
$ npm config set registry http://registry.npmmirror.com
# 全局安裝Hexo
$ npm install hexo-cli -g
$ hexo -v
hexo-cli: 4.3.0
os: android 3.10.73-ge8c57cd
node: 18.14.1
v8: 10.2.154.23-node.22
uv: 1.44.2
zlib: 1.2.13
brotli: 1.0.9
ares: 1.19.0
modules: 108
nghttp2: 1.51.0
napi: 8
llhttp: 6.0.10
uvwasi: 0.0.14
acorn: 8.8.1
simdutf: 3.1.0
undici: 5.19.1
openssl: 3.1.0
cldr: 43.0
icu: 73.1
tz: 2023c
unicode: 15.0
使用Hexo搭建口袋部落格
# 建立目錄
$ mkdir myblog && cd myblog
$ hexo init
# 生成靜态檔案 啟動Hexo
$ hexo g
$ hexo s
部署Hexo 到 Nginx
# 生成靜态檔案
$ hexo g
# 在 nginx 根目錄下建立 myblog 檔案夾
$ mkdir $PREFIX/share/nginx/html/myblog
# 拷貝 源碼到 nginx 下
$ cp -rf public/* $PREFIX/share/nginx/html/myblog
手機上通路`http://127.0.0.1:8080/myblog/` 或者電腦端打開