本文搭建的 LNMP 环境软件组成版本及说明如下:
Linux:Linux 系统,本文以 Ubuntu 22.04 为例。
Nginx:Web 服务器程序,用来解析 Web 程序,本文以 Nginx 1.18.0 为例。
MySQL:一个数据库管理系统,本文以 MySQL 8.0.39 为例。
PHP:Web 服务器生成网页的程序,本文以 PHP 8.1.2 为例。
一、安装环境
1、安装配置 Nginx
因为需要一些插件,apt直接安装会缺,所以这边用源码编译安装
# 首先卸载现有的 Nginx
apt remove nginx nginx-common nginx-full
apt autoremove
# 安装编译所需的依赖
apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# 下载 Nginx 源码
mkdir ~/nginx-build && cd ~/nginx-build
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz
# 下载 fancyindex 模块
git clone https://github.com/aperezdc/ngx-fancyindex.git
# 编译和安装 Nginx
cd nginx-1.24.0
./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www-data \
--group=www-data \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-threads \
--add-module=../ngx-fancyindex
make
make install
# 注册系统服务
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
# 刷新配置,启动服务
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
2、安装配置 MySQL
# 更新 Ubuntu 系统内的软件包
apt update
# 执行以下命令,安装 mysql
apt -y install mysql-server
# 查看 MySQL 版本
mysql -V
# 首次登录设置root密码
mysql -u root -p
3、安装配置 PHP
# 更新 Ubuntu 系统内的软件包
apt update
# 执行以下命令,安装PHP
apt -y install php-fpm
# 查看 PHP 版本
php -v
# 启动PHP
systemctl start php8.1-fpm
# 查看已安装插件
php -m
# 安装常用php扩展
apt update
apt install php-mbstring
apt install php-mysqli
apt install php-mysql
apt install php-sqlite3
# 重启服务
systemctl start php8.1-fpm
4、安装docker
apt update
apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
docker version
docker compose version
systemctl enable docker
二、nginx配置示例
1、nginx上传文件大小限制配置
在nginx.conf
中增加下面两行配置,否则有些上传如alist程序会报错:
client_max_body_size 300m;
client_header_timeout 5m;
# 再在http里面加入utf8编码设置
http {
...
charset utf-8,gbk;
...
}
2、用到PHP的相关配置
# typecho博客配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 域名;
# 强制301定向到https
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name 域名;
# 证书配置
ssl_certificate /path/cert.crt;
ssl_certificate_key /path/cert.key;
root /root/www/typecho/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
# 处理静态文件请求(可选)
location / {
try_files $uri $uri/ /index.php?$args;
}
# PHP8.1的配置
location ~ .*\.php(\/.*)*$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
}
}
3、反向代理的相关配置
# 反向代理
server {
listen 80;
server_name 域名;
return 301 https://$host$request_uri; # 强制 HTTPS
}
server {
listen 443 ssl;
server_name 域名;
# 证书配置
ssl_certificate /path/cert.crt;
ssl_certificate_key /path/cert.key;
location / {
proxy_pass http://localhost:10001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
4、挂载目录的相关设置
下载并配置主题:
git clone https://github.com/Naereen/Nginx-Fancyindex-Theme.git
# light主题
mv Nginx-Fancyindex-Theme/Nginx-Fancyindex-Theme-light /分享路径目录/
# 暗黑主题
mv Nginx-Fancyindex-Theme/Nginx-Fancyindex-Theme-dark /分享路径目录/
修改nginx配置,之后重启nginx服务即可。
server {
listen 80;
server_name 域名;
return 301 https://$host$request_uri; # 强制 HTTPS
}
server {
listen 443 ssl;
server_name 域名;
ssl_certificate /path/cert.crt;
ssl_certificate_key /path/cert.key;
location / {
alias /共享路径/;
autoindex on; # 启用目录列表
charset utf-8,gbk; # 编码支持
fancyindex on; # 启用插件
fancyindex_exact_size off; # 不显示精确大小
fancyindex_localtime on; # 使用本地时间
# 下面三行是启用主题并屏蔽掉主题目录
fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
fancyindex_ignore "Nginx-Fancyindex-Theme-dark";
}
}