在Debian上安装LANMP及在Win10下安装MariaDB的zip版本
一、安装Apache
(一)、安装apache命令:
| 1 | sudo apt install apache2` | 
Apache安装完成后,安装程序将立即触发systemd系统和服务管理器启动Apache2服务,并使其在系统引导时自动启动。
要检查Apache服务是否已启动并正常运行,请运行以下systemctl命令。# systemctl status apache2
其他常用命令1
2
3
4
5# systemctl start apache2.service
# systemctl restart apache2.service
# systemctl stop apache2.service
# systemctl reload apache2.service
# systemctl status apache2.service
(二)、修改apache网站服务路径为自己网站目录
(1)修改 /etc/apache2/sites-available/000-default.conf
不建议直接修改,而是新建网站配置文件并监听8080端口:
命令:1
sudo vim /etc/apache2/sites-available/wpweb.conf
添加以下内容:1
2
3
4
5
6
7<VirtualHost *:8080>  
  #ServerName www.example.com  
  ServerAdmin webmaster@localhost  
  DocumentRoot ~/www/html/php-web/  
  ErrorLog ~/www/html/php-web/wordpress/logs/error.log  
  CustomLog ~/www/html/php-web/wordpress/logs/access.log combined  
</VirtualHost>  
可以将网站配置命名为:网站名.conf
(2)###修改 /etc/apache2/apache2.conf
[不建议直接修改]
将 <Directory /var/www/> 改为 <Directory 你的路径>1
2
3
4
5####<Directory /var/www/>
#####Options Indexes FollowSymLinks
#####AllowOverride None
#####Require all granted
######</Directory>
1》在ports.conf添加新增监听端口
命令:1
sudo vim /etc/apache2/ports.conf
在Linsten下添加以下内容:Listen 8080  
2》新建网站的日子文件夹:
命令:1
sudo mkdir -p ~/www/html/php-web/wordpress/logs/
3》启用配置:
命令:1
sudo a2ensite wpweb.conf
(三)安装libapache2-mod-fastcgi模块启用php-fpm管理
(1)添加安装源:
命令:1
sudo vim /etc/apt/sources.list
添加源地址:deb http://ftp.cn.debian.org/debian buster main non-free
根据速度可以选择网页各地区地址。
(2)安装fastcgi模块和php7.4支持模块:
1》安装模块及添加模块支持命令:
| 1 | sudo apt update | 
添加以下内容:1
2
3
4
5
6<IfModule mod_fastcgi.c>
   AddHandler php7.4-fcgi .php
   Action php7.4-fcgi /php7.4-fcgi
   Alias /php7.4-fcgi /usr/lib/cgi-bin/php7.4-fcgi
   FastCgiExternalServer /usr/lib/cgi-bin/php7.4-fcgi -socket /var/run/php/php7.4-fpm.sock -pass-header Authorization
</IfModule>
2》检查用户组
查看/etc/php/7.4/fpm/pool.d/www.conf 文件中用户和用户组是否为www-data。
不是就改为该用户名和用户组,否则可能会出现权限问题。
(四)验证配置:
命令:1
2sudo apache2ctl configtest
Syntax OK
(五)重启apache服务器
| 1 | sudo service apache2 restart | 
可能的问题:
403 Forbiden 或者 You don't have permission to access / on this server
修改你的路径目录权限为可读写
(1)chown -R www-data:www-data  网站目录名
(2)修改apache2.conf配置文件;将 <Directory />中的”Require all denied“修改成”Require all granted“;
最后重启apache2
常用命令:1
2
3
4
5sudo chown www-data:www-data ~/www/html/php-web/ -R #修改网站根目录所有权限改为Apache进程用户
sudo apache2ctl configtest #测试Apache2配置
sudo a2ensite wordpress.conf #启用配置
sudo a2dissite wordpress.conf #取消配置
a2enmod rewrite  #启用伪静态重写url功能
[关于为静态可能出现无反映或读取.htaccess的问题]sudo vim /etc/apache2/apache2.conf
将其中的:AllowOverride None
修改为:AllowOverride All
上面的 /etc/apache2/sites-enabled/000-default 实则为 /etc/apache2/sites-available/default 的连接。
而 AllowwOverride None 则会完全忽略 .htaccess 文件,自然其中所定义的 rewrite 规则也就不能生效了。  
其他内容可参考:
Install PHP-FPM and Apache on Debian 8
debian下为apache启用rewrite模块
二、安装nginx官网稳定版[nginx1.16.1]并配置和apache2共存
(一)更新源并安装nginx
命令:
sudo apt install curl gnupg2 ca-certificates lsb-release   #安装必要的支持包
打开安装源列表:1
sudo vim /etc/apt/sources.list
添加以下内容:deb http://nginx.org/packages/debian/ buster nginx #安装稳定版本
或者有安装直接输入以下命令:1
sudo echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
上述命令将直接在/etc/apt/sources.list.d下新建nginx安装源1
2
3sudo curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -   #添加nginx的key到系统
sudo apt update && sudo apt upgrade
sudo apt install nginx
因80端口被先安装的apache2占用,安装完启动会提示错误,
修改/etc/nginx/nginx/conf.d/default.conf的默认端口
命令:1
sudo vim /etc/nginx/conf.d/default.conf
修改:listen 80 default_server;为:listen 8090 default_server;
修改nginx目录下nginx.conf
第二行user  nginx;改为:user  www-data;
因为前面apache2的设置中php7.4-fpm的用户和用户组为www-data。
文件位于:/etc/php/7.4/fpm/pool.d/www.conf件中用户和用户组为www-data。
(二)添加网站配置:
拷贝一份配置到conf.d文件夹:
命令:1
sudo cp /etc/nginx/nginx.conf /etc/nginx/conf.d/nginxsite1.conf
修改nginxsite1.conf的配置:
命令:1
sudo vim /etc/nginx/conf.d/nginxsite1.conf
删除全部内容,替换为以下内容:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43server {
    listen       8091;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   ~/www/html/nginx-web;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           ~/www/html/nginx-web/;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;  
    #   # With php-cgi (or other tcp sockets):
    #    fastcgi_pass 127.0.0.1:9000;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
(三)重启服务器
命令:1
2
3sudo nginx -t #测试配置是否正确
sudosystemctl start/restart nginx  #启动/重启服务器
sudosystemctl stop/status nginx  #停止服务器/查看服务器状态
测试Nginx安装结果
打开浏览器,输入 :http://localhost:8090/index.nginx-debian.html
其中index.nginx-debian.html为nginx在debian的默认首页
出现:“Welcome to nginx!”则安装成功。
测试PHP网页
新建php网页,输入如下内容:1
2
3
phpinfo();
保存并命名为:info.php
打开浏览器,输入 :http://localhost:8091/info.php
出现php信息页面,则安装完成。
nginx加入到systemctl服务运行
| 1 | $ sudo vim /usr/lib/systemd/system/nginx.service | 
输入以下内容并保存1
2
3
4
5
6
7
8
9
10
11
12[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/etc/nginx/logs/nginx.pid
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/etc/nginx/sbin/nginx -s reload
ExecStop=/etc/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载服务配置:1
2sudo systemctl daemon-reload  # 重新加载服务配置文件
sudo systemctl start/stop/reload nginx #开启、停止、重新加载nginx
可能的一些情况
1、若因为因为安装源造成版本升级错误:
如出现:dpkg-deb: 子进程 粘贴 被信号(断开的管道) 终止了 导致无法继续安装,这个问题可以使用dpkg强制覆盖解决。
找到安装包位置 cd /var/cache/apt/archives 之后强制安装 dpkg -i --force-overwrite xxx.deb即可。
2、若出现打开php页面空白,请检查配置(nginxsite1.conf)中网站路径是否正确。
3、一些命令[在nginx1.16.1中未用到]1
2
3
4
5
6sudo ln -s /etc/nginx/sites-available/nginxsite1 /etc/nginx/sites-enabled/nginxsite1  #建立软链接
sudo dpkg --get-selections | grep nginx   // 查看剩余的与nginx相关的软件包
sudo apt --purge remove nginx           // 卸载nginx(不保留配置文件)
sudo apt autoremove                     // 卸载自动安装的软件包
sudo apt install net-tools               //安装网络配置工具
sudo netstat -nlp |grep 端口号           //查询端口占用情况
更多内容参考:Installation instructions for debian
三、安装PHP
安装首要条件
使用shell访问权限登录Debian10系统,对于远程系统,使用SSH连接。Windows用户可以使用Putty或其他替代应用程序进行SSH连接。
(一)运行以下命令将当前包升级到最新版本。
| 1 | sudo apt update && sudo apt upgrade | 
(二)在Debian10上设置PPA
分别执行以下命令,首先在系统上安装所需的软件包。然后导入包签名密钥。之后,为系统上的PHP包配置PPA。1
2
3
4
5sudo apt -y install software-properties-common apt-transport-https lsb-release ca-certificates
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
sudo echo "deb https://packages.sury.org/php/ buster main" | tee /etc/apt/sources.list.d/php.list
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://mirror.xtom.com.hk/sury/php/apt.gpg
sudo echo "deb https://mirror.xtom.com.hk/sury/php/ buster main" | tee /etc/apt/sources.list.d/php.list
若提示tee: /etc/apt/sources.list.d/php.list: 权限不够1
2deb https://packages.sury.org/php/ buster main`
deb https://mirror.xtom.com.hk/sury/php/ buster main
可用命令:1
sudo vim /etc/apt/sources.list.d/php.list
手动创建添加php安装源:“deb https://packages.sury.org/php/ buster main”Debian10系统已经准备好安装PHP了。
(三)在Debian10上安装稳定版PHP7.4
还可以在系统上安装稳定的PHP版本。运行以下命令在Debian10安装php7.4。1
2sudo apt update
sudo apt install php7.4
同时安装所需的PHP模块。1
sudo apt install php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-mysql php7.4-xml
安装后,使用以下命令验证已安装和运行的PHP版本
命令:1
php -v
(四)切换php版本为默认用于Apache和命令行(可多版本PHP共存)
| 1 | sudo a2dismod php7.3 | 
PHP最新版PHP8.1.0已经于2021年11月25日发布,源安装可参照如上方法,注意对应版本号,其编译安装可参考:
Debian11编译安装PHP8.1.0版
(五)安装imagick
1. 依次运行以下命令
| 1 | sudo apt install imagemagick | 
安装php-pear是为了使用pecl 中的 phpizesudo apt install php7.4-dev
2. 安装C语言编译器
sudo apt install gcc
如果装不了gcc就用这个命令:sudo apt install gcc gcc- c++ autoconf automake
3. 安装imagick
sudo pecl install imagick
4. 加载imagick到php
在/etc/php/7.4/apache2/php.ini 加上extension=imagick.so  
5. 重启service httpd restart
6. 查看安装结果
使用 phpinfo() 或运行 php -m | grep imagick 来查看是否安装成功
其他操作:
停止PHP:sudo pkill php-fpm
查看9000端口:netstat -lnt | grep 9000  
启动PHP:如果修改过配置文件,一定要重新加载配置文件才行/usr/sbin/php-fpm7.4 -c /etc/php/7.4/apache2/php.ini
查看9000端口:1
2netstat -tunlp | grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN
参考网站:linux下 安装 ImageMagick 及其 php imagick扩展
其他安装源:1
2wget https://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg
源:1
2deb http://packages.dotdeb.org buster all
deb-src http://packages.dotdeb.org buster all
四、在Debian 10上安装MariaDB
Apache Web服务器启动并运行后,您需要安装数据库系统才能保留和管理您网站的数据。
(一)要安装MariaDB,请使用Debian的apt包管理器。
sudo apt install mariadb-server
(二)安装MariaDB后,建议运行以下安全脚本,
该脚本将删除一些不安全的默认设置并禁用对数据库系统的访问。
命令:sudo mysql_secure_installation
上述安全脚本将引导您完成以下一系列问题,您可以在其中对MariaDB设置进行一些更改。
均选择:y
若提示:ERROR 1698 (28000): Access denied for user 'root'@'localhost'
请用管理员权限重新运行上述命令。
(三)如果要创建名为wpDB的数据库和用户名和密码均为wpadmin且具有数据库完全权限的用户,请运行以下命令。
命令:1
2
3
4
5sudo mysql -u root -p××××××
  MariaDB [(none)]> CREATE DATABASE wpDB;
  MariaDB [(none)]> grant all on wpDB.* to 'wpadmin'@'localhost' identified by 'wpadmin' with grant option;
  MariaDB [(none)]> FLUSH PRIVILEGES;
  MariaDB [(none)]> exit;
您可以通过使用用户凭据登录MariaDB来确认新用户是否具有数据库的完全权限,如下所示。
命令:1
2mysql -uwpadmin -pwpadmin
  MariaDB [(none)]> SHOW DATABASES;
五、在windows下安装madiadb数据库zip版本
1. 下载
mariadb下载地址: 选择windows和zip
2. 解压
比如:解压到D:\mariadb。
3. 服务安装
需要在windows中的安装一个名字为MariaDB的服务:
在d盘下新建一个data的文件夹。
用户管理员身份启动powershell。
键入如下命令:D:\mariadb\bin> .\mysql_install_db.exe --datadir=D:\data --service=MariaDB --password=123
【tip: --datadir= 如果指定不同的路径,配合my.ini文件设置不同的端口,即可以完成多个MariaDB的服务实例。】
服务启动:
方法一:win+R—>运行services.msc,然后在服务列表中找到MariaDB,点击运行。
方法二:在《管理员的POWERSHELL》中,运行net start MariaDB
客户端接入:
 .\mariadb.exe -uroot -P3306 -p
4. 注意:可以修改端口的方式:
在D:\data\my.ini文件中,加入port=xxxx1
2
3
4
5[mysqld]
datadir=D:/data
port=3306
[client]
plugin-dir=D:/mariadb/lib/plugin
win10中MariaDB的zip压缩版的安装和使用(多服务实例)
六、安装wordpress
[需科学工具,否则打开网页可能会出现:429 Too Many Requests错误]
(一)下载
下载地址:
下载后解压到网站目录
(二)安装
在数据库中建立网站数据库,后面依照安装指引填写就可以了。
注意事项:
1、若安装为英文版后要切换中文版需要在:wp-config.php文件中添加以下内容:
 在设置中切换为中文define('WPLANG','zh_CN');
取消切换为中文后升级后本地网站需要填写FTP信息提示1
2
3define("FS_METHOD","direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
2、升级中若出现无法写入目录文件,则需要对网站目录授权[apache2运行的www-data用户权限]:
命令:1
sudo chown -R www-data:www-data  网站目录名
[授予该权限也能解决需要FTP信息的提示]
3、解决更新提示另一更新存在的问题1
2
3
4sudo mysql -u wpadmin -pwpadmin
MariaDB [(none)]> show databases
MariaDB [(none)]> use wpDB;
MariaDB [wpDB]> delete from wp_options where option_name='core_updater.lock';
(三)若更换端口需更改数据库中wp_options中的端口配置
命令:1
2MariaDB [wpDBtest]> update wp_options set option_value= "http://localhost:8090/wordpress" where option_name in("siteurl","home");
MariaDB [wpDBtest]> select * from wp_options where option_name in("siteurl","home");  //查看修改情况