ubuntu 22.04 配置多版本 php nginx mysql redis 超简单教程

ubuntu 22.04 配置多版本 php nginx mysql redis 超简单教程

1. 安装 Nginx

# 更新系统
sudo apt update
sudo apt upgrade
		
# 安装 nginx
sudo apt install nginx

2. 安装 MySQL

sudo apt install mysql-server
# 启动 MySQL
sudo systemctl start mysql.service

# 进入 MySQL
sudo mysql -uroot -p

# 修改 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'BiLlxxx*2023';
flush privileges;

# 创建远程用户并授权(如果需要)
CREATE USER 'manager'@'%' IDENTIFIED WITH mysql_native_password BY "MasTerxxx*2023";
GRANT ALL PRIVILEGES ON *.* TO 'manager'@'%';
flush privileges;

注意:在撰写本文时,本机MySQL PHP库 mysqlnd 不支持 caching_sha2_authentication ,这是 MySQL 8 的默认身份验证方法。因此,在 MySQL 8 上为 PHP 应用程序创建数据库用户时,您需要确保将它们配置为使用 mysql_native_password 代替。

允许远程访问 MySQL

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0 # 127.0.0.1 修改为 0.0.0.0
# 重启 MySQL
sudo systemctl restart mysql.service

3. 添加 PHP 源

# 用于添加 ppa 源的小工具,ubuntu server 默认没装
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

sudo apt update
sudo apt upgrade

4. 安装 PHP 8.2

# 安装 PHP
sudo apt install php8.2 php8.2-fpm php8.2-common php8.2-mbstring php8.2-xml php8.2-zip php8.2-opcache php8.2-mcrypt php8.2-cli php8.2-gd php8.2-curl php8.2-mysql php8.2-redis php8.2-bcmath php8.2-bz2 php8.2-sqlite3

# 启动 PHP 8.2
sudo systemctl start php8.2-fpm.service

5. 安装 PHP 7.4

# 安装 PHP
sudo apt install php7.4 php7.4-fpm php7.4-common php7.4-json php7.4-mbstring php7.4-xml php7.4-zip php7.4-opcache php7.4-mcrypt php7.4-cli php7.4-gd php7.4-curl php7.4-mysql php7.4-redis php7.4-bcmath php7.4-bz2 php7.4-sqlite3

# 启动 PHP 7.4
sudo systemctl start php7.4-fpm.service

6. 安装 PHP 5.6

# 安装 PHP
sudo apt install php5.6 php5.6-fpm php5.6-common php5.6-json php5.6-mbstring php5.6-xml php5.6-zip php5.6-opcache php5.6-mcrypt php5.6-cli php5.6-gd php5.6-curl php5.6-mysql php5.6-redis php5.6-bcmath php5.6-bz2 php5.6-sqlite3 

# 启动 PHP 5.6
sudo systemctl start php5.6-fpm.service

7. 安装 Redis

sudo apt install redis

# 启动 Redisß
sudo systemctl start redis-server.service

8. 配置 Nginx

vim /etc/nginx/conf.d/xxx.test.conf
server {
    listen 80;
    server_name xxx.test;
    root /www/xxx-web/public;
    # 注意这个error_log,有些错误可能页面不会展示出来,但是可以通过nginx的error_log查询出来
    # error_log /www/xxx.test.error.log;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index  index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass   unix:/run/php/php8.2-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
         deny all;
    }
}

你可能感兴趣的:(mysql,php,ubuntu,nginx,redis)