Refers:
- https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-22-04
- https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04
安装 Apache 更新防火墙
|
|
输出:
Available applications: Apache Apache Full Apache Secure OpenSSH
|
|
输出:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
现在可以通过 IP 访问初始页面了。
找服务器的公网 IP 地址:
|
|
如果有域名绑定到这个 IP 上,也可以用域名 =http://example.com:80=。
安装 MySQL 和 PHP
|
|
第一步点击 Y,接下来根据情况选择。我进行到这几步时,密码验证总是通不过。但也能进入 Mysql shell 里。
|
|
your_domain.conf
文件内容:
<VirtualHost *:80> ServerName your_domain ServerAlias www.your_domain # 如果只有一个域名,可用 # 注释掉 ServerAdmin webmaster@localhost DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
|
|
注意:默认情况下,index.html 比 index.php 的优先级高。如果想反过来,可进行如下修改:
|
|
修改后的结果:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
|
|
=info.php=:
<?php phpinfo();
访问 =http://server_domain_or_IP/info.php=,会出现一个页面描述 php 的配置信息。
|
|
输出:
+--------------------+ | Database | +--------------------+ | example_database | | information_schema | +--------------------+ 2 rows in set (0.000 sec)
|
|
输出:
+---------+--------------------------+ | item_id | content | +---------+--------------------------+ | 1 | My first important item | +---------+--------------------------+ 4 rows in set (0.000 sec)
|
|
=todo_list.php=:
<?php $user = "example_user"; $password = "password"; $database = "example_database"; $table = "todo_list"; try { $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password); echo "<h2>TODO</h2><ol>"; foreach($db->query("SELECT content FROM $table") as $row) { echo "<li>" . $row['content'] . "</li>"; } echo "</ol>"; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }
访问 =http://your_domain_or_IP/todo_list.php=。
开启 HTTPS
|
|
输出:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
|
|
输出:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache Full ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache Full (v6) ALLOW Anywhere (v6)
|
|