如何在 Ubuntu VPS 安装LEMP(Nginx,MariaDB,PHP8.1)

购买VPS之后,如果需要在VPS上搭建WordPress或者其它网站项目,需要安装一些应用程序来运行网站项目,用的比较多的是LEMP技术栈,LEMP是一套开源软件的组合,具体是指Linux、Nginx、Mysql、PHP,主要用来构建web服务器,简单来说就是让网站运行起来的基础环境。

第一步: 更新系统

首先更新系统,以确保所有安装的软件包都是最新的。Ubuntu系统可以通过以下命令来更新。

apt update

apt upgrade

第二步: 安装Nginx

使用以下命令来安装Nginx服务器

apt install nginx

输出以下内容:

root@localhost:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
nginx is already the newest version (1.22.0-1ubuntu3).

安装完成后,启用Nginx(在系统启动时自动启动),启动Web服务器,并使用以下命令验证状态。

systemctl start nginx

systemctl enable nginx

systemctl status nginx

输出

 root@localhost:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enable>
     Active: active (running) since Thu 2023-04-13 19:05:38 UTC; 33min ago
       Docs: man:nginx(8)
    Process: 1895 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; ->
   Main PID: 604 (nginx)
      Tasks: 5 (limit: 3386)
     Memory: 13.6M
        CPU: 210ms
     CGroup: /system.slice/nginx.service
             ├─ 604 "nginx: master process /usr/sbin/nginx -g daemon on; master>
             ├─1896 "nginx: worker process"
             ├─1897 "nginx: worker process"
             ├─1898 "nginx: worker process"
             └─1899 "nginx: worker process"

检查Nginx版本,

nginx -v

输出:

root@localhost:~# nginx -v
nginx version: nginx/1.22.0 (Ubuntu)

通过访问服务器的IP地址,验证Web服务器是否正在运行并可访问。
在浏览器输入:

http://IP_address
如何在 Ubuntu VPS  安装LEMP(Nginx,MariaDB,PHP8.1)

我们需要让用户Nginx成为web目录的所有者。默认情况下,它由根用户所有。

chown www-data:www-data /usr/share/nginx/html -R

第二步: 安装 MariaDB 数据库

MariaDB是一种流行的数据库服务器。安装很简单,只需要几个步骤,如图所示。

apt install mariadb-server mariadb-client

安装完成后,启用MariaDB(在系统启动时自动启动),启动MariaDB,并使用以下命令验证状态。

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

最后,通过以下命令来确保MariaDB安装的安全。

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.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

一旦安全,您就可以连接到MySQL,并使用以下命令查看数据库服务器上的现有数据库。

mysql -e "SHOW DATABASES;" -p

第三步: 安装PHP 8.1

安装PHP8.1 使用以下命令:

apt install php php-fpm php-mysql php-common php-cli php-common php-json php-opcache php-readline php-mbstring php-xml php-gd php-curl

设置开机启动及启用 php8.1-fpm

systemctl start php8.1-fpm

systemctl enable php8.1-fpm

检查php8.1-fpm的状态

systemctl status php8.1-fpm

第四步: Nginx配置站点

首先删除默认配置

rm /etc/nginx/sites-enabled/default

在/etc/nginx/conf.d/目录下创建新的文件。

nano /etc/nginx/conf.d/default.conf

添加以下内容:

server {
  listen 80;
  listen [::]:80;
  server_name _;
  root /var/www/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

 # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
  }

  # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;
  }
}

接下来,进行测试以确保任何Nginx文件中都没有语法错误。

nginx -t

如果没有任何问题,重新启动Nginx。

systemctl reload nginx

使用NginxWeb服务器测试PHP-FPM,我们需要在webroot目录中创建一个info.PHP文件。

nano /var/www/html/info.php

添加以下代码:

<?php phpinfo(); ?>

在浏览器访问 http://yourserver-ip-address/info.php , 会在浏览器显示以下内容,就表示安装成功了。

如何在 Ubuntu VPS  安装LEMP(Nginx,MariaDB,PHP8.1)

原创文章,作者:zhuji001,如若转载,请注明出处:https://www.zhuji66.com/how-to-install-lemp-in-vps/

本站分享VPS和云服务器信息均来源于网络,如有侵权请邮箱联系zhuji66com@yeah.net。本站不销售任何产品,如遇问题请联系对应客服。