如何在Ubuntu 20.04上安装Linux,Nginx,MySQL,PHP(LEMP堆栈)[快速入门]

news/2024/7/6 1:26:02

介绍 (Introduction)

In this quickstart guide, we’ll install a LEMP stack on an Ubuntu 20.04 server.

在本快速入门指南中,我们将在Ubuntu 20.04服务器上安装LEMP堆栈。

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 20.0

有关本教程的更详细版本,以及每个步骤的更多说明,请参阅如何在Ubuntu 20.0上安装Linux,Nginx,MySQL,PHP(LEMP堆栈)。

先决条件 (Prerequisites)

To follow this guide, you’ll need access to an Ubuntu 20.04 server as a sudo user.

要遵循本指南,您需要以sudo用户身份访问Ubuntu 20.04服务器。

第1步-安装Nginx (Step 1 — Install Nginx)

Update your package manager cache and then install Nginx with:

更新您的软件包管理器缓存,然后使用以下命令安装Nginx:

  • sudo apt update

    sudo apt更新
  • sudo apt install nginx

    sudo apt安装nginx

Once the installation is finished, you’ll need to adjust your firewall settings to allow HTTP traffic on your server. Run the following command to allow external access on port 80 (HTTP):

安装完成后,您需要调整防火墙设置以允许服务器上的HTTP通信。 运行以下命令以允许端口80 (HTTP)上的外部访问:

  • sudo ufw allow in "Nginx"

    sudo ufw在“ Nginx”中允许

With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser. You’ll see a page like this:

添加新的防火墙规则后,您可以通过从Web浏览器访问服务器的公用IP地址或域名来测试服务器是否已启动并正在运行。 您会看到这样的页面:

第2步-安装MySQL (Step 2 — Install MySQL)

We’ll now install MySQL, a popular database management system used within PHP environments.

现在,我们将安装MySQL,这是在PHP环境中使用的流行数据库管理系统。

Again, use apt to acquire and install this software:

同样,使用apt来获取并安装此软件:

  • sudo apt install mysql-server

    sudo apt安装mysql服务器

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. Start the interactive script by running:

安装完成后,建议您运行MySQL随附的安全脚本。 通过运行以下命令来启动交互式脚本:

  • sudo mysql_secure_installation

    须藤mysql_secure_installation

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN. Answer Y for yes, or anything else to continue without enabling. If you answer “yes”, you’ll be asked to select a level of password validation.

这将询问您是否要配置VALIDATE PASSWORD PLUGIN 。 答Y (是),或其他任何继续不启用。 如果回答“是”,则将要求您选择密码验证级别。

Your server will next ask you to select and confirm a password for the MySQL root user. Even though the default authentication method for the MySQL root user dispenses the use of a password, even when one is set, you should define a strong password here as an additional safety measure.

接下来,您的服务器将要求您选择并确认MySQL 用户的密码。 即使MySQL超级用户的默认身份验证方法免除了使用密码, 即使设置了密码,您也应在此处定义一个强密码作为附加的安全措施。

For the rest of the questions, press Y and hit the ENTER key at each prompt.

对于其余的问题,请按Y并在每个提示下按ENTER键。

Note: At the time of this writing, the native MySQL PHP library mysqlnd doesn’t support caching_sha2_authentication, the default authentication method for MySQL 8. For that reason, when creating database users for PHP applications on MySQL 8, you’ll need to make sure they’re configured to use mysql_native_password instead. Please refer to step 6 of our detailed LEMP on Ubuntu 20.04 guide to learn how to do that.

注意:在撰写本文时,本机MySQL PHP库mysqlnd 不支持 caching_sha2_authentication ,这是MySQL 8的默认身份验证方法。因此,在MySQL 8上为PHP应用程序创建数据库用户时,您需要确保将它们配置为使用mysql_native_password代替。 请参阅我们在Ubuntu 20.04上详细的LEMP指南的第6步,以了解如何执行此操作。

第3步-安装PHP (Step 3 — Install PHP)

To install the php-fpm and php-mysql packages, run:

要安装php-fpmphp-mysql软件包,请运行:

  • sudo apt install php-fpm php-mysql

    须藤apt安装php-fpm php-mysql

步骤4 —为PHP配置Nginx (Step 4 — Configure Nginx for PHP)

In this guide, we’ll set up a domain called your_domain, but you should replace this with your own domain name.

在本指南中,我们将设置一个名为your_domain的域,但是您应该使用自己的domain name替换它

On Ubuntu 20.04, Nginx has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become difficult to manage if you are hosting multiple sites. Instead of modifying /var/www/html, we’ll create a directory structure within /var/www for the your_domain website, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

在Ubuntu 20.04上,Nginx默认情况下启用了一个服务器块,并将其配置为提供/var/www/html目录中的文档。 尽管这对于单个站点非常有效,但是如果您托管多个站点,则可能变得难以管理。 我们将在/var/wwwyour_domain网站创建目录结构,而不是修改/var/www/html ,如果客户请求未得到响应,则将/var/www/html保留为默认目录以供使用匹配其他任何网站。

Create the root web directory for your_domain as follows:

your_domain创建根Web目录,如下所示:

  • sudo mkdir /var/www/your_domain

    须藤mkdir / var / www / your_domain

Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:

接下来,使用$ USER环境变量分配目录的所有权,该变量将引用您当前的系统用户:

  • sudo chown -R $USER:$USER /var/www/your_domain

    须藤chown -R $ USER:$ USER / var / www / your_domain

Then, open a new configuration file in Nginx’s sites-available directory using your preferred command-line editor. Here, we’ll use nano:

然后,使用首选的命令行编辑器在Nginx的sites-available目录中打开一个新的配置文件。 在这里,我们将使用nano

  • sudo nano /etc/nginx/sites-available/your_domain

    须藤纳米/ etc / nginx / sites-available / your_domain

This will create a new blank file. Paste in the following bare-bones configuration:

这将创建一个新的空白文件。 粘贴以下基本配置:

/etc/nginx/sites-available/your_domain
/ etc / nginx / sites-available / your_domain
server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}

When you’re done editing, save and close the file. If you’re using nano, you can do so by typing CTRL+X and then y and ENTER to confirm.

完成编辑后,保存并关闭文件。 如果您使用的是nano ,则可以通过键入CTRL+X ,然后输入yENTER进行确认。

Activate your configuration by linking to the config file from Nginx’s sites-enabled directory:

通过链接到Nginx sites-enabled目录中的配置文件来激活您的配置:

  • sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

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

This will tell Nginx to use the configuration next time it is reloaded. You can test your configuration for syntax errors by typing:

这将告诉Nginx在下次重新加载时使用配置。 您可以通过键入以下内容来测试配置中的语法错误:

  • sudo nginx -t

    须藤Nginx -t

If any errors are reported, go back to your configuration file to review its contents before continuing.

如果报告了任何错误,请在继续操作之前返回到配置文件以检查其内容。

When you are ready, reload Nginx to apply the changes:

准备就绪后,重新加载Nginx以应用更改:

  • sudo systemctl reload nginx

    须藤systemctl重新加载nginx

Your new website is now active, but the web root /var/www/your_domain is still empty. Create an index.html file in that location so that we can test that your new server block works as expected:

您的新网站现在处于活动状态,但是Web根目录/var/www/ your_domain仍然为空。 在该位置创建一个index.html文件,以便我们可以测试您的新服务器块是否按预期工作:

  • nano /var/www/your_domain/index.html

    纳米/ var / www / your_domain /index.html

Include the following content in this file:

在此文件中包括以下内容:

/var/www/your_domain/index.html
/var/www/your_domain/index.html
<html>
  <head>
    <title>your_domain website</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>This is the landing page of <strong>your_domain</strong>.</p>
  </body>
</html>

Now go to your browser and access your server’s domain name or IP address, as listed within the server_name directive in your server block configuration file:

现在转到浏览器并访问服务器的域名或IP地址,如服务器块配置文件中server_name指令中列出的那样:

http://server_domain_or_IP

You’ll see a page like this:

您会看到这样的页面:

第5步—使用Nginx测试PHP (Step 5 — Test PHP with Nginx)

We’ll now create a PHP test script to confirm that Nginx is able to handle and process requests for PHP files.

现在,我们将创建一个PHP测试脚本,以确认Nginx能够处理和处理对PHP文件的请求。

Create a new file named info.php inside your custom web root folder:

在自定义Web根文件夹中创建一个名为info.php的新文件:

  • nano /var/www/your_domain/info.php

    纳米/ var / www / your_domain /info.php

This will open a blank file. Add the following content inside the file:

这将打开一个空白文件。 在文件内添加以下内容:

/var/www/your_domain/info.php
/var/www/your_domain/info.php
<?php
phpinfo();

When you are finished, save and close the file.

完成后,保存并关闭文件。

You can now access this page in your web browser by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by /info.php:

现在,您可以在Web浏览器中访问此页面,方法是访问在Nginx配置文件中设置的域名或公共IP地址,然后/info.php

http://server_domain_or_IP/info.php

You will see a web page containing detailed information about your server:

您将看到一个包含有关服务器的详细信息的网页:

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Ubuntu server. You can use rm to remove that file:

通过该页面检查有关PHP服务器的相关信息后,最好删除您创建的文件,因为该文件包含有关PHP环境和Ubuntu服务器的敏感信息。 您可以使用rm删除该文件:

  • sudo rm /var/www/your_domain/info.php

    须藤rm / var / www / your_domain /info.php

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04-quickstart


http://www.niftyadmin.cn/n/3648285.html

相关文章

[J2ME/kSOAP]kSOAP的运用讲义[去年8月手稿]

去年 8月份的一份手稿&#xff0c;完整描述了我对j2me-kSOAP如何和服务器端的Web Service交互的经验和教训。本手稿已刊登在mingjava兄弟的新书中。[j2me]kSOAP的运用编写者日期关键词郑昀ultrapower2006-8-24J2me webservice ksoap1&#xff0e;概述对于J2ME访问远端的Web Ser…

多元化专业化没有好坏之分

多元化风潮没有好坏之分专业化和多元化&#xff0c;就其本质而言&#xff0c;并不是一个问题。从现象上来说&#xff0c;既有专业化公司的成功案例&#xff0c;也有多元化公司的成功案例&#xff0c;所以以某一个个案来否定或肯定是否多元化&#xff0c;并没有多少说服力&#…

深入浅出 RPC详解

浅出篇 近几年的项目中&#xff0c;服务化和微服务化渐渐成为中大型分布式系统架构的主流方式&#xff0c;而 RPC 在其中扮演着关键的作用。在平时的日常开发中我们都在隐式或显式的使用 RPC&#xff0c;一些刚入行的程序员会感觉 RPC 比较神秘&#xff0c;而一些有多年使用 RP…

如何在Ubuntu 20.04上安装Django并设置开发环境

介绍 (Introduction) Django is a free and open-source web framework written in Python with its core principles being scalability, re-usability and rapid development. It is also known for its framework-level consistency and loose coupling, allowing for indiv…

[职场生存]细节和感觉[一]:细节

[职场生存]细节和感觉[一]&#xff1a;细节 zhengyun_ustc 200701全文链接&#xff1a; [职场生存]细节和感觉[一][职场生存]细节和感觉[二]&#xff1a;细节包括哪些部分&#xff1f;[职场生存]细节和感觉[三]&#xff1a;感觉刚刚进入软件行业的时候&#xff0c;…

如何在Ubuntu 20.04上安装Anaconda Python发行版

介绍 (Introduction) Anaconda is an open-source package manager, environment manager, and distribution of the Python and R programming languages. It is commonly used for data science, machine learning, large-scale data processing, scientific computing, and …

如何在Ubuntu 20.04上安装和保护Redis [快速入门]

介绍 (Introduction) Redis is an in-memory key-value store known for its flexibility, performance, and wide language support. This quickstart tutorial demonstrates how to install, configure, and secure Redis on an Ubuntu 20.04 server. Redis是一个内存键值存储…

[职场生存]细节和感觉[二]:细节包括哪些部分?

[职场生存]细节和感觉[二]&#xff1a;细节包括哪些部分&#xff1f; zhengyun_ustc 200701前文链接&#xff1a;[职场生存]细节和感觉[一]前面说了细节是一种技巧&#xff0c;并且展示了老外的细节之一。那么到底我所说的细节都包括哪些部分呢&#xff1f;简单地说&#xf…