Debian10安装LAMP

LAMP一般指Linux上的Apache MySQL PHP开源软件包。 该术语是描述Linux操作系统,Apache Web服务器,MariaDB数据库和PHP编程的首字母缩写。在Debian发行版中MySQL已用MariaDB替代。
● 高亮行表示要输入的命令或系统的主要提示。

1.安装Apache

user@debian:~$ sudo apt install apache2  //输入安装命令按回车键
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcurl4
  liblua5.2-0
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcurl4
  liblua5.2-0
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,632 kB of archives.
After this operation, 8,713 kB of additional disk space will be used.
Do you want to continue? [Y/n]   //输入Y或yes,按回车键

“Apache2″中有个数字2表示Apache的版本2,Apache的版本1已经很古老了,现在是版本2了。在有的发行版中Apache又叫httpd,Linux的世界中各个发行版的软件包五花八门,所以个人还是喜欢Debian的统一规范。
Apache安装完成后,安装程序将立即触发systemd系统和服务管理器启动Apache2服务,并使其在系统引导时自动启动。要检查Apache服务是否已启动并正常运行,请运行以下systemctl命令。

 user@debian:~$ systemctl status apache2 //输入命令按回车键
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-08-01 19:51:51 CST; 57s ago
     Docs: https://httpd.apache.org/docs/2.4/
 Main PID: 1697 (apache2)
    Tasks: 55 (limit: 4661)
   Memory: 15.6M
   CGroup: /system.slice/apache2.service
           ├─1697 /usr/sbin/apache2 -k start
           ├─1699 /usr/sbin/apache2 -k start
           └─1700 /usr/sbin/apache2 -k start

您还可以使用以下systemctl命令启动,停止,重新启动并获取Apache Web服务器的状态。

# systemctl start apache2.service
# systemctl restart apache2.service
# systemctl stop apache2.service
# systemctl reload apache2.service
# systemctl status apache2.service

现在您需要测试Apache是否已正确安装并可以提供Web页面。 打开Web浏览器并使用以下URL访问Apache Debian默认页面。
http://SERVER_IP/
或者
http://localhost/

2.安装MariaDB
Apache Web服务器启动并运行后,您需要安装数据库系统才能保留和管理您网站的数据。要安装MariaDB,请使用Debian的apt包管理器。

user@debian:~$ sudo apt install mariadb-server  //输入安装命令按回车键
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  galera-3 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl
  libfcgi-perl libhtml-template-perl libreadline5 libsigsegv2 libterm-readkey-perl mariadb-client-10.3
  mariadb-client-core-10.3 mariadb-server-10.3 mariadb-server-core-10.3 rsync socat
Suggested packages:
  gawk-doc libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx
  mariadb-test netcat-openbsd tinyca
The following NEW packages will be installed:
  galera-3 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl
  libfcgi-perl libhtml-template-perl libreadline5 libsigsegv2 libterm-readkey-perl mariadb-client-10.3
  mariadb-client-core-10.3 mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 rsync socat
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 20.9 MB of archives.
After this operation, 167 MB of additional disk space will be used.
Do you want to continue? [Y/n]   //输入Y或yes,按回车键

安装MariaDB后,建议运行安全脚本mysql_secure_installation,该脚本将删除一些不安全的默认设置并禁用对数据库系统的访问。安全脚本将引导您完成以下一系列问题,您可以在其中对MariaDB设置进行一些更改。切换到root用户再执行,完成后退出root用户,使用sudo也可以。

user@debian:~$ 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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   //提示输入当前MariaDB(MySQL)root密码,直接按回车键
OK, successfully used password, moving on...

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

Set root password? [Y/n] Y    //是否设置MariaDB(MySQL)root的密码,输入Y按回车键。
New password:    //输入MariaDB(MySQL)root的密码
Re-enter new password:    //再次输入MariaDB(MySQL)root的密码
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    //是否删除匿名用户,输入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    //是否禁用root远程登录,输入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    //是否删除测试数据库,输入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    //是否在不重启的前提下更新设置,输入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!

创建名为“t_db”的数据库和名为“t”且具有数据库完全权限的用户,请运行以下命令。
在MariaDB(MySQL)命令提示符中,语句结尾必须加上分号后按回车键才能执行,否者不执行语句。如果没加分号按了回车键,没关系,在下一行里输入分号按回车键也会执行。

user@debian:~$ sudo mysql -uroot -p
Enter password:    //输入刚刚设置的MariaDB(MySQL)root的密码,不是Debian的root密码。
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 62
Server version: 10.3.29-MariaDB-0+deb10u1 Debian 10

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 t_db;    //创建数据库
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> GRANT ALL ON t_db.* TO 't'@'localhost' IDENTIFIED BY '123' WITH GRANT OPTION;    //创建用户并授予管理权限
Query OK, 0 rows affected (0.000 sec)     

MariaDB [(none)]> FLUSH PRIVILEGES;        //更新数据库设置
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> exit;    //退出MariaDB(MySQL)
Bye

用刚刚创建的用户登录MariaDB来确认新用户是否具有数据库的完全权限。

user@debian:~$ mysql -ut -p
Enter password:    //输入用户t的密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 63
Server version: 10.3.29-MariaDB-0+deb10u1 Debian 10

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)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| t_db               |
+--------------------+
2 rows in set (0.000 sec)

MariaDB [(none)]> USE t_db;
Database changed
MariaDB [t_db]> SHOW TABLES;
Empty set (0.000 sec)

MariaDB [t_db]> exit;
Bye

3.安装PHP
PHP(超文本预处理器)是一种流行的脚本语言,用于构建用于显示Web内容和用户与数据库交互的逻辑。
安装PHP时,顺便把需要的PHP模块一起安装了。

user@debian:~$ sudo apt install php libapache2-mod-php php-mysql php-mbstring php-xml //前三个是AMP运行的主程序基本模块,后两个是phpMyAdmin运行所需模块
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php7.3 php-common php7.3 php7.3-cli php7.3-common php7.3-json php7.3-mbstring php7.3-mysql
  php7.3-opcache php7.3-readline php7.3-xml
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php libapache2-mod-php7.3 php php-common php-mbstring php-mysql php-xml php7.3 php7.3-cli
  php7.3-common php7.3-json php7.3-mbstring php7.3-mysql php7.3-opcache php7.3-readline php7.3-xml
0 upgraded, 16 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,790 kB of archives.
After this operation, 20.1 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y    //输入Y或yes,按回车键

如果要安装其他PHP模块,可以使用apt-cache命令和grep命令的组合进行搜索和安装。

user@debian:~$ apt-cache search php | egrep 'module' | grep default
libapache2-mod-php - server-side, HTML-embedded scripting language (Apache 2 module) (default)
php-bcmath - Bcmath module for PHP [default]
php-bz2 - bzip2 module for PHP [default]
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-enchant - Enchant module for PHP [default]
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
php-imap - IMAP module for PHP [default]
php-interbase - Interbase module for PHP [default]
php-intl - Internationalisation module for PHP [default]
php-json - JSON module for PHP [default]
php-ldap - LDAP module for PHP [default]
php-mbstring - MBSTRING module for PHP [default]
php-mysql - MySQL module for PHP [default]
php-odbc - ODBC module for PHP [default]
php-pgsql - PostgreSQL module for PHP [default]
php-pspell - pspell module for PHP [default]
php-readline - readline module for PHP [default]
php-recode - recode module for PHP [default]
php-snmp - SNMP module for PHP [default]
php-soap - SOAP module for PHP [default]
php-sqlite3 - SQLite3 module for PHP [default]
php-sybase - Sybase module for PHP [default]
php-tidy - tidy module for PHP [default]
php-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP [default]
php-xmlrpc - XMLRPC-EPI module for PHP [default]
php-zip - Zip module for PHP [default]

重新加载Apache的配置并检查Apache运行状态。

user@debian:~$ systemctl reload apache2
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to reload 'apache2.service'.
Authenticating as: root
Password:    //输入Debian的root的密码,在命令前加sudo后可以不用输入密码
==== AUTHENTICATION COMPLETE ===
user@debian:~$ systemctl status apache2
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-08-01 20:34:39 CST; 5min ago
     Docs: https://httpd.apache.org/docs/2.4/
  Process: 12666 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
  Process: 12886 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
 Main PID: 12670 (apache2)
    Tasks: 6 (limit: 4661)
   Memory: 14.3M
   CGroup: /system.slice/apache2.service
           ├─12670 /usr/sbin/apache2 -k start
           ├─12890 /usr/sbin/apache2 -k start
           ├─12891 /usr/sbin/apache2 -k start
           ├─12892 /usr/sbin/apache2 -k start
           ├─12893 /usr/sbin/apache2 -k start
           └─12894 /usr/sbin/apache2 -k start

测试功能是否正常。在Apache上测试PHP,创建一个简单的PHP脚本来验证Apache是否可以处理PHP文件的请求。
在/var/www/html/文件夹里创建一个info.php文件,输入代码:,保存并关闭文件。
如果创建不成功请切换到root用户再试试。

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

现在打开浏览器并键入以下地址,以查看Web服务器是否可以显示由此PHP脚本创建的内容。
http://SERVER_IP/info.php
或者
http://localhost/info.php
如果您在Web浏览器中看到上面的页面,那么PHP安装正在按预期工作。 此外,此页面显示了有关PHP安装的一些基本详细信息,它对于调试非常有用,但同时它还会显示有关PHP的一些敏感信息。

4.安装phpMyAdmin
phpMyAdmin的官方网址是https://www.phpmyadmin.net。
可以自己下载phpMyAdmin上传到/var/www/html/文件夹,也可以wget下载解压缩到/var/www/html/文件夹,方法很多,请自行 探索。

user@debian:~$ wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip
--2021-08-01 20:54:03--  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.15, 2a02:6ea0:c800::7
Connecting to files.phpmyadmin.net (files.phpmyadmin.net)|89.187.187.15|: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-language 100%[=================================================>]  14.12M  5.69MB/s    in 2.5s

2021-08-01 20:54:07 (5.69 MB/s) - ‘phpMyAdmin-5.1.1-all-languages.zip’ saved [14801905/14801905]

user@debian:~$ ls     //查看phpMyAdmin压缩包文件名,那么长懒得一个一个敲。
Desktop  Documents  Downloads  Music  phpMyAdmin-5.1.1-all-languages.zip  Pictures  Public  Templates  Videos
user@debian:~$ sudo unzip phpMyAdmin-5.1.1-all-languages.zip -d /var/www/html/  //解压到指定文件夹
Archive:  phpMyAdmin-5.1.1-all-languages.zip
   creating: /var/www/html/phpMyAdmin-5.1.1-all-languages/
  inflating: /var/www/html/phpMyAdmin-5.1.1-all-languages/CONTRIBUTING.md
  inflating: /var/www/html/phpMyAdmin-5.1.1-all-languages/ChangeLog
.......

打开浏览器,在网址中输入http://localhost//phpMyAdmin-5.1.1-all-languages/即可看到登录页面。使用刚刚创建的用户t和密码登录phpMyAdmin就可以管理MariaDB(MySQL)数据库。

5.最后
在本文中,已经解释了如何在Debian 10服务器上安装Linux,Apache,MariaDB和PHP(LAMP)。
这只是在单机安装LAMP,仅供学习用。实际中因各种安全问题仍需做大量工作。
如果您对本文有疑问,请随时在评论部分询问。

发表评论