一、前期准备

1.更新源

apt-get -y update
apt-get -y upgrade

2.安装vi

apt-get -y install vi

3.安装ssl证书

sudo apt -y install apt-transport-https ca-certificates

4.修改源文件权限

chmod 777 /etc/apt/sources.list

5.修改国内源:

deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

二、PHP部分

1.先更新

apt-get -y update

2.安装php

apt-get -y install php
apt install -y php-fpm php-curl php-gd php-intl php-mbstring php-mysql php-imap php-opcache php-sqlite3 php-xml php-xmlrpc php-zip

3.查看php版本信息

php -v

4.查看配置文件

php -i|grep "Loaded Configuration File"

输出显示配置文件在/etc/php/7.3/cli/php.ini。注意,实际上配置文件有两个,另外一个在/etc/php/7.3/fpm/php.ini。通过命令行调用php时,会使用第一个配置文件;通过fpm调用php(例如nginx)会使用第二个配置文件。

php-fpm常用管理命令: 开启php-fpm: sudo systemctl start php7.3-fpm 关闭php-fpm: sudo systemctl stop php7.3-fpm 重启php-fpm: sudo systemctl restart php7.3-fpm 编辑php-fpm配置文件: vi /etc/php/7.3/fpm/php-fpm.ini

三、Nginx部分

1.安装Nginx

apt-get -y install nginx

2.nginx相关命令

启动nginx:systemctl start nginx 
关闭nginx:systemctl stop nginx 
重启nginx:systemctl restart nginx
设置nginx开机启动:systemctl enable nginx
取消开机自启动:sudo systemctl disable nginx

配置nginx解析php

1.编辑nginx文件

vi  /etc/nginx/sites-enabled/default

2.修改一下内容

index index.php index.html index.htm index.nginx-debian.html;

location ~ \.php$ { 
include snippets/fastcgi-php.conf; 
# 
# # With php-fpm (or other unix sockets): 
fastcgi_pass unix:/run/php/php7.3-fpm.sock; 
# # With php-cgi (or other tcp sockets): 
#fastcgi_pass 127.0.0.1:9000; 
}

或者

location ~ \.php$ {
        root           html;
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

3.重启nginx

systemctl restart nginx

四、测试Nginx+PHP

1.新建index.php文件

vi /var/www/html/index.php

2.编辑输入

<?php
phpinfo();

3.Nginx启动问题

启动nginx服务时如果遇到这个错误 Job for nginx.service failed because the control process exited with error code. See “systemctl stat 可能原因如下: 1.nginx配置文件有错误

1)运行下面命令查看修改

nginx -t

之前配置解析php时始终不能成功,只因为default文件最后一行缺少一个“}”

2)已经启动nginx或者配置文件中的端口号被占用,检查端口是否被占用

netstat -tnlp

如果端口已经被占用,自己权衡一下是换个端口还是把占用端口的进程杀掉,检查nginx是否已经启动

ps -aux | grep nginx

3)如果已经启动使用下面命令干掉即可

pkill -9 nginx

五、安装MySQL

1.这里使用的是MySql分支mariadb:

apt-get -y install mariadb-server mariadb-client

2.执行数据库初始化安装

mysql_secure_installation

3.根据提示设置数据库root用户密码、是否允许外网访问等。

4.开启远程访问

1)编辑配置文件

vi /etc/mysql/mariadb.conf.d/50-server.cnf 

2)修改以下内容

 将bind-address这行注释掉 
 或者将127.0.0.1 这个值改为  0.0.0.0 

3)然后重启

sudo /etc/init.d/mysql restart

5.相关命令

状态:systemctl status mariadb
停止:systemctl stop mariadb
启动:systemctl start mariadb
重启:systemctl restart mariadb

六、安装Mysql数据库可视化操作工具phpmyadmin

1.安装PHPMyAdmin

apt-get install -y phpmyadmin

设置数据库phpmyadmin的密码,,然后确认密码,等待安装完成

2.将phpmyadmin默认安装信息链接到Web网站根目录

ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

3.将/usr/share/phpmyadmin目录下的配置文件config.sample.inc.php复制一份,重命名为config.sample.inc.php_bak

cp -pr /usr/share/phpmyadmin/config.sample.inc.php /usr/share/phpmyadmin/config.sample.inc.php_bak

4.打开复制出的新配置文件config.inc.PHP进行简单配置

vi /usr/share/phpmyadmin/config.sample.inc.php

5.在“blowfish”获取随机字符串,填入配置文件中

$cfg['blowfish_secret'] = '-,L9JmK:}Hf/K0UXZ01Gs5Z8QU9,Ozb0'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

6.为PHPMyAdmin安装根目录设置权限,避免访问受限问题出现

chmod 775 -R /usr/share/phpmyadmin

7.在浏览器输入http://localhost/phpmyadmin即可访问phpmyadmin界面

原文参考:原文1 原文2 原文3