It has been 1485 days since the last update, the content of the article may be outdated.

在接触PHP以前,我一直使用的是Python-Django-Sqlite,Java-Springboot-Mysql这些选型来构建个性化的网页应用。
个人博客从Pelican到Vuepress再到Hexo这些静态工具,也使用过Gridea。现在又发现了基于PHP的框架Typecho,奔赴科研理想之余玩玩新东西。
看上了💖Brave💖这一款主题,打算用一天搭起来。没有PHP基础,但是建站什么的还有些经验。
打算直接上阿里云ECS服务器实例。毕竟今年博士入学,学生认证走起。

阿里云ECS实例

春节当天在阿里云上购置了一台ECS实例,链接如下开发者成长计划

aliyun-ECS
aliyun-ECS

其实阿里云提供应用镜像的选择,包括WordPress和LAMP等有选择,因为我可能用于比较多,就选了ubuntu20.04的系统镜像,可能搭建环境方面需要更多工作。
aliyun-ecs-panel
aliyun-ecs-panel

安装LAMP环境

LAMP= Linux + Apache + Mysql + PHP,从系统,http服务器,数据库到开发环境的一整套。

Typecho的环境要求:

  1. PHP 5.4 以上
  2. MySQL, PostgreSQL, SQLite 任意一种数据库支持,并在 PHP 中安装了相关扩展
  3. CURL 扩展支持
  4. mbstring 或 iconv 扩展支持

安装Apache2

plaintext
sudo apt update
sudo apt install apache2

开启apache服务,并查看服务状态:

plaintext
sudo /etc/init.d/apache2 start 
Starting apache2 (via systemctl): apache2.service.

ps aux | grep apache
root 22813 0.0 0.2 6520 4472 ? Ss 16:24 0:00 /usr/sbin/apache2 -k start
www-data 22815 0.0 0.2 752648 4484 ? Sl 16:24 0:00 /usr/sbin/apache2 -k start
www-data 22816 0.0 0.2 752648 4484 ? Sl 16:24 0:00 /usr/sbin/apache2 -k start
root 23544 0.0 0.0 9032 732 pts/0 S+ 16:25 0:00 grep --color=auto apache
plaintext
sudo apache2ctl configtest 
Syntax OK
sudo systemctl status apache2

显示active说明服务正常
apache-status

apache-status

登陆浏览器地址,公网ip地址 http://公网ip,默认端口80。显示如下页面

apache-active

apache-active


安装Mysql

安装Mysql服务和客户端,两行代码,比windows下简单多了。

plaintext
sudo apt install mysql-server
sudo apt install mysql-client

查看mysql

plaintext
#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \s
--------------
mysql Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

Connection id: 8
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 1 min 10 sec

Threads: 2 Questions: 5 Slow queries: 0 Opens: 117 Flush tables: 3 Open tables: 36 Queries per second avg: 0.071

字符类型全部是utf8mb4,支持Emoji类型。


安装PHP

plaintext
sudo apt install php
sudo apt install php-mysql

当前安装的版本是php7.4
同时安装到位的库还有libapache2-mod-php7.4 php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
php-mysql 会自动下载配套的最新稳定版的php-mysql php7.4-mysql

安装php-xml,

plaintext
sudo spt install php-xml

安装的库有libxslt1.1 php-xml php7.4-xml.

检验

plaintext
root@iZbp15zw0q0jnshf9chedxZ:/home/hjh# php -a
Interactive mode enabled
php > var_dump(function_exists('utf8_decode'));
bool(true)

重启服务

plaintext
root@iZbp15zw0q0jnshf9chedxZ:/home/hjh# service apache2 restart
root@iZbp15zw0q0jnshf9chedxZ:/home/hjh# service mysql restart

测试Apache能否解析PHP

plaintext
root@iZbp15zw0q0jnshf9chedxZ:/home/hjh# echo '<?php echo phpinfo();?>' >/var/www/html/phpinfo.php

浏览器访问 http://公网ip/phpinfo.php,应该有如下PHP信息。
aliyun-ECS

aliyun-ECS

编辑/etc/apache2/apache2.conf配置文件:

plaintext
# Do NOT add a slash at the end of the directory path.
#ServerRoot "/etc/apache2"
###加上如下两行
AddType application/x-httpd-php .php .htm .html
AddDefaultCharset UTF-8