如何在Ubuntu 18.04上安装和配置LAMP Web服务器?

目的

LAMP是目前世界上应用最广泛的web应用软件之一。这个缩写源于它的四个软件组件的名称:Linux、Apache、MySQL和PHP。每个设计都是为了与其他部件一起运行。本文将解释如何安装和配置所有必要的组件,以便在Ubuntu 18.04中安装LAMP。

要安装LAMP,首先要通过SSH连接连接到服务器。可以参考有关的攻略连接SSH协议。如果是本地服务器,则进入下一步,打开服务器的终端。

安装Apache

Apache是最广泛使用的web服务器,能够在各种各样的操作系统上运行,包括UNIX / Linux、Microsoft Windows和OpenVMS。

如要完成安装,首先要更新发布存储库,下载最新的Apache版本。

$ sudo apt update && sudo apt install apache2

注意,在使用sudo命令以root用户身份运行命令时,需要插入以前使用的用户密码。在您接收同意安装Apache之后,安装被开始执行。

配置防火墙

如果系统上有防火墙,首先将其设置为在机器上启用HTTP通信和HTTPS通信。如果使用UFW防火墙,则提供用于Apache的预安装配置文件,可以直接启动:

查看UFW防火墙中安装的可用配置文件。

$ sudo ufw app list

你会看到:

Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

使用“Apache Full”配置文件,允许HTTP(端口80)和HTTPS(端口443)通信。
资料信息可按以下方式检查:

$ sudo ufw app info "Apache Full"

此时可以看到:

Profile: Apache Full

Title: Web Server (HTTP,HTTPS)

Description: Apache v2 is the next generation of the omnipresent Apache web server.

Ports:
80,443/tcp

检查配置文件之后,就可以启用:

$ sudo ufw allow in "Apache Full"

至此,Apache和防火墙的配置就完成。要测试web服务器,请访问http://< server . ip >,如果是本地服务器,则访问http://localhost。
如果您操作正确,将看到Apache欢迎页面。

安裝MySQL

MySQL是世界上应用最广泛的关系数据库管理系统之一。更新发布库以下载最新版本的MySQL。如果您已经启动过该命令,可直接跳至下一步。

$ sudo apt update

安装:

$ sudo apt install mysql-server

验证是否安装成功,并检查MySQL版本:

$ sudo mysqld --version

如果一切顺利,屏幕会显示已安装的MySQL版本。现在通过运行MySQL包含的脚本来保护MySQL,以提高安全性并限制对数据库的访问:

$ sudo mysql_secure_installation

此时,将启动一个指导过程来配置MySQL安全级别。

系统将询问您是否要启用密码验证系统。如果启用,在设置用户密码时会评估密码。如果密码不符合最低安全要求,则拒绝该密码并提示错误。

之后,系统会询问您是否希望使用您所选择的其中一个更改root密码(如果启用密码验证系统,则必须输入符合安全条件的密码)。
如何创建安全度较高的密码:

  • 包括大写字母和小写字母
  • 包括使用字母和数字
  • 使用特殊符号,如@ # $% ˆ &!
  • 避免使用已创建的密码。

最后,选择是否删除匿名用户并测试数据库,以及是否禁用root用户远程登录(更高安全性)。建议允许所有这些更改。
要验证MySQL功能,可使用以下命令登录:

$ sudo mysql

如果所有操作正确,您将进入MySQL控制台:

mysql > exit;

此时,您已经完成MySQL的安装与配置。

安裝PHP

PHP is the interpreter of the homonymous scripting language for programming dynamic web pages.
Update the distribution repositories, to download the latest PHP version. If you have already launched this command before, go to the next step.

$ sudo apt update

Install the PHP interpreter and some modules to execute PHP with Apache and make it communicate with MySQL:

$ sudo apt install php libapache2-mod-php php-mysql

At this point, the installation will have been completed. All you need to do is verify it by checking the installed PHP version:

$ sudo php -v

In case of successful installation , the version of PHP just installed will be displayed.
Then, change the priority of the Apache index files to have it give the highest priority to files named index.php.
Then edit the Apache configuration file “dir.conf”:

$ sudo nano /etc/apache2/mods-enabled/dir.conf

Move the index.php file to the top of the list, in this way :

Save the changes by pressing CTRL + X, typing Y and then confirm by pressing enter.
Restart Apache to apply the changes

$ sudo systemctl restart apache2

To verify the operation of the PHP interpreter and its configuration, all you need to do is create an index.php file in the folder that contains your site:

$ sudo nano /var/www/html/index.php

Insert the following content in the file to print the information of your on-screen configuration:


Then save the changes by pressing CTRL + X, typing Y, and then confirm by pressing enter.
At this point, visit the address http:// or in the case of a local server http: // localhost .
If the PHP info page is correctly displayed, the procedure was successfully performed.
At this point you have correctly installed and configured your server with a LAMP architecture.

您的免费试用从这里开始!
联系我们的团队申请物理服务器服务!
注册成为会员,尊享专属礼遇!
您的免费试用从这里开始!
联系我们的团队申请物理服务器服务!
注册成为会员,尊享专属礼遇!
Telegram Skype