ubuntu24.04配置多版本php,nginx,mysql,redis

本文最后更新于:20 小时前

ubuntu24.04配置多版本php,nginx,mysql,redis

1. 安装 Nginx

更新系统

1
2
sudo apt update
sudo apt upgrade

安装 nginx

1
sudo apt install nginx
1
2
3
4
5
6
7
8
# 启动nginx命令
sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# 设置开机自启动
sudo systemctl enable nginx

创建www用户:
使用 adduser 命令来创建一个新用户,同时会自动创建用户组和相应的宿主目录:

1
sudo adduser www

创建用户组
使用 groupadd 命令创建一个新的用户组:

1
sudo groupadd 用户组名

将用户加入用户组
使用 usermod 命令将现有用户加入到新创建的用户组中:

1
sudo usermod -aG 用户组名 用户名

验证用户组和用户的关系
使用以下命令来查看用户所属的所有用户组,验证用户是否已成功加入新的用户组:

1
groups 用户名

或使用以下命令查看某个用户组下的所有成员:

1
getent group 用户组名
2. 安装 MySQL8.0

安装mysql, 配置文件路径: /etc/mysql/mysql.conf.d/mysqld.cnf

1
sudo apt install mysql-server

启动 MySQL, 停止MySQL,重启MySQL

1
2
3
4
5
6
7
sudo systemctl status mysql.service
sudo systemctl start mysql.service
sudo systemctl stop mysql.service
sudo systemctl restart mysql.service

# 设置开机自启动
sudo systemctl enable mysql.service

进入 MySQL

1
sudo mysql -uroot -p

修改 root 密码

1
2
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxx*2024';
flush privileges;

创建远程用户并授权(如果需要)

1
2
3
CREATE USER 'remote'@'%' IDENTIFIED WITH mysql_native_password BY "xxx*2024";
GRANT ALL PRIVILEGES ON *.* TO 'remote'@'%';
flush privileges;

允许远程访问 MySQL

1
2
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0 # 127.0.0.1 修改为 0.0.0.0
3. 添加PHP源, 安装PHP

用于添加 ppa 源的小工具,ubuntu server 默认没装

1
2
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
安装 PHP 8.2及相关组件
1
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

1
sudo systemctl start php8.2-fpm.service
安装 PHP 7.4及相关组件

安装 PHP

1
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

1
2
3
sudo systemctl start php7.4-fpm.service

sudo systemctl enable php7.4-fpm.service
安装 PHP 5.6及相关组件

安装 PHP

1
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及相关组件

1
sudo systemctl start php5.6-fpm.service
4. 安装 Redis
1
sudo apt install redis

启动 Redis

1
2
3
4
5
6
sudo systemctl status redis-server.service
sudo systemctl start redis-server.service
sudo systemctl stop redis-server.service
sudo systemctl restart redis-server.service
# 开机启动
sudo systemctl enable redis-server.service
5. 配置 Nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
server {
listen 443 ssl;
server_name www.xxxxxx.com;
charset utf-8;
access_log /var/log/nginx/www.xxxxxx.com.access.log;
error_log /var/log/nginx/www.xxxxxx.com.error.log;
ssl_certificate /etc/nginx/conf.d/ssl/certificate.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
index index.html;
ssi on;
set $CLICKSTRN_ID "-";
if ( $http_cookie ~* "CLICKSTRN_ID=(\S+)(;.*|$)"){
set $CLICKSTRN_ID $1;
}
set $motion_id "-";
if ( $http_cookie ~* "motion_id=(\S+)(;.*|$)"){
set $motion_id $1;
}

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
if ($http_user_agent ~ "okhttp" ) {
#return 444;
}
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_min_length 1024;
gzip_types text/css application/x-javascript text/plain text/xml text/javascript application/javascript;

location / {
root /home/www/html/xxxx/current;
add_header 'Access-Control-Allow-Origin' '*';
try_files $uri $uri/ /index.html;

location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/html/xxxx/current/public/index.php;
include fastcgi_params;
}
}

location /apple-app-site-association {
add_header 'Content-Type' 'application/json;charset=UTF-8';
}
location ~ \.mp3 {
}
location ~ \.pdf {
}
}