在开始搭建前,确保Debian系统为最新状态,避免因软件包版本冲突导致问题:
sudo apt update && sudo apt upgrade -y
确认服务器具备公网IP(或域名已解析至IP),并具备基本的命令行操作知识。
Nginx作为轻量、高性能的Web服务器,负责处理HTTP请求:
sudo apt install nginx -y
启动Nginx并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证安装:浏览器访问服务器IP,若出现Nginx默认欢迎页面则说明成功。
电商网站需存储商品、订单、用户等数据,推荐使用MariaDB(MySQL分支,兼容性更好):
sudo apt install mariadb-server -y
启动MariaDB并设置开机自启:
sudo systemctl start mariadb
sudo systemctl enable mariadb
安全加固:运行脚本设置root密码、删除匿名用户、禁止远程root登录:
sudo mysql_secure_installation
按照提示操作(如设置强密码、移除测试数据库)。
电商网站需PHP处理动态逻辑(如商品展示、购物车功能),推荐安装PHP 8.2(最新稳定版)及常用扩展:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-bcmath -y
启动PHP-FPM并设置开机自启:
sudo systemctl start php8.2-fpm # 根据实际PHP版本调整(如php7.4-fpm)
sudo systemctl enable php8.2-fpm
编辑Nginx默认站点配置文件,添加PHP处理逻辑:
sudo nano /etc/nginx/sites-available/default
修改server块,关键配置如下(重点关注PHP处理部分):
server {
listen 80;
server_name your_domain.com www.your_domain.com; # 替换为你的域名或IP
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404; # 尝试匹配文件,不存在则返回404
}
location ~ \.php$ { # 处理PHP文件
include snippets/fastcgi-php.conf; # 引入FastCGI配置
fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 与PHP-FPM通信的socket路径(需与安装版本一致)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 传递脚本路径
include fastcgi_params; # 包含通用FastCGI参数
}
location ~ /\.ht { # 禁止访问.htaccess文件(Apache遗留配置)
deny all;
}
}
保存退出后,测试Nginx配置语法:
sudo nginx -t
若无错误,重新加载Nginx:
sudo systemctl reload nginx
将电商网站代码(如WordPress、Shopify自定义主题或自主开发的PHP项目)上传至/var/www/html目录(或自定义目录,需同步修改Nginx配置中的root参数)。可使用SCP、SFTP等工具:
scp -r local_website/ user@your_server_ip:/var/www/html
或使用Git克隆:
cd /var/www/html
sudo git clone https://github.com/your-repo/ecommerce-website.git .
注意:确保代码目录权限正确,避免Nginx无法读取文件:
sudo chown -R www-data:www-data /var/www/html # 将所有者设为Nginx用户(www-data)
sudo find /var/www/html -type d -exec chmod 755 {} \; # 目录权限755
sudo find /var/www/html -type f -exec chmod 644 {} \; # 文件权限644
登录MySQL,为电商网站创建专用数据库和用户(避免使用root账户):
sudo mysql -u root -p
在MySQL shell中执行:
CREATE DATABASE ecommerce_db; -- 创建数据库(名称自定义)
CREATE USER 'ecommerce_user'@'localhost' IDENTIFIED BY 'StrongPassword123'; -- 创建用户(密码强度高)
GRANT ALL PRIVILEGES ON ecommerce_db.* TO 'ecommerce_user'@'localhost'; -- 授权用户访问数据库
FLUSH PRIVILEGES; -- 刷新权限
EXIT; -- 退出MySQL
若电商网站需连接数据库(如WordPress),编辑其配置文件(如WordPress的wp-config.php),填入数据库信息:
define('DB_NAME', 'ecommerce_db');
define('DB_USER', 'ecommerce_user');
define('DB_PASSWORD', 'StrongPassword123');
define('DB_HOST', 'localhost');
浏览器访问http://your_domain.com(或服务器IP),若看到电商网站首页(如WordPress的欢迎页面),则说明搭建成功。
常见问题排查:
sudo systemctl status php8.2-fpm),或Nginx配置中的fastcgi_pass路径是否正确。sudo chown -R www-data:www-data /var/www/html)。wp-config.php中的数据库信息是否正确,或MySQL服务是否启动。使用Let’s Encrypt免费获取SSL证书,加密网站流量:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com -d www.your_domain.com # 替换为你的域名
按照提示完成证书安装,Nginx会自动配置HTTPS重定向。
编辑Nginx配置文件(/etc/nginx/nginx.conf),调整以下参数:
worker_processes auto; # 根据CPU核心数自动设置(如4核则设为4)
events {
worker_connections 1024; # 每个worker的最大连接数(根据内存调整)
}
gzip on; # 启用gzip压缩
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
测试配置并重载:
sudo nginx -t
sudo systemctl reload nginx
编辑MySQL配置文件(/etc/mysql/mysql.conf.d/mysqld.cnf),调整关键参数:
[mysqld]
innodb_buffer_pool_size = 1G # 缓存数据的内存大小(服务器内存的50%-80%,如1GB内存设为1G)
max_connections = 100 # 最大并发连接数(根据网站流量调整)
query_cache_type = 0 # 新版本MySQL弃用查询缓存,设为0
重启MySQL使配置生效:
sudo systemctl restart mysql
编辑PHP-FPM配置文件(/etc/php/8.2/fpm/php.ini),启用OPcache(字节码缓存,提升PHP执行速度):
[opcache]
opcache.enable=1
opcache.memory_consumption=128 # 缓存内存大小(MB)
opcache.max_accelerated_files=4000 # 缓存文件数量
opcache.revalidate_freq=60 # 缓存验证频率(秒)
重启PHP-FPM:
sudo systemctl restart php8.2-fpm
通过以上步骤,你已在Debian上成功搭建基于LNMP的电商网站。后续可根据业务需求安装电商CMS(如Magento、OpenCart)或扩展功能(如支付接口、物流集成)。