Ubuntu 22.04 LTS 安装lnmp
Ubuntu 22.04 LTS 安装最新稳定版本nginx、mysql5.7和php7.2
全部apt-get安装,就是快,迅速。前提是需要在有网络环境的情况下哈!!
操作系统版本:Ubuntu 22.04 LTS
一、安装最新稳定版本的nginx
apt-get update
# 查看默认安装的nginx版本(默认为1.18。有点老,我们安装最新稳定版本)
apt-cache policy nginx
# 修改apt源,在最后添加(:wq保存退出)
vim /etc/apt/sources.list
# NGINX
deb http://nginx.org/packages/ubuntu jammy nginx
deb-src http://nginx.org/packages/ubuntu jammy nginx
# 导入nginx公钥
wget https://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
# 更新apt源
apt-get update
# 再次查看默认的nginx版本(第一个已经是最新稳定的nginx版本)
apt-cache policy nginx
# 安装nginx
apt-get install nginx
# 查看nginx版本加入开机启动
nginx -V
systemctl enable nginx
systemctl is-enabled nginx
至此ningx安装完成
二、安装mysql5.7版本
可以参考:https://www.cnblogs.com/wltian-one/p/17767082.html
基本是一样的。
# 查看系统默认安装的数据库版本(ubuntu 22.04自带的版本是8.0.35,而我们需要安装mysql5.7的版本就需要换源)
apt-cache policy mysql-server
# 备份原来的sorce文件
cp /etc/apt/sources.list /etc/apt/sources.list_back
# (dG全部删除)
vim /etc/apt/sources.list
# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# 导入mysql公钥
gpg --keyserver keyserver.ubuntu.com --recv 3B4FE6ACC0B21F32
gpg --export --armor 3B4FE6ACC0B21F32 | apt-key add -
# 更新
apt-get update
# 再次查看默认mysql版本
apt-cache policy mysql-server
# 安装mysql
apt-get install mysql-server-5.7
# 查看mysql是否安装成功
dpkg -l | grep mysql
# 设置开机自启
systemctl enable mysql
systemctl is-enabled mysql
# 修改mysql密码(直接输入mysql,此时不需要输入密码)
mysql
msyql>use mysql;
mysql> alter user root@'localhost' identified with mysql_native_password by "12345678";
mysql> flush privileges;
mysql> exit
# 把镜像源重新换回
rm -f /etc/apt/sources.list
mv /etc/apt/sources.list_back /etc/apt/sources.list
apt-get update
退出后重新进入就需要输入密码了!
三、修改mysql数据库数据存储位置(按照实际需求来,可选)
1、先停止mysql服务
systemctl stop mysql
2、创建新的数据存储目录
mkdir /home/mysql
cp -ar /var/lib/mysql/* /home/mysql
chown -R mysql:mysql /home/mysql
3、修改mysql配置文件
# 先备份一份
cp -a /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf_back
vim /etc/mysql/mysql.conf.d/mysqld.cnf
datadir = /home/mysql
注意:只是修改MySQL的配置文件是不行的,因为Ubuntu有访问控制系统apparmor,所以还需要修改与该访问控制系统相关的文件
4、修改安全保护文件
cp -a /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/usr.sbin.mysqld_back
vim /etc/apparmor.d/usr.sbin.mysqld
把带有/var/lib/mysql
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
改成
/home/mysql/ r,
/home/mysql/** rwk,
5、修改访问控制文件
cp -a /etc/apparmor.d/abstractions/mysql /etc/apparmor.d/abstractions/mysql_back
vim /etc/apparmor.d/abstractions/mysql
/var/lib/mysql{,d}/mysql{,d}.sock rw,
改成
/home/mysql{,d}/mysql{,d}.sock rw,
vim /etc/apparmor.d/tunables/alias
#最后添加
alias /var/lib/mysql/ -> /home/mysql/,
6、重启apparmor服务
systemctl restart apparmor
7、开启数据库
systemctl start mysql
8、查看数据库的状态
systemctl status mysql
至此mysql数据库安装完成
四、安装php7.2
1、安装 SURY APT 存储库
# 查看默认安装的php
apt-cache policy php
# 要在 Ubuntu 22.04 上安装 PHP 7.1/7.2/7.3/7.4,您需要安装 SURY,即提供 PHP 包的第三方存储库。
apt-get -y install software-properties-common
add-apt-repository ppa:ondrej/php --yes
apt-get update
# 查看
apt list|grep php7.2-*
2、安装php7.2
apt-get -y install php7.2
下载了php7.2会自动下载apache2,这时候需要我们删除apache2
# 查看apache2
dpkg -l |grep apache2
# 删除
apt-get autoremove apache2
apt-get autoremove apache2-bin
find /etc -name "*apache*" |xargs rm -rf
rm -rf /var/www
rm -rf /etc/libapache2-mod-jk
rm -rf /etc/init.d/apache2
rm -rf /etc/apache2
# 删除关联
dpkg -l |grep apache2|awk '{print $2}'|xargs dpkg -P
# 再次查看apache2
dpkg -l |grep apache2
# 安装扩展(安装自己的需求)
apt-get install php7.2-curl php7.2-dev php7.2-gd php7.2-mysql php7.2-xml php7.2-xdebug php7.2-redis php7.2-apcu php7.2-intl php7.2-memcached
# 查看
php -v
php -m
dpkg -l | grep php7.2*
# 开机自启
systemctl enable php7.2-fpm
至此php7.2安装成功
四、nginx调用php7.2
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}