由于网上大多安装LNMP环境教程新旧不一。为了记录,此篇Ubuntu20.04LTS安装LNMP环境教程。
1、 安装nginx
1.1 更新可用软件包列表
apt update && apt upgrade && apt autoremove
1.2 安装nginx
sudo apt install nginx
1.3 启动、设置开机启动nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
1.4 在本地浏览器中访问以下地址,查看 Nginx 服务是否正常运行
http://实例服务器地址IP
2、 安装数据库
sudo apt install mariadb-server mariadb-client
2.1 执行以下命令,启动mariadb
systemctl start mariadb
2.2 执行以下命令,开机启动mariadb
systemctl enable mariadb
2.3 验证mariadb是否安装成功、查看状态
systemctl status mariadb
sudo mysql
3、 安装PHP
3.1 安装PHP
sudo apt install php7.4-cli php7.4-common php7.4-mysqlnd php7.4-fpm php7.4-mysql php7.4-curl php7.4-json php7.4-cgi php7.4-xsl
3.2 启动PHP
systemctl start php7.4-fpm
3.3 设置开机启动PHP
systemctl enable php7.4-fpm
3.4 查看PHP状态
systemctl status php7.4-fpm
3.5 修改Nginx配置以支持PHP
sudo nano /etc/nginx/sites-available/default
Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
}
3.6 修改之后重启Nginx,即可配置好Nginx和PHP:
sudo nginx -s reload
3.7 验证环境
sudo nano /var/www/html/index.php
输入 ,按下CTRL+O保存,CTRL+X退出。
执行以下命令,重启Nginx服务
sudo systemctl restart nginx
然后在本地浏览器中访问如下地址,查看环境配置是否成功。
http://实例服务器地址IP
4、安装phpMyAdmin最新版
4.1 使用apt安装
sudo apt update
sudo apt install phpmyadmin
4.2 建立软连接
sudo ln -s /usr/share/phpmyadmin /var/www/html
4.3手动安装
去phpmyadmin官网下载最新源码,并解压到web根目录下(/var/www/html/)
sudo apt update && sudo install unzip -y
sudo unzip -d /var/www/html phpMyAdmin-5.1.3-all-languages.zip
sudo mv phpMyAdmin-5.1.3-all-languages.zip phpmyadmin
或者
sudo unzip -d /usr/share/phpmyadmin phpMyAdmin-5.1.3-all-languages.zip
sudo mv phpMyAdmin-5.1.3-all-languages.zip phpmyadmin
sudo ln -s /usr/share/phpmyadmin /var/www/html
5、修改数据库密码
执行以下命令,注意这只适用于mariadb
sudo mysql -u root -p
不用输入密码,按下ENTER键直接进入
修改加密方式:
update mysql.user set plugin=’mysql_native_password’;
修改密码:
update mysql.user set password=PASSWORD(“输入你的密码”) where User=’root’;
刷新权限信息:
flush privileges;
这时候登录数据库需要密码了。
6、防ssh爆破简单防护
#以Ubuntu为例
apt-get install fail2ban
#直接复制粘贴全部到SSH窗口即可
#配置参数
fail2ban=”/etc/fail2ban/jail.d/sshd.local”
cat>”${fail2ban}”<<EOF
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
logpath = /var/log/auth.log # 如果是Centos路径要改/var/log/secure这个
maxretry = 10 # 最多连接10次失败
bantime = 777600 # 禁止9天(按秒单位1天86400)
EOF
#启动&开机自启
systemctl start fail2ban.service
systemctl enable fail2ban.service
systemctl status fail2ban.service
ALL done Enjoy it