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已经完成并配置成功。

发表评论