当前位置: 首页 > news >正文

Ubuntu22.04部署-LNMP

步骤一:关闭防火墙

1.运行以下命令,检查防火墙当前状态。

sudo ufw status
  • 如果防火墙状态为Status: inactive,则表示防火墙为关闭状态。
  • 如果防火墙状态为Status: active,则表示防火墙为开启状态。

2.可选:关闭防火墙。

如果您的防火墙为开启状态,需要运行以下命令,关闭防火墙并关闭开机自启动防火墙。

sudo ufw disable

说明

如果您想重新开启防火墙并开启开机自启动防火墙,请运行sudo ufw enable命令。

步骤二:安装Nginx

1.运行以下命令,更新Ubuntu系统内的软件包。

sudo apt update

2.运行以下命令,安装Nginx。

sudo apt -y install nginx

3.运行以下命令,查看Nginx版本。

nginx -v

返回结果类似如下所示,表示Nginx已成功安装。

nginx version: nginx/1.18.0 (Ubuntu)

步骤三:安装并配置MySQL

1.运行以下命令,安装MySQL。

sudo apt -y install mysql-server

2.运行以下命令,查看MySQL版本。

mysql -V

返回结果类似如下所示,表示MySQL已成功安装。

mysql  Ver 8.0.36-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

3.配置MySQL。

apt安装后,root用户默认可以无密码登陆

sudo mysql
  • 运行以下命令,设置root用户密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '你的新密码';

本示例中密码以root123为例,示例命令:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'root123';

3.运行以下命令,退出MySQL数据库。

exit;

4.运行以下命令,对MySQL进行安全性配置。

sudo mysql_secure_installation

5.根据命令行提示,依次完成以下配置项。

  • 输入root用户的密码。本示例中输入Mysql@1234
root@localhost:~# sudo mysql_secure_installationSecuring the MySQL server deployment.Enter password for user root: 

说明

在输入密码时,系统为了最大限度地保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。

  • 输入Y,设置密码验证策略。
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?Press y|Y for Yes, any other key for No: Y
  • 根据提示,选择密码验证策略。

本示例输入0。

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
  • 输入Y,更改root用户密码。
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
  • 输入root用户密码。MyNewPass123!
New password:Re-enter new password:Estimated strength of the password: 100
  • 输入Y,确认使用已设置的密码。
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
  • 输入Y删除MySQL自带的匿名用户。
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y
  • 输入Y,禁止MySQL的root用户的远程登录权限。
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? (Press y|Y for Yes, any other key for No) : Y
  • 输入Y,移除test数据库。
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 
  • 输入Y,重新加载授权表。
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

当命令行回显All done!时,表示配置完成。

3.测试登录MySQL数据库。

  • 运行以下命令,登录MySQL数据库。
sudo mysql -uroot -p
  • 在命令行回显的Enter password:后输入已设置的数据库密码。

说明

在输入密码时,系统为了最大限度地保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。

成功登录MySQL数据库后,命令行信息如下所示。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.36-0ubuntu0.22.04.1 (Ubuntu)Copyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
  • 运行以下命令,退出MySQL数据库。
exit;

步骤四:安装并配置PHP

1.运行以下命令,安装PHP与PHP-MySQL模块。

sudo apt -y install php-fpm php-mysql

2.运行以下命令,查看PHP版本。

php -v

返回结果如下所示,表示PHP已成功安装。

PHP 8.1.2-1ubuntu2.17 (cli) (built: May  1 2024 10:10:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.17, Copyright (c), by Zend Technologie

修改Nginx配置文件以支持PHP。

1.运行以下命令,打开Nginx默认的配置文件。

sudo vim /etc/nginx/sites-enabled/default

2.按i进入编辑模式,修改Nginx配置文件。

  • server{}内,找到index开头的配置行,在该行中添加index.php

    nginx-indexphp

  • server{}内找到location ~ \.php$ {},去除以下配置行的注释符号。

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

image

3.按Esc退出编辑模式,然后输入:wq并按Enter键,保存并退出文件。

4.运行以下命令,重启Nginx服务。

sudo systemctl restart nginx.service

配置PHP。

1.运行以下命令,在Nginx网站根目录中,新建phpinfo.php文件。

sudo vim <网站根目录>/phpinfo.php

<网站根目录>为变量,可通过Nginx配置文件查看。本教程中Nginx配置文件为默认文件/etc/nginx/sites-enabled/default,您可以运行cat /etc/nginx/sites-enabled/default命令查看文件内容,其中如下图所示的/var/www/html部分即为网站根目录。

网站根目录

因此,对应的运行命令为:

sudo vim /var/www/html/phpinfo.php

2.按i进入编辑模式,添加以下配置信息。

phpinfo()函数会展示PHP的所有配置信息。

<?php echo phpinfo(); ?>

3.按Esc退出编辑模式,然后输入:wq并按Enter键,保存并退出文件。

4.运行以下命令,启动PHP。

sudo systemctl start php8.1-fpm

步骤八:测试访问PHP配置信息页面

1.在本地Windows主机或其他具有公网访问能力的Windows主机中,打开浏览器。

2.在浏览器的地址栏输入http://<IP地址>/phpinfo.php进行访问。

 

出现图片中内容,就说明搭建成功了!!


文章转载自:

http://I9wpCmro.hxsdh.cn
http://mUpyxVrG.hxsdh.cn
http://LtVTCDBr.hxsdh.cn
http://4qU4ZrAj.hxsdh.cn
http://VrMr1G22.hxsdh.cn
http://L0zXXAG6.hxsdh.cn
http://7lI3ZqRP.hxsdh.cn
http://j7DUx3oc.hxsdh.cn
http://7sV68dEn.hxsdh.cn
http://3hPWpjBG.hxsdh.cn
http://EpyaLAZb.hxsdh.cn
http://AsNuORRm.hxsdh.cn
http://AIaAx8N9.hxsdh.cn
http://HzGMhs2q.hxsdh.cn
http://MI2QJUBQ.hxsdh.cn
http://QOccsTf3.hxsdh.cn
http://Za6njlEg.hxsdh.cn
http://KjFfSl8O.hxsdh.cn
http://3dM6kUGy.hxsdh.cn
http://egb4DEYq.hxsdh.cn
http://5c6Xo5ff.hxsdh.cn
http://M1RoUEZL.hxsdh.cn
http://WE6233Xb.hxsdh.cn
http://MDS7XQQ5.hxsdh.cn
http://91mGssOW.hxsdh.cn
http://nXvYlcl6.hxsdh.cn
http://H8z16LOx.hxsdh.cn
http://RMWsbFug.hxsdh.cn
http://qI48SBMb.hxsdh.cn
http://n14r3G4B.hxsdh.cn
http://www.dtcms.com/a/386066.html

相关文章:

  • Day05_苍穹外卖——Redis店铺营业状态设置
  • C++(list)
  • Toshiba东芝TB67S109AFNAG炒菜机器人的应用体验
  • Parasoft 斩获 AutoSec 2025 优秀汽车 AI 测试创新方案奖,引领行业安全测试革新
  • MoonBit 正式加入 WebAssembly Component Model 官方文档 !
  • 【线性代数:代数余子式】
  • 基于一种域差异引导的对比特征学习的小样本故障诊断方法
  • k8s pod优雅滚动更新实践
  • Day43 嵌入式 中断、定时器与串行通信
  • Flink框架中的窗口类别:时间窗口、计数窗口
  • PayPal将加密货币整合到点对点支付中,打通Web2与Web3?
  • 正则表达式学习
  • IP 打造:如何长期保持表达动力与热情?
  • 网站使用独立ip有什么好处
  • 【保姆级喂饭教程】MySQL修改用户对应IP范围
  • Linux内存管理章节十六:非均匀的内存访问:深入Linux NUMA架构内存管理
  • 【AI论文】3D与四维4D世界建模综述
  • 为 Spring Boot 项目配置 Logback 日志
  • std::initializer_list<int> 和 std::vector<int>
  • untiy之材质纹理的不同效果
  • 定制开发开源AI智能名片S2B2C商城小程序的角色设计及其职责分析
  • 云手机的适配性如何?
  • 开源AI红队工具“Red AI Range“助力发现、分析与缓解AI系统漏洞
  • MyBatis XML开发
  • 《拆解URP管线角色材质失效:从现象到底层的深度排障与优化》
  • 《URP管线中后处理效果的创新应用与优化实践》
  • Tomcat Servlet 执行流程源码解析
  • jenkins审批机器人功能概述-Telegram版
  • 苍穹外卖 —— 环境搭建
  • Jenkins运维之路(Jenkins流水线改造Day02-3-容器项目)