在CentOS环境下配置Laravel,你需要遵循以下步骤:
首先,确保你已经安装了PHP和Composer。然后,使用Composer全局安装Laravel安装器:
composer global require laravel/installer
将Composer的全局二进制文件目录添加到系统的PATH变量中。编辑~/.bashrc
或~/.bash_profile
文件,添加以下行:
export PATH="$HOME/.composer/vendor/bin:$PATH"
保存文件并运行source ~/.bashrc
或source ~/.bash_profile
使更改生效。
使用Laravel安装器创建一个新的Laravel项目:
laravel new project_name
将project_name
替换为你的项目名称。这将在当前目录下创建一个新的Laravel项目。
在CentOS上,我们通常使用Apache或Nginx作为Web服务器。下面是如何配置这两个服务器以运行Laravel项目。
Apache配置:
启用mod_rewrite模块:
sudo yum install mod_rewrite
sudo systemctl enable httpd
sudo systemctl start httpd
在/etc/httpd/conf/httpd.conf
文件中,找到<Directory>
部分并启用AllowOverride All
:
<Directory /var/www/html>
AllowOverride All
</Directory>
将Laravel项目移动到Apache的默认文档根目录(例如/var/www/html
),或者更新DocumentRoot指向你的项目目录。
重启Apache服务:
sudo systemctl restart httpd
Nginx配置:
安装Nginx:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
在/etc/nginx/conf.d/default.conf
文件中,添加以下配置:
server {
listen 80;
server_name example.com; # 替换为你的域名或公网IP
root /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;
}
}
将example.com
替换为你的域名或公网IP,将/path/to/your/laravel/project
替换为你的Laravel项目路径。
重启Nginx服务:
sudo systemctl restart nginx
确保Laravel项目的storage
和bootstrap/cache
目录具有可写权限:
sudo chown -R nginx:nginx /path/to/your/laravel/project/storage
sudo chown -R nginx:nginx /path/to/your/laravel/project/bootstrap/cache
将nginx:nginx
替换为你的Web服务器用户和组,将/path/to/your/laravel/project
替换为你的Laravel项目路径。
现在,你应该可以在浏览器中访问你的Laravel项目了。如果遇到任何问题,请检查Web服务器和Laravel的日志以获取更多信息。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>