Laravel是基於PHP的一個開源MVC架構,優點不少,缺點也有。這裡就不細說,專註於Laravel的安裝。由於國內被牆的關係,不能直接使用composer直接安裝,本文主要展開講解的是使用Laravel提供的一鍵安裝包。 下載Laravel一鍵安裝包
下載地址,注意不同版本對PHP版本有要求,本文下載的版本是laravel-v5.2.15
安裝PHP和對應的擴充
本文使用的Laravel版本是laravel-v5.2.15,對PHP版本要求如下:
PHP >= 5.6.4 OpenSSL PHP Extension PDO PHP Extension Mbstring PHP Extension Tokenizer PHP Extension XML PHP Extension
上邊的PHP擴充有興趣的可以研究一下是幹什麼的,這裡不細說,有什麼疑惑可以共同探討。
本文使用的是ubuntu16.04,使用下面的命令安裝PHP:
sudo apt-get install php
當前預設的版本是php7.0。然後使用下面的命令安裝幾個擴充:
sudo apt-get install php-fpm php-cli php-mcrypt
安裝nginx
本文使用的是 nginx/1.10.0 (Ubuntu),使用下面的命令安裝即可:
sudo apt-get install nginx
修改PHP配置
主要修改的是php.ini檔案中的cgi.fix_pathinfo=1,預設是1,需要修改為0。執行下面的命令即可:
sudo gedit /etc/php/7.0/fpm/php.ini
找到 cgi.fix_pathinfo=1 修改為 cgi.fix_pathinfo=0
然後再執行下面的命令啟動PHP:
phpenmod mcryptservice php7.0-fpm restart
修改Nginx設定檔
Nginx安裝好後預設的web發布路徑是/var/www/html,使用下面的命令建立一個Laravel的目錄。
cd /var/www/htmluzip laravel-v5.2.15.zipmv laravel-v5.2.15 laravel
執行完上面的命令後就完成了發布laravel到Nginx伺服器,但是現在還不能正常開啟網頁,需要再Nginx中對Laravel進行配置。先看看Nginx預設的設定檔如下:
sudo gedit /etc/nginx/sites-available/default
### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.### Default server configuration#server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; **root /var/www/html/;** # Add index.php to the list if you are using PHP **index index.html index.htm index.nginx-debian.html;** **server_name _;** location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. **try_files $uri $uri/=404;** } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # **#location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #}** # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}}# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {# listen 80;# listen [::]:80;## server_name example.com;## root /var/www/example.com;# index index.html;## location / {# try_files $uri $uri/ =404;# }#}
修改代碼中粗體的部分,修改成如下代碼:
### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.### Default server configuration#server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html/laravel/public; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.php?$query_string; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}}# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {# listen 80;# listen [::]:80;## server_name example.com;## root /var/www/example.com;# index index.html;## location / {# try_files $uri $uri/ =404;# }#}
最後再重新啟動Nginx服務:
sudo service nginx restart
修改Laravel目錄許可權
sudo chown -R :www-data /var/www/html/laravelsudo chmod -R 775 /var/www/html/laravel/storage
產生秘鑰
cd /var/www/html/laravel sudo php artisan key:generate
收穫的季節
在瀏覽器中輸入 http://localhost 即可看到一個頁面中顯示Laravel5,就表示大功告成。恭喜恭喜。。。。