在 CentOS 上部署和保护 Laravel,通常从「系统层 + Web服务层 + Laravel自身 + 运维习惯」几个方面入手。下面给你一套实用、可落地的防护方案。
yum update -y
useradd deploy
passwd deploy
usermod -aG wheel deploy
禁止 root 直接 SSH 登录:
vi /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
systemctl restart sshd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
location ~ /\. {
deny all;
}
location ~ /(vendor|storage|bootstrap)/ {
deny all;
}
location ~* /(storage|bootstrap)/.*\.php$ {
deny all;
}
root /var/www/laravel/public;
编辑 php.ini:
expose_php = Off
display_errors = Off
allow_url_fopen = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
重启 PHP:
systemctl restart php-fpm
chown -R deploy:nginx /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 775 storage bootstrap/cache
.env
APP_DEBUG=false
APP_ENV=production
chmod 600 .env
yum install certbot python3-certbot-nginx -y
certbot --nginx
yum install fail2ban -y
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache
如果你愿意,我可以:
你现在是 新服务器还是已有项目?