部署LNMP环境(CentOS 7)

步骤一:

1.安装Nginx

运行以下命令,安装Nginx。

sudo yum -y install nginx

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

nginx -v

 

2.安装MySQL

使用以下命令更新系统:

sudo yum update

安装 MySQL Yum Repository:MySQL 提供了一个 Yum 存储库,您可以从中安装 MySQL。您需要导入 MySQL 的 Yum 存储库配置。在终端中运行以下命令:

sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

安装 MySQL运行以下命令:

sudo yum install -y mysql-community-server

3.安装PHP

1.添加PHP软件源

yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install -y 
yum-utilsyum-config-manager --enable remi-php80

2.通过yum命令安装php相关软件包

yum -y install -y php php-cli php-fpm php-common php-mysqlphp-gd

步骤二:配置Nginx,Mysql,PHP

1.配置Nginx

 (1)启用Nginx 服务:

systemctl enable nginx 
systemctl start nginx 

(2)修改Nginx配置文件

提前备份:

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

a.打开文件

vim /etc/nginx/nginx.conf

b.修改信息

        location / {
            index index.php index.html index.htm;
        }
        #添加下列信息,配置Nginx通过fastcgi方式处理您的PHP请求。
        location ~ .php$ {
            root /usr/share/nginx/html;    #将/usr/share/nginx/html替换为您的网站根目录,本文使用/usr/share/nginx/html作为网站根目录。
            fastcgi_pass 127.0.0.1:9000;   #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;   #Nginx调用fastcgi接口处理PHP请求。
        }

 

c.关闭并保存配置文件

(3) 启动Nginx服务

sudo systemctl start nginx 

启动失败:通常是因为端口 80被占用。可尝试修改端口。

(4)验证Nginx,浏览器输入http://云服务器实例的公网IP

 2.配置Mysql

 (1)安装MySQL 服务器

yum -y install mysql-community-server

启动MySQL 服务

systemctl start mysqld

设置开机自启

systemctl enable mysqld

获取临时密码

grep 'temporary password' /var/log/mysqld.log

在下一步重置root用户密码时,会使用该初始密码。 

(2)设置 MySQL 密码

首次登录 MySQL 时,您需要使用上一步骤中检索到的临时密码。登录后,您将被要求更改密码。输入以下命令并按照提示操作:

mysql_secure_installation
Securing the MySQL server deployment.
 
Enter password for user root: #输入上一步获取的root用户初始密码
 
The existing password for the user account root has expired. Please set a new password.
 
New password: #输入新密码。长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号包含()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
 
Re-enter new password: #确认新密码。
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
 
Estimated strength of the password: 100 #返回结果包含您设置的密码强度。
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #您需要输入Y以确认使用新密码。
 
#新密码设置完成后,需要再次验证新密码。
New password:#再次输入新密码。
 
Re-enter new password:#再次确认新密码。
 
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #您需要输入Y,再次确认使用新密码。

3.配置PHP

(1)检查PHP 版本:

php -v

(2)启动PHP-FPM 服务:

systemctl enable php-fpm
systemctl start php-fpm

(3)重新启动:

Nginx:systemctl restart nginx

(4)验证PHP安装

sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php

(5)查看版本

php -v

 

步骤三: 测试检验

 1.验证LNMP是否部署成功

 访问 http://公网IP/phpinfo.php,您应该能够看到 PHP 8.0 的信息页面。

 

2.验证php中数据库访问是否正常

vim /usr/share/nginx/html/mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "xxxx";//替换成root的密码
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接if (!$conn) {die("Connection failed: " . mysqli_connect_error());
}
echo "php连接MySQL数据库成功";
?>

浏览器的地址栏输入http://云服务器实例的公网IP /mysql.php进行访问,输出“php连接MySQL数据库成功”,则互通。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值