一聚教程网:一个值得你收藏的教程网站

热门教程

centos上laravel环境如何配置

时间:2026-06-28 09:11:05 编辑:袖梨 来源:一聚教程网

在CentOS上配置Laravel环境需要完成以下几个步骤:

centos上laravel环境如何配置

  1. 安装PHP及相关依赖

Laravel需要PHP 7.3或更高版本。首先,确保已经安装了PHP。如果没有,请按照以下命令安装:

sudo yum install epel-releasesudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpmsudo yum install yum-utilssudo yum-config-manager --enable remi-php74sudo yum install php php-cli php-fpm php-json php-common php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
  1. 安装Composer

Laravel使用Composer作为依赖管理工具。在CentOS上安装Composer,请执行以下命令:

curl -sS https://getcomposer.org/installer | phpsudo mv composer.phar /usr/local/bin/composer
  1. 安装Nginx或Apache

Laravel可以通过Nginx或Apache服务器运行。这里以Nginx为例:

sudo yum install nginxsudo systemctl start nginxsudo systemctl enable nginx
  1. 配置Nginx

编辑Nginx配置文件,通常位于/etc/nginx/conf.d/default.conf。将以下内容替换为现有配置:

server {listen 80;server_name yourdomain.com; # 替换为你的域名或公网IProot /path/to/your/laravel/project; # 替换为你的Laravel项目路径add_header X-Frame-Options "SAMEORIGIN";add_header X-Content-Type-Options "nosniff";index index.php;charset utf-8;location / {try_files $uri $uri/ /index.php?$query_string;}location = /favicon.ico { access_log off; log_not_found off; }location = /robots.txt{ access_log off; log_not_found off; }error_page 404 /index.php;location ~ .php$ {fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;include fastcgi_params;}location ~ /.(?!well-known).* {deny all;}}
  1. 设置文件权限

确保Laravel项目的storagebootstrap/cache目录具有可写权限:

sudo chown -R nginx:nginx /path/to/your/laravel/projectsudo chmod -R 755 /path/to/your/laravel/projectsudo chmod -R 775 /path/to/your/laravel/project/storagesudo chmod -R 775 /path/to/your/laravel/project/bootstrap/cache
  1. 配置PHP-FPM

编辑PHP-FPM配置文件,通常位于/etc/php-fpm.d/www.conf。找到以下行:

;listen = 127.0.0.1:9000

取消注释并修改为:

listen = /tmp/php-cgi.sock

然后重启PHP-FPM服务:

sudo systemctl restart php-fpm
  1. 重启Nginx

最后,重启Nginx以应用更改:

sudo systemctl restart nginx

现在,你应该可以在浏览器中访问你的Laravel项目了。如果遇到任何问题,请检查Nginx和PHP-FPM的错误日志以获取更多信息。

热门栏目