分类目录归档:Debian

DEBIAN启用密钥登陆

在Linux系统里面,密钥登录是比较安全的一种方法,这里只说在debian 10里面,如何用密钥进行登录。

一,用ssh方式登录到debian系统,建议用putty这个软件

二,生成密钥,输入
ssh-keygen -t rsa(生成密钥)

按两次回车,即可完整的生成密钥了,就会在当前目录(一般为root目录)生成一个.ssh的文件夹

三,设置密钥

cd .ssh
查看目录内有哪些文件
ls -la
发现有id-rsa和id-rsa.pub两个文件,其中 id-rsa为私钥, id-rsa.pub 则为公钥

把公钥改一个文件名
cat id_rsa.pub >> authorized_keys

把改好的文件给上权限
chmod 600 authorized_keys
chmod 700 ~/.ssh

四,设置私钥
在.ssh目录内,查看私钥id_rsa的内容 ,命令:cat id_rsa

cat id_rsa查看密钥内容
然后把下面的所有的内容复制到一个记事本内,命名为:id_rsa

五,设置ssh用密钥登录的配置

nano /etc/ssh/sshd_config

把以下两个前面的注释去掉,让其生效
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

然后重启ssh

service sshd restart
或者
systemctl restart sshd.service

六,设置私钥

https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
下载puttygen.exe,把私钥转换为putty可用模式

点File—–>Load Private key,找到刚才另存为的id_rsa文件,打开,如下,点Save private key,然后保存为一个ppk的文件

七,用putty采用密钥登录,把上一步保存的ppk文件选中

如下图,直接输入root就可以登录了,不用密码

然后在/etc/ssh/sshd_conifg里面,把密码登录改为no,即为不用密码登录

PasswordAuthentication no

DEBIAN11 配置域名并启用SSL

DEBIAN 配置域名并启用SSL

1.将SSL证书文件放在/etc/ssl目录下,放其他目录也可以。

miie_net.pem
miie_net.key

2. 建立网站跟目录

sudo mkdir /var/www/miienet

3.编辑NGINX主机配置文件

miie@miie:~$ cd /etc/nginx/sites-available
miie@miie:/etc/nginx/sites-available$ sudo cp default miienet

miie@miie:/etc/nginx/sites-available$ sudo vi miienet

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        # 注释下面两行,不监听80端口
        #listen 80 default_server;
        #listen [::]:80 default_server;

        # SSL configuration
        #
        # 以下两行注释去掉,监听443端口,并去掉default_server
          listen 443 ssl ;
          listen [::]:443 ssl ;
        # 添加以下6行,导入证书
        ssl_certificate  /etc/ssl/miie_net.pem;
        ssl_certificate_key /etc/ssl/miie_net.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        # 配置网站跟目录
        root /var/www/miienet;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

        # 配置网站域名
        server_name miie.net www.miie.net;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # 启用PHP支持
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # 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;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
# 配置http网站支持
server {
       listen 80;
       listen [::]:80;

       server_name www.miie.net miie.net;
# 配置http转发到https
       rewrite ^(.*)$ https://miie.net;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
}

4.建立配置文件链接

sudo ln -s /etc/nginx/sites-available/miienet  /etc/nginx/sites-enabled/

5.测试并重启NGINX

sudo systemctl reload nginx
sudo systemctl restart nginx

DEBIAN11 安装NGINX PHP MARIADB

DEBIAN11 安装NGINX PHP MARIADB

1.更新系统

更新系统,不更新也行。纯粹个人强迫症

miie@miie:~$ sudo apt update  && sudo apt upgrade
Hit:1 http://mirrors.ustc.edu.cn/debian bullseye InRelease
Get:2 http://mirrors.ustc.edu.cn/debian bullseye-updates InRelease [44.1 kB]
Fetched 44.1 kB in 0s (113 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

安装wget unzip

sudo apt install wget unzip

2.安装NGINX

miie@miie:~ $ sudo apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libdeflate0 libfontconfig1 libgd3 libgeoip1 libicu67 libjbig0
  libjpeg62-turbo libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip libtiff5 libwebp6 libxml2 libxpm4 libxslt1.1 nginx-common nginx-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libdeflate0 libfontconfig1 libgd3 libgeoip1 libicu67 libjbig0
  libjpeg62-turbo libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip libtiff5 libwebp6 libxml2 libxpm4 libxslt1.1 nginx nginx-common nginx-core
0 upgraded, 24 newly installed, 0 to remove and 0 not upgraded.
Need to get 16.8 MB of archives.
After this operation, 56.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
........
Setting up libnginx-mod-http-image-filter (1.18.0-6.1+deb11u2) ...
Setting up nginx-core (1.18.0-6.1+deb11u2) ...
Upgrading binary: nginx.
Setting up nginx (1.18.0-6.1+deb11u2) ...
Processing triggers for libc-bin (2.31-13+deb11u3) ...
miie@miie:~$

3.安装MARIADB

miie@miie:~$ sudo apt install mariadb-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  galera-4 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdbd-mariadb-perl libdbi-perl
  libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl libgdbm-compat4 libgdbm6 libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmariadb3 libmpfr6
  libperl5.32 libsigsegv2 libsnappy1v5 libterm-readkey-perl libtimedate-perl liburi-perl lsof mariadb-client-10.5
  mariadb-client-core-10.5 mariadb-common mariadb-server-10.5 mariadb-server-core-10.5 mysql-common perl perl-modules-5.32
  psmisc rsync socat
Suggested packages:
  gawk-doc libmldbm-perl libnet-daemon-perl libsql-statement-perl gdbm-l10n libdata-dump-perl libipc-sharedcache-perl
  libwww-perl mailx mariadb-test netcat-openbsd perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make
  libtap-harness-archive-perl python3
The following NEW packages will be installed:
  galera-4 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdbd-mariadb-perl libdbi-perl
  libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl libgdbm-compat4 libgdbm6 libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmariadb3 libmpfr6
  libperl5.32 libsigsegv2 libsnappy1v5 libterm-readkey-perl libtimedate-perl liburi-perl lsof mariadb-client-10.5
  mariadb-client-core-10.5 mariadb-common mariadb-server mariadb-server-10.5 mariadb-server-core-10.5 mysql-common perl
  perl-modules-5.32 psmisc rsync socat
0 upgraded, 43 newly installed, 0 to remove and 0 not upgraded.
Need to get 27.3 MB of archives.
After this operation, 210 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y //输入Y按回车键
Setting up libhtml-template-perl (2.97-1.1) ...
Setting up libcgi-fast-perl (1:2.15-1) ...
Processing triggers for libc-bin (2.31-13+deb11u3) ...
miie@miie:~$

配置MARIADB

miie@miie:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

创建数据库和用户

miie@miie:~$ sudo mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 40
Server version: 10.5.15-MariaDB-0+deb11u1 Debian 11

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE miie_db;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> GRANT ALL ON miie_db.* TO 'miie'@'localhost' IDENTIFIED BY '123' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> exit;
Bye
miie@miie:~$

3.安装PHP


miie@miie:~$ sudo apt install php php-fpm php-mysql php-mbstring php-xml
//前三个是NMP运行的主程序基本模块,后两个是phpMyAdmin运行所需模块
Reading package lists... Done
Building dependency tree... Done
..................
Processing triggers for php7.4-cli (7.4.28-1+deb11u1) ...
Processing triggers for php7.4-fpm (7.4.28-1+deb11u1) ...
miie@miie:~$

在/var/www/html/文件夹里创建一个info.php文件,输入代码:,保存并关闭文件。
如果创建不成功请切换到root用户再试试。

miie@miie:~$ sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php
-bash: /var/www/html/info.php: Permission denied
miie@miie:~$ su
Password:
root@miie:/home/miie# echo "<?php phpinfo(); ?>" > /var/www/html/info.php
root@miie:/home/miie#

现在打开浏览器并键入以下地址,以查看Web服务器是否可以显示由此PHP脚本创建的内容。
http://IP/info.php
这里还没有配置NGINX,当然打不开的,打开的时候提示下载文件。

4.配置NGINX

//刚刚操作完忘了退出root用户了
root@miie:/var/www/html# cd /etc/nginx/sites-available
root@miie:/etc/nginx/sites-available# ls
default  default.bak

//编辑default文件
root@miie:/etc/nginx/sites-available# vi default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        #修改这一行,添加index.php。
        index index.html index.htm index.nginx-debian.html index.php;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        #修改下面内容,去掉注释。
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       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;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}
root@miie:/etc/nginx/sites-available#

修改完保存文件。执行以下命令确认配置文件无误并重启NGINX

root@miie:/etc/nginx/sites-available# systemctl reload nginx
root@miie:/etc/nginx/sites-available# systemctl restart nginx

现在打开浏览器并键入以下地址,可以查看PHP的信息页面,如果页面空白,请检查/var/www/html/info.php文件内容是否为“<?php phpinfo(); ?>”。
http://IP/info.php

5.安装PHPMYADMIN

root@miie:/var/www/html# ls
index.nginx-debian.html  info.php
root@miie:/var/www/html# wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip
--2022-08-31 13:19:11--  https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip
Resolving files.phpmyadmin.net (files.phpmyadmin.net)... 89.187.187.12, 89.187.187.20, 89.187.187.15, ...
Connecting to files.phpmyadmin.net (files.phpmyadmin.net)|89.187.187.12|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14801905 (14M) [application/zip]
Saving to: ‘phpMyAdmin-5.1.1-all-languages.zip’
phpMyAdmin-5.1.1-all-languages.z 100%[=======================================================>]  14.12M   860KB/s    in 15s
2022-08-31 13:19:28 (933 KB/s) - ‘phpMyAdmin-5.1.1-all-languages.zip’ saved [14801905/14801905]
root@miie:/var/www/html# ls
index.nginx-debian.html  info.php  phpMyAdmin-5.1.1-all-languages.zip
root@miie:/var/www/html# unzip  phpMyAdmin-5.1.1-all-languages.zip

解压完之后,在浏览器中打开
http://IP/phpMyAdmin-5.1.1-all-languages/
即可用刚刚设置的MARIADB的ROOT用户和密码以及自定的用户密码登录。

至此在Debian上安装NGINX PHP MariaDB已经完成并配置成功。