目录

ubuntu22.04安装nginx,php,mysql

# ubuntu22.04安装nginx,php,mysql

版本(默认)

ubuntu22.04
php 8.1
nginx 1.18.0
mysql 8.0

# 换源

备份原有源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
1

编辑源

sudo rm /etc/apt/sources.list
sudo vim /etc/apt/sources.list
1
2

将源替换为阿里云源

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
1
2
3
4
5
6
7
8
9
10
11

更新

sudo apt update
1

# 安装nginx

sudo apt install nginx
1

配置文件路径: /etc/nginx/nginx.conf(主配置文件),网站配置文件通常位于 /etc/nginx/sites-available/

管理命令

sudo systemctl start nginx    # 启动 Nginx
sudo systemctl enable nginx   # 设置开机自启动
sudo systemctl restart nginx  # 重启 Nginx
1
2
3

# 安装mysql

sudo apt install mysql-server
1

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

默认安装没有密码

管理命令

sudo systemctl start mysql    # 启动 MySQL
sudo systemctl enable mysql   # 设置开机自启动
sudo systemctl restart mysql  # 重启 MySQL
1
2
3

# 安装php

sudo apt install php-fpm php-mysql
1

配置文件路径: /etc/php/8.1/fpm/php.ini

管理命令

sudo systemctl start php8.1-fpm    # 启动 PHP-FPM
sudo systemctl enable php8.1-fpm   # 设置开机自启动
sudo systemctl restart php8.1-fpm  # 重启 PHP-FPM
1
2
3

# 配置nginx

编辑配置文件

sudo vim /etc/nginx/sites-available/default
1

修改配置文件

server {
    listen 80;
    listen [::]:80;

    server_name _;

    root /var/www/html;
    # 添加php类型
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    # 取消注释,修改版本
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
    location ~ /\.ht {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# mysql相关

mysql安装后默认没有密码

sudo mysql
1

设置密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
FLUSH PRIVILEGES;
1
2
最后一次更新于: 2025/01/19, 23:04:33