如何在Debian云服务器上安装Nginx、PHP和MySQL?

  • 发布时间:2022-01-23 09:41:57
  • 阅读次数:986

LEMP平台是由Linux、Nginx、MySQL/MariaDB和PHP四种软件组成,和LAMP平台相似,只是其中的Web服务器Apache换成了Nginx。Nginx在性能和安全性方面相比其他Web服务器有很多优势。

安装软件

用一条命令安装Nginx、MariaDB和PHP。

$ sudo apt install nginx mariadb-server php-fpm php-mysql

配置Nginx

编辑站点配置文件,把example.com替换为实际域名。

$ sudo nano /etc/nginx/sites-available/example.com

在配置文件中添加如下代码,同样要把example.com替换为实际域名。

server {
        listen 80;
        listen [::]:80;

        server_name example.com;

        root /var/www/html;
        index index.html index.htm index.php;

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

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

启用该配置文档,并重载Nginx。

$ sudo ln -sf /etc/nginx/sites-{available,enabled}/example.com
$ sudo systemctl reload nginx

配置MariaDB

运行MariaDB安装脚本。

$ mysql_secure_installation

根据提示回答以下问题,通常按照默认设置即可。

Enter current password for root (enter for none):
Set root password? [Y/n] 
Remove anonymous users? [Y/n]
Press enter to disable root logins from remote machines.
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]

测试服务器

创建一个PHP测试脚本。

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

添加如下代码并保存。

<?php
phpinfo();

然后在浏览器中访问:https://example.com/test.php 。如果能够看到PHP版本信息,说明配置成功。

最后删除测试脚本,开始正式部署应用程序。

$ sudo rm -f /var/www/html/test.php

【全文完】

< 上一篇:如何在Fedora云服务器上安装Apache、PHP和MySQL? 下一篇:如何在Ubuntu云服务器上安装Nginx、PHP和MySQL? >