如何在debian 10 上安装lamp服务

时间:2019-04-29 03:18:27  来源:igfitidea点击:

LAMP是Linux、Apache web服务器、MariaDB数据库(替换掉MySQL)和PHP web脚本语言的首字母缩写。LAMP是常用的动态网站和流行的应用程序如wordpress的基础环境。

在Debian 10上安装LAMP

更新Debian Linux

sudo apt update
sudo apt upgrade

安装apche

sudo apt install apache2
sudo systemctl start apache2.service

如何启动、停止、重启apache服务器

## 启动
sudo systemctl start apache2.service
## 停止
sudo systemctl stop apache2.service
## 重启
sudo systemctl restart apache2.service
## 查看状态
sudo systemctl status apache2.service
## 重新加载配置
sudo systemctl reload apache2.service

设置防火墙

sudo ufw allow www
sudo ufw allow https
sudo ufw status

测试
使用浏览器打开 http://服务器IP,出现默认首页:

apache2 debian默认页面

配置Apache

配置文件为:/etc/apache2/mods-available/mpm_prefork.conf

# prefork MPM
# StartServers: number of server processes to start 要启动的服务进程数
# MinSpareServers: minimum number of server processes which are kept spare 保持为备用的进程的最少数量
# MaxSpareServers: maximum number of server processes which are kept spare 保持为备用的进程的最少数量
# MaxRequestWorkers: maximum number of server processes allowed to start 允许启动的进程的最大数目
# MaxConnectionsPerChild: maximum number of requests a server process serves 服务器进程的最大请求数
 
<IfModule mpm_prefork_module>
	StartServers		  4
	MinSpareServers		  20
	MaxSpareServers		  40
	MaxRequestWorkers	  200
	MaxConnectionsPerChild    4500
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

禁用Apache event模块并启用httpd prefork

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork

重启,使配置生效

sudo systemctl restart apache2.service

如何创建apache虚拟主机

复制默认的Apache配置文件

sudo cp -v /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/www.theitroad.com.conf

www.theitroad.com.conf:

<Directory /var/www/www.theitroad.local/public_html>
        Require all granted
</Directory>
<VirtualHost *:80>
        ServerName theitroad.local
        ServerAlias www.theitroad.local
        ServerAdmin [email protected]
        DocumentRoot /var/www/theitroad.local/public_html
	ErrorLog ${APACHE_LOG_DIR}/www.theitroad.local_error.log
	CustomLog ${APACHE_LOG_DIR}/www.theitroad.local_access.log combined
</VirtualHost>

创建站点的主目录,和上面的配置对应

mkdir -p /var/www/www.theitroad.local/public_html

启用新站点,并禁用默认站点

sudo a2ensite www.theitroad.local
sudo a2dissite 000-default

创建一个index.html进行测试

/var/www/theitroad.local/public_html/index.html

<p>hello world</p>

配置权限

sudo chmod 754 /var/www/theitroad.local/

重新加载配置或者重启apache服务使配置生效

sudo systemctl reload apache2.service
或者
sudo systemctl restart apache2.service

再次使用浏览器打开 http://服务器IP

如何在Debian 10上安装MariaDB

sudo apt install mariadb-server

首次安装时,需要运行脚本mysql_secure_installation, 提高MariaDB的安全性

sudo mysql_secure_installation

根据提示进行配置

# 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): 
OK, successfully used password, moving on...

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

Change the root password? [Y/n] y 是否修改root密码

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 是否禁用root通过远程登录
 ... 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 是否删除test数据库
 - 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!

创建MariaDB用户

登录数据库

mysql -u root -p

创建一个名为' wpdb '的数据库,并授予用户权限。

CREATE DATABASE wpdb;
GRANT ALL ON wpdb.* TO 'wpuser' IDENTIFIED BY 'wppasword';
quit

测试新用户

mysql -u wpuser -p'wppasword' wpdb

在Debian上安装PHP

sudo apt install php libapache2-mod-php -y

查看php模块

apt-cache search php | egrep 'module' | grep default

根据需要安装PHP模块

sudo apt install php-mysql php-gd

重启apache,使其能解析php文件

sudo systemctl restart apache2.service

创建一个新文件测试PHP

/var/www/theitroad.local/public_html/test.php

<?php
  phpinfo();
?>

使用浏览器打开 http://服务器IP/test.php

可以看到php的安装信息

phpinfo