LAMP

Linux + Apache + Mysql + PHP

#安装 Apache

#安装 Apache

sudo apt update
sudo apt install apache2

#设置全局 Server Name 消除警告

使用下面的语句检查配置时会有警告信息:

sudo apache2ctl configtest

打开 apache2.conf 文件:

sudo vim /etc/apache2/apache2.conf

在文件的最后一行添加:

ServerName server_domain_or_public_IP

测试是否还有警告信息:

sudo apache2ctl configtest

重启Apache使修改生效:

sudo systemctl restart apache2

#调整防火墙

确保 ufw 有 Apache 的应用配置文件,能够看到 Apache, Apache Full, Apache Secure:

sudo ufw app list

如果查看 Apache Full 的应用配置文件,应该看到它允许到 80 和 443 端口流量:

sudo ufw app info "Apache Full"

允许进入流量:

sudo ufw allow in "Apache Full"

测试,如果可以看到默认页面则成功:

http://your_server_IP_address

#安装 MySQL

sudo apt update
sudo apt install mysql-server

安装完成后,运行一个简单的安全脚本,它将删除一些危险的默认值并锁定对数据库系统的访问,运行以下命令启动交互式脚本:

mysql_secure_installation

登录数据库:

mysql -u root -p

#安装 PHP

#安装 PHP

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

(可选)改变客户端从服务器查找文件顺序:

sudo vim /etc/apache2/mods-enabled/dir.conf

原:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

改为:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

重启 Apache 服务生效:

sudo systemctl restart apache2

使用下面的命令可以查看 Apache 状态:

sudo systemctl status apache2 

安装额外模块:

分页查看 PHP 模块和库的可用选项,使用箭头键向上和向下滚动,按 q 退出

apt-cache search php- | less

查看某个包的长描述

apt-cache show package_name

安装某个(些)包:

apt install package_name package_name2 package_name3 ...

#测试 PHP 处理

sudo vim /var/www/html/info.php

在文件中写一段 PHP 代码:

1
2
3
<?php
phpinfo();
?>

访问:

http://your_server_IP_address/info.php

删除测试文件:

sudo rm /var/www/html/info.php

#使用 Let 加密保护 Apache

#安装 Let 的加密客户端

sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-apache

#设置 SSL 证书

对于单个域

sudo certbot --apache -d example.com

对于多个域,第一个参数为基本域,后面的为子域名或者别名

sudo certbot --apache -d example.com -d www.example.com

生成的证书文件位置 /etc/letsencrypt/live。链接验证 SSL 证书的状态(不要忘记将 example.com 替换为您的基本域)

https://www.ssllabs.com/ssltest/analyze.html?d=example.com&latest

#自动续订

加密证书只有 90 天有效期,之后需要续订。下面是自动续订

sudo certbot renew --dry-run

#参考

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

How To Secure Apache with Let's Encrypt on Ubuntu 16.04

updatedupdated2022-05-032022-05-03