Ubuntu20.04手动安装LNMP环境

① 我这里用的是aliyun官方的云主机,首先要更新一下apt源(很重要)

sudo apt-get update

#tips: 如果你使用本地机器或虚拟机,需要配置一下apt源
vim /etc/apt/sources.list
#将以下内容写入到文件中
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal main restricted
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal main restricted
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates main restricted
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates main restricted
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal universe
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal universe
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates universe
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates universe
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal multiverse
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal multiverse
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates multiverse
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-updates multiverse
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.cloud.aliyuncs.com/ubuntu focal-security main restricted
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu focal-security main restricted
deb http://mirrors.cloud.aliyuncs.com/ubuntu focal-security universe
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu focal-security universe

apt-update.png

② 安装Nginx和MariaDB

sudo apt-get install -y nginx mariadb-server

nginx-mysql.png

可以看到,这里已经安装完成了

③ 安装php

sudo apt-get install -y php-fpm php-mysql

php.png

④ 编辑nginx配置文件

nginx的配置文件有三个位置

/etc/nginx/conf.d/xxx.conf

/etc/nginx/site-available/xxx

/etc/nginx/nginx.conf

这里我建议使用第二个写配置文件

server {
    listen 80;
    listen [::]:80;
    server_name localhost;
    root /var/www/html;
    location / {
        index index.html index.htm index.shtml index.php
    }
}

⑤ 测试

#这里我们使用默认web目录 /var/www/html/
sudo chown -R $USER:$USER /var/www/html/
cd /var/www/html
sudo echo "<?php phpinfo(); ?>" > index.php
#访问网页 成功解析

sudo mysql_secure_installation
#首次运行会让我们输入密码 我们一路回车 然后输入自定义密码即可
mysql -u root -p
#登入mysql终端

这次安装过程中踩了无数的坑,总结出一个100%安装的方法。

最后我还是选择了apache.