Debian9,Debian10安装LNMP环境

LNM.png

此教程已过时,可以查看 Debian10/11 安装nginx1.22 PHP8.2

先贴两个官方源

debian9官方源

deb http://deb.debian.org/debian/ stretch main
deb-src http://deb.debian.org/debian/ stretch main
deb http://security.debian.org/ stretch/updates main
deb-src http://security.debian.org/ stretch/updates main
deb http://deb.debian.org/debian/ stretch-updates main
deb-src http://deb.debian.org/debian/ stretch-updates main

debian10官方源

deb http://deb.debian.org/debian/ buster main
deb-src http://deb.debian.org/debian/ buster main
deb http://security.debian.org/debian-security buster/updates main contrib
deb-src http://security.debian.org/debian-security buster/updates main contrib
deb http://deb.debian.org/debian/ buster-updates main contrib
deb-src http://deb.debian.org/debian/ buster-updates main contrib

Debian9以上默认内核应该都是4.9以上了,所以可以在开始之前先开一波BBR,优化下网络:

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

1.一些报错先提前解决

先更新软件源,更新软件

apt update
apt upgrade

报错1

gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation

安装

apt-get install -y gnupg2

报错2

apt-get update 
E: 无法找到获取软件包的渠道 /usr/lib/apt/methods/https 所需的驱动程序。
N: 请检查是否安装了 apt-transport-https 软件包
E: 无法下载 https://mariadb.mirror.serveriai.lt/repo/10.5/debian/dists/stretch/InRelease
E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。

安装

apt-get install apt-transport-https

2.安装Mriadb

查看APT存储库mariadb版本

apt show mariadb-server

MariaDB 10.1是默认Debian 9 stretch存储库上的当前可用版本。

MariaDB 10.3是默认Debian 10 Buster存储库上的当前可用版本。

安装自带的版本直接

apt update
apt-get install mariadb-server

安装最新10.5版本

Mariadb官网选择对应版本根据提示安装

链接
MariaDB - Setting up MariaDB Repositories - MariaDB

进入后选择对应版本后如下
mariadbx.png

执行过程过出现上面那些报错已经先解决了

我选择的是debian9安装10.5版本

以下是要运行以将 MariaDB 添加到系统的命令:

sudo apt-get install software-properties-common dirmngr
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,i386,ppc64el] https://mariadb.mirror.serveriai.lt/repo/10.5/debian stretch main'

导入密钥并添加存储库后,可以使用以下功能安装 MariaDB:

sudo apt-get update
sudo apt-get install mariadb-server

您还可以创建自定义 MariaDB 源.list 文件。为此,请将以下内容复制并粘贴到*/etc/apt/sources.d*/(我们建议命名文件 MariaDB.list 或类似内容)下的文件中,或将其添加到您的 /etc/apt/sources.list 文件的底部。

# MariaDB 10.5 repository list - created 2020-12-19 02:11 UTC
# http://downloads.mariadb.org/mariadb/repositories/
deb [arch=amd64,arm64,i386,ppc64el] https://mariadb.mirror.serveriai.lt/repo/10.5/debian stretch main
deb-src https://mariadb.mirror.serveriai.lt/repo/10.5/debian stretch main

3.安装Nginx

Debian9的源默认只能安装到1.10版本
Debian10的源默认只能安装到1.14版本

我们要安装新一点的版本

vi /etc/apt/sources.list

Debian9写入

deb http://nginx.org/packages/debian/ stretch nginx
deb-src http://nginx.org/packages/debian/ stretch nginx

Debian10写入

deb http://nginx.org/packages/debian/ buster nginx
deb-src http://nginx.org/packages/debian/ buster nginx

添加key不然安装的时候出现GPG签名报错:

wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key

更新软件并源查看版本

apt update
apt show nginx
Package: nginx
Version: 1.18.0-2~stretch

安装

apt install nginx

4.安装PHP

Debian 9 自带PHP7.0

apt-get install php7.0 php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-mysql php7.0-opcache php7.0-xml php7.0-xmlrpc php7.0-sqlite3 php7.0-mbstring

Debian 10 自带PHP7.3

apt-get install php7.3 php7.3-cli php7.3-common php7.3-curl php7.3-fpm php7.3-gd php7.3-mysql php7.3-opcache php7.3-xml php7.3-xmlrpc php7.3-sqlite3 php7.3-mbstring

安装完成之后记得启动一下FPM

systemctl start php7.0-fpm
systemctl start php7.3-fpm

安装最新版PHP7.4

依次输入

apt -y install lsb-release apt-transport-https ca-certificates 
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
apt update
apt-get install php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-fpm php7.4-gd php7.4-mysql php7.4-opcache php7.4-xml php7.4-xmlrpc php7.4-sqlite3 php7.4-mbstring

启动

systemctl start php7.4-fpm

php -v查看版本

PHP 7.4.13 (cli) (built: Nov 30 2020 20:40:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

安装php7.4必要的扩展

apt install php7.4-dev  #phpize需要
apt install php-pear    #pecl需要
apt install php7.4-fpm php7.4-mysql php7.4-curl php7.4-json php7.4-mbstring php7.4-xml php7.4-intl

安装可选扩展

apt install php7.4-gd
apt install php7.4-soap
apt install php7.4-gmp 
apt install php7.4-odbc 
apt install php7.4-pspell 
apt install php7.4-bcmath 
apt install php7.4-enchant 
apt install php7.4-ldap 
apt install php7.4-opcache
apt install php7.4-readline 
apt install php7.4-sqlite3 
apt install php7.4-xmlrpc
apt install php7.4-bz2
apt install php7.4-interbase
apt install php7.4-pgsql 
apt install php7.4-recode 
apt install php7.4-sybase 
apt install php7.4-xsl
apt install php7.4-cgi 
apt install php7.4-dba
apt install php7.4-phpdbg 
apt install php7.4-snmp 
apt install php7.4-tidy 
apt install php7.4-zip*

注:这个7.0.30的FPM监听方式已经从之前的127.0.0.1:9000改为了/run/php/php7.0-fpm.sock所以接下来我们在编辑Nginx配置文件的时候需要注意一下。

5.搭建环境测试

先来安装一个phpMyAdmin

apt-get install unzip
cd /usr/share/nginx/
wget https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip
unzip phpMyAdmin-4.8.2-all-languages.zip
rm -rf phpMyAdmin-4.8.2-all-languages.zip
mv phpMyAdmin-4.8.2-all-languages phpmyadmin

新建一个Nginx配置文件:

vi /etc/nginx/conf.d/phpmyadmin.conf
server {
    listen       2333;
    server_name  192.168.31.22;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/phpmyadmin;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        root           /usr/share/nginx/phpmyadmin;
        fastcgi_pass   unix:/run/php/php7.0-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/phpmyadmin$fastcgi_script_name;
        include        fastcgi_params;
    }

}
systemctl restart nginx

访问 http://IP:2333

再来安装一个WordPress:

cd /usr/share/nginx/
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
unzip wordpress-4.9.4-zh_CN.zip
rm -rf wordpress-4.9.4-zh_CN.zip

新建一个Nginx配置文件:

nano /etc/nginx/conf.d/wordpress.conf
server {
    listen       81;
    server_name  172.104.111.195;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/wordpress;
        index  index.html index.htm index.php;
        include /etc/nginx/conf.d/rewrite/wordpress.conf;
    }

    location ~ \.php$ {
        root           /usr/share/nginx/wordpress;
        fastcgi_pass   unix:/run/php/php7.0-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/wordpress$fastcgi_script_name;
        include        fastcgi_params;
    }

}

再新建一个WordPress的伪静态配置文件:

mkdir -p /etc/nginx/conf.d/rewrite/
vi  /etc/nginx/conf.d/rewrite/wordpress.conf

写入伪静态规则:

location / {
	try_files $uri $uri/ /index.php?$args;
}

重要!最后把wordpress的目录所有者改为www-data(在Debian9中,PHP默认以www-data用户运行)

chown -R www-data:www-data wordpress

502 错误网关

502.png

出现此错误是由于权限不足

vim /etc/nginx/nginx.conf

将运行用户改为root

user root;

你可能会用到的配置文件

Nginx错误日志:/var/log/nginx/error.log
Nginx主配置文件:/etc/nginx/nginx.conf
Nginx站点配置文件:/etc/nginx/conf.d/
Nginx站点目录:/usr/share/nginx/
PHP-FPM配置文件:/etc/php/7.0/fpm/pool.d/www.conf
PHP.INI配置文件:/etc/php/7.0/fpm/php.ini
MySQL主配置文件:/etc/mysql/my.cnf

如果不想用这个环境了,想删除环境:

apt-get autoremove --purge nginx
apt-get autoremove --purge mysql-server

参考:https://lala.im/3894.html
https://www.sooele.com/4156.html

# mysql  mariadb  nginx  php 

本文由:星际难民
实践,测试,整理发布.如需转载请注明地址 本文标题:Debian9,Debian10安装LNMP环境
地址:https://530503.xyz/articles/2020/12/19/1608341377718.html

评论

取消