在搭建完LEMP環境之后,首先要明確兩個重要目錄
Nginx的默認root文件夾
/usr/share/nginx/html
Nginx的服務器配置文件所在目錄
/etc/nginx/sites-available/
上面兩個目錄記住就好,很常用,先擺出來
下面一步一步在阿里云ECS上部署Laravel
1.創建網站的根目錄
sudo mkdir -p /var/www
2.配置nginx服務器
sudo vim /etc/nginx/sites-available/default
打開nginx的配置文件之后,找到server
這一塊,大概是長這個樣子的
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost; location / { try_files $uri $uri/ =404;
}}
其中root,index ,server_name和location這幾行需要稍微修改一下
root修改
root /var/www/laravel/public;
這里就是將nginx服務器的根目錄指向Laravel的public文件夾下,后續的Laravel項目的代碼我們會放在我們之前創建的/var/www/laravel目錄下
index修改
index index.php index.html index.htm;
這里需要注意的是,將index.php排在最前面
server_name修改
server_name server_domain_or_IP;將server_domain_or_IP修改為你的公網IP
比如server_name dl.minshop.com;但是這時記住要修改/etc/hosts文件中的配置:加入127.0.0.1 dl.mminshop.com;才能在瀏覽器中輸入dl.minshop.com時訪問到頁面
location修改
location / { try_files $uri $uri/ /index.php?$query_string;}
修改完是這樣的:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}}
最后我們還需要配置一下Nginx,讓其執行PHP文件。同樣是在這個文件里,在location
下方添加下面的配置:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public; index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}}
注意,這一塊是自己加上去的:
location ~ .php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
配置完之后重啟Nginx,使上面的配置項生效。
sudo service nginx restart
3.創建Laravel項目
在配置完nginx后,怎么獲取Laravel的項目代碼呢?有以下幾種方法:
(1).直接composer安裝
直接通過composer來安裝,你可以在CES上通過執行
cd ~curl -sS https://getcomposer.org/installer | php
上面命令會安裝composer
composer全局使用:
sudo mv composer.phar /usr/local/bin/composer
然后在/var/www目錄下直接執行
sudo composer create-project laravel/laravel laravel
因為我們之前創建/var/www目錄,你可以直接cd /var/www然后執行上面的命令.然后坐等安裝完成。
(2).直接上傳代碼
使用下面命令上傳
scp -r laravel root@your_IP:
然后在阿里云的ECS上將laravel移動到/var/www目錄下
sudo mv laravel/ /var/www
(3).使用Git和Coding平臺
流程大概是這樣:
本地代碼---->Coding---->阿里云ECS
既然要使用git,那么先在ECS上安裝git:
sudo apt-get install git
安裝完成就可以使用git了,然后在Coding上創建一個私有項目laravel
,里面包含所有該Laravel項目所需代碼。一旦本地代碼都推送到Coding,然后在/var/www目錄下直接使用
git clone your-project-git-link your-project-git-link替換為你Coding上的laravel
項目地址
4.最后的最后
不管哪種方式安裝的代碼,/var/www/都是屬于root用戶的,而訪問網站的用戶則需要正確的權限和訪問限制,我們可以通過下面的命令來實現。
sudo chown -R :www-data /var/www/laravel
根據Laravel的官方文檔,/var/www/laravel/app/storage目錄需要給網站的用戶寫
權限
sudo chmod -R 775 /var/www/laravel/app/storage
5.BINGO
在瀏覽器輸入: