nginx+php+mysql

dafenqi
2023-11-20 / 0 评论 / 37 阅读 / 正在检测是否收录...

nginx

下载安装

https://nginx.org/download

 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

启动

/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文件

建立软链接

建立软链接目的是省去根目录快捷操作nginx

[root@xxx opt]# ln -s /www/nginx/sbin/nginx /usr/sbin/
[root@xxx opt]# nginx -v
nginx version: nginx/1.25.3

# 删除软连接
rm -rf /usr/sbin/nginx

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

/scripts$fastcgi_script_name** 改成 **$document_root$fastcgi_script_name

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;
        }

重启nginx

nginx -t
nginx -s reload
PS:如果还报错 [error] 37432#0: *85 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream 那一定是php文件路径不对

php

yum方式安装

用yum方式安装php最新版

1 安装 epel-release源和 源管理工具yum-utils

yum -y install epel-release yum-utils

2 安装Remi软件源

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

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

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

yum-config-manager --enable remi-php80    #启用remi源的php8.0模块
注:如果显示没有这个命令,则需要先安装yum -y install yum-utils 插件。
安装php及相关扩展:
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

[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

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

[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源
[root@bunian tmp]# rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm
2.2 下载rpm源文件再安装
[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版本

[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.去除公钥检索校验 (最重要的地方)

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

#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

[root@bunian tmp]# yum install mysql-community-server

6.配置mysql

[root@bunian tmp]# vim /etc/my.cnf
[mysqld]
# 不区分大小写
lower_case_table_names=1
# 端口号
port = 3306

lower_case_table_names

7.启动mysql

[root@bunian tmp]# systemctl start mysqld

8.修改mysql root密码及外网访问

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

# 密码是: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修改密码
[root@bunian etc]# mysql -h localhost -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.35

Copyright (c) 2000, 2023, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpasswd';
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 数据库的 user 表中查看当前 root 用户的相关信息

select host, user, authentication_string, plugin from user;

执行完上面的命令后会显示一个表格
查看表格中 root 用户的 host,默认应该显示的 localhost,只支持本地访问,不允许远程访问。

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

update user set host='%' where user='root';
GRANT ALL ON *.* TO 'root'@'%';
GRANT ALL ON *.* TO 'root'@'%';
执行两次 GRANT ALL ON . TO 'root'@'%';

刷新权限

mysql> flush privileges;

9.检验是否安装成功

# 查看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 -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
$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: 测试文章
0

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/testblog.58heshihu.com/var/Widget/Archive.php on line 1032

评论 (0)

取消