lnmp环境部署极简保姆级教程(nginx+php+mysql)

lnmp极简保姆级教程(nginx+php+mysql)

nginx

下载安装

https://nginx.org/download

bash 复制代码
 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel #编译环境

cd /opt
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
mkdir -p /www/nginx
./configure --prefix=/www/nginx
make && make install

启动

bash 复制代码
/www/nginx/sbin/nginx

常用指令

nginx -V 查看版本,以及配置文件地址

nginx -v 查看版本

nginx -c filename 指定配置文件

nginx -h 帮助

nginx -s reload|reopen|stop|quit // 重新加载配置|重启|停止|退出 nginx")重新加载配置|重启|停止|退出 nginx

nginx -t //查看配置是否有语法错误

nginx -c /usr/local/etc/nginx/nginx.conf //启动是加载指定nginx.conf文件

安装启动好php-fpm后,修改nginx.conf

bash 复制代码
location ~ \.php$ {
            root           /www/nginx/php;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #支持解析php文件
            include        fastcgi_params;
        }

php

yum方式安装

用yum方式安装php最新版

1 安装 epel-release源和 源管理工具yum-utils
bash 复制代码
yum -y install epel-release yum-utils
2 安装Remi软件源

Remi软件源官方地址: https://rpms.remirepo.net/

CentOS7安装:
bash 复制代码
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
CentOS7启动php模块并安装:

yum启动php模块,根据需要选择自己格式的版本,如remi-php74模块,就是php7.4版本。:

bash 复制代码
yum-config-manager --enable remi-php80    #启用remi源的php8.0模块

注:如果显示没有这个命令,则需要先安装yum -y install yum-utils 插件。

安装php及相关扩展:
bash 复制代码
yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis

安装成功:php -v 查看版本为php8.0版本

3 启动php-fpm
bash 复制代码
[root@xx logs]# whereis php-fpm
php-fpm: /usr/sbin/php-fpm /etc/php-fpm.d /etc/php-fpm.conf /usr/share/man/man8/php-fpm.8.gz

[root@xx logs]# systemctl start php-fpm

mysql

yum安装mysql
MySQL三种安装方法(yum安装、编译安装、二进制安装)

yum安装mysql

1.卸载旧版mysql

如果安装过先卸载,第一次安装略过此步

bash 复制代码
[root@bunian etc]# rpm -qa | grep mysql
mysql-community-common-5.7.38-1.el7.x86_64
mysql-community-client-5.7.38-1.el7.x86_64
mysql80-community-release-el7-6.noarch
mysql-community-server-5.7.38-1.el7.x86_64
mysql-community-libs-5.7.38-1.el7.x86_64
# 所有组件都要删除
[root@bunian etc]# rpm -e --nodeps mysql-community-common-5.7.38-1.el7.x86_64
[root@bunian etc]# rpm -e --nodeps mysql-community-client-5.7.38-1.el7.x86_64
[root@bunian etc]# rpm -e --nodeps mysql80-community-release-el7-6.noarch
[root@bunian etc]# rpm -e --nodeps mysql-community-server-5.7.38-1.el7.x86_64
[root@bunian etc]# rpm -e --nodeps mysql-community-libs-5.7.38-1.el7.x86_64
# 清理yum headers cache
[root@bunian etc]# yum clean all
2.安装
2.1 rpm安装yum源
bash 复制代码
[root@bunian tmp]# rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm
2.2 下载rpm源文件再安装
bash 复制代码
[root@bunian tmp]# wget https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm
[root@bunian tmp]# yum localinstall mysql80-community-release-el7-6.noarch.rpm
3.选择mysql版本
bash 复制代码
[root@bunian tmp]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64           MySQL Cluster 7.5 Comm 禁用
mysql-cluster-7.5-community-source           MySQL Cluster 7.5 Comm 禁用
mysql-cluster-7.6-community/x86_64           MySQL Cluster 7.6 Comm 禁用
mysql-cluster-7.6-community-source           MySQL Cluster 7.6 Comm 禁用
mysql-cluster-8.0-community/x86_64           MySQL Cluster 8.0 Comm 禁用
mysql-cluster-8.0-community-debuginfo/x86_64 MySQL Cluster 8.0 Comm 禁用
mysql-cluster-8.0-community-source           MySQL Cluster 8.0 Comm 禁用
mysql-connectors-community/x86_64            MySQL Connectors Commu 启用:    192
mysql-connectors-community-debuginfo/x86_64  MySQL Connectors Commu 禁用
mysql-connectors-community-source            MySQL Connectors Commu 禁用
mysql-tools-community/x86_64                 MySQL Tools Community  启用:     90
mysql-tools-community-debuginfo/x86_64       MySQL Tools Community  禁用
mysql-tools-community-source                 MySQL Tools Community  禁用
mysql-tools-preview/x86_64                   MySQL Tools Preview    禁用
mysql-tools-preview-source                   MySQL Tools Preview -  禁用
mysql57-community/x86_64                    MySQL 5.7 Community Se 禁用
mysql57-community-source                     MySQL 5.7 Community Se 禁用
mysql80-community/x86_64                     MySQL 8.0 Community Se 启用:    343
mysql80-community-debuginfo/x86_64           MySQL 8.0 Community Se 禁用
mysql80-community-source                     MySQL 8.0 Community Se 禁用

默认是开启8.0版本

4.去除公钥检索校验 (最重要的地方)

这个地方容易出错,原因是没有去除校验导致服务安装后无法启动

bash 复制代码
#gpgcheck改成0
[root@bunian tmp]# vim /etc/yum.repos.d/mysql-community.repo
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
5.安装mysql
bash 复制代码
[root@bunian tmp]# yum install mysql-community-server
6.配置mysql
bash 复制代码
[root@bunian tmp]# vim /etc/my.cnf
[mysqld]
# 不区分大小写
lower_case_table_names=1
# 端口号
port = 3306
7.启动mysql
bash 复制代码
[root@bunian tmp]# systemctl start mysqld
8.修改mysql root密码及外网访问

初始密码在这里/var/log/mysqld.log

bash 复制代码
# 密码是:6;-#?gn*q;0H
[root@bunian etc]# grep 'temporary password' /var/log/mysqld.log
2022-06-21T03:25:39.021858Z 1 [Note] A temporary password is generated for root@localhost: 6;-#?gn*q;0H
登录mysql修改密码
bash 复制代码
[root@bunian etc]# mysql -h localhost -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.38 MySQL Community Server (GPL)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpasswd';
修改外网访问

在 mysql 数据库的 user 表中查看当前 root 用户的相关信息

mysql 复制代码
select host, user, authentication_string, plugin from user;

执行完上面的命令后会显示一个表格

查看表格中 root 用户的 host,默认应该显示的 localhost,只支持本地访问,不允许远程访问。

授权 root 用户的所有权限并设置远程访问

mysql 复制代码
update user set host='%' where user='root';
GRANT ALL ON *.* TO 'root'@'%';
GRANT ALL ON *.* TO 'root'@'%';

执行两次 GRANT ALL ON . TO 'root'@'%';

刷新权限

mysql 复制代码
mysql> flush privileges;
9.检验是否安装成功
bash 复制代码
# 查看mysql端口号
[root@bunian etc]# netstat -nltp | grep 3306
tcp6       0      0 :::33060                :::*                    LISTEN      1292/mysqld         
tcp6       0      0 :::3306                 :::*                    LISTEN      1292/mysqld    
10.Navicat连接
用Navicat连接报错:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded
原因以及解决办法

由于目前已有的客户端连接软件还不支持Mysql8新增加的加密方式: caching_sha2_password

所以我们需要修改用户的加密方式,将其改为老的加密验证方式: mysql_native_password

mysql 复制代码
#登录
mysql -h localhost -p
 
#选择数据库
use mysql; 
 
# 注意:先 select host, user from user; 查看host值再决定以下是'root'@'%' 还是 'root'@'localhost'
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的数据库密码';
 
#刷新权限
FLUSH PRIVILEGES; 
11.php连接mysql

连接Navicat创建test数据库、new表

vim /www/nginx/php/index.php

php 复制代码
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 执行查询
$sql = "SELECT * FROM new";
$result = $conn->query($sql);

// 输出数据
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 结果";
}
//访问ip/index.php
id: 1 - name: 测试文章
相关推荐
Flying_Fish_roe3 小时前
linux-网络管理-网络配置
linux·网络·php
运维小白。。3 小时前
Nginx 反向代理
运维·服务器·nginx·http
sleP4o5 小时前
Python操作MySQL
开发语言·python·mysql
大熊程序猿5 小时前
python 读取excel数据存储到mysql
数据库·python·mysql
知识分享小能手6 小时前
mysql学习教程,从入门到精通,SQL DISTINCT 子句 (16)
大数据·开发语言·sql·学习·mysql·数据分析·数据库开发
lamb张6 小时前
MySQL锁
数据库·mysql
卑微的码蚁7 小时前
ngxin
nginx
躺平的花卷7 小时前
Python爬虫案例六:抓取某个地区某月份天气数据并保存到mysql数据库中
数据库·爬虫·python·mysql
飞翔的佩奇9 小时前
xxl-job适配sqlite本地数据库及mysql数据库。可根据配置指定使用哪种数据库。
数据库·spring boot·mysql·sqlite·xxl-job·任务调度