《PHP編程:詳解如何在云服務器上部署Laravel》要點:
本文介紹了PHP編程:詳解如何在云服務器上部署Laravel,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰學習PHP和Laravel已經有一段時間了,但是所有的代碼都是跑在本地的虛擬主機上的,于是去騰訊云申請了一個月的免費云主機,想把項目部署到云服務器上.
PHP實戰不得不說這里面的坑實在是有點多,讓我這個初次接觸服務器的小白摸不清頭腦.在配置好服務器之后,部署一個Laravel項目更是費勁心思,于是乎想記錄下部署Laravel項目的過程.
PHP實戰PS: Linux真是越用越有感覺的系統,回家在臺式機上也要裝個Linux敲代碼用.
PHP實戰環境簡介
PHP實戰在操作系統的選擇上,我選用了Linux ubuntu16.04的系統,使用的是LNMP的環境,即 Linux + Nginx + Mysql + PHP的環境.
PHP實戰刪除Apache
PHP實戰
sudo service apache2 stop
update-rc.d -f apache2 remove
sudo apt-get remove apache2
PHP實戰先用這三條命令來刪除Apaceh 之后更新一下包列表
PHP實戰
sudo apt-get update
PHP實戰1.安裝Nginx
PHP實戰
sudo apt-get install nginx
PHP實戰在安裝完Nginx之后,要重啟nginx
PHP實戰
sudo service nginx start
PHP實戰執行完之后,在瀏覽器輸入云服務器分配給你的公網ip,就可以看到welcome to nginx的界面了
PHP實戰2. 安裝Mysql
PHP實戰
sudo apt-get install mysql-server mysql-client
PHP實戰過程中會提示你設置Mysql的密碼,就跟平時的密碼設置一樣,一次輸入,一次確認.密碼確認完畢后基本等一會就安裝好了.嘗試
PHP實戰
mysql -u root -p
PHP實戰如果登錄成功,那Mysql就正確安裝了.
PHP實戰3.安裝PHP
PHP實戰
sudo apt-get install php5-fpm php5-cli php5-mcrypt
PHP實戰只有通過php5-fpm,PHP在Nginx下才能正常運行,遂,安裝之.
PHP實戰至于php5-mcrypt,有些PHP框架會依賴于這個,比如Laravel就是,所以也把它裝上了.
PHP實戰題外話,這里的php5我自己在部署時安裝了php7 如果想嘗試的也可以試試.
PHP實戰4.配置PHP
PHP實戰
sudo vim /etc/php5/fpm/php.ini
PHP實戰打開PHP配置文件,找到cgi.fix_pathinfo選項,去掉它前面的注釋分號;,然后將它的值設置為0,如下
PHP實戰
cgi.fix_pathinfo=0
PHP實戰5. 啟用php5-mcrypt:
PHP實戰
sudo php5enmod mcrypt
PHP實戰6.重啟php5-fpm:
PHP實戰
sudo service php5-fpm restart
PHP實戰在搭建完LEMP環境之后,首先要明確兩個重要目錄
PHP實戰Nginx的默認root文件夾
PHP實戰/usr/share/nginx/html
PHP實戰Nginx的服務器配置文件所在目錄
PHP實戰/etc/nginx/sites-available/
PHP實戰上面兩個目錄記住就好,很常用,先擺出來
PHP實戰下面一步一步在云服務器上部署Laravel
PHP實戰1.創建網站的根目錄
PHP實戰
sudo mkdir -p /var/www
PHP實戰2.配置nginx服務器
PHP實戰
sudo vim /etc/nginx/sites-available/default
PHP實戰打開nginx的配置文件之后,找到server這一塊,大概是長這個樣子的
PHP實戰
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;
}
}
PHP實戰其中root,index ,server_name和location這幾行需要稍微修改一下
PHP實戰root修改
PHP實戰
root /var/www/laravel/public;
PHP實戰這里就是將nginx服務器的根目錄指向Laravel的public文件夾下,后續的Laravel項目的代碼我們會放在我們之前創建的/var/www/laravel目錄下
PHP實戰index修改
PHP實戰
index index.php index.html index.htm;
PHP實戰這里需要注意的是,將index.php排在最前面
PHP實戰server_name修改
PHP實戰
server_name server_domain_or_IP;
PHP實戰將server_domain_or_IP修改為你的公網IP
PHP實戰location修改
PHP實戰
location / {
try_files $uri $uri/ /index.php?$query_string;
}
PHP實戰修改完是這樣的:
PHP實戰
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;
}
}
PHP實戰最后我們還需要配置一下Nginx,讓其執行PHP文件.同樣是在這個文件里,在location下方添加下面的配置:
PHP實戰
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;
}
}
PHP實戰注意,最下面的location ~ \.php$
是自己加上去的:
PHP實戰配置完之后重啟Nginx,使上面的配置項生效.
PHP實戰
sudo service nginx restart
PHP實戰3.創建Laravel項目
PHP實戰在配置完nginx后,怎么獲取Laravel的項目代碼呢?有以下幾種方法:
PHP實戰(1).直接composer安裝
PHP實戰直接通過composer來安裝,你可以在服務器上通過執行
PHP實戰
cd ~
curl -sS https://getcomposer.org/installer | php
PHP實戰上面命令會安裝composer
PHP實戰composer全局使用:
PHP實戰
sudo mv composer.phar /usr/local/bin/composer
PHP實戰然后在/var/www目錄下直接執行
PHP實戰
sudo composer create-project laravel/laravel laravel
PHP實戰因為我們之前創建/var/www目錄,你可以直接cd /var/www然后執行上面的命令.然后坐等安裝完成.
PHP實戰(2).直接上傳代碼
PHP實戰使用下面命令上傳
PHP實戰
scp -r laravel root@your_IP:
PHP實戰然后在服務器上將laravel移動到/var/www目錄下
PHP實戰
sudo mv laravel/ /var/www
PHP實戰(3).使用Git和Coding平臺
PHP實戰個人比較喜歡使用git來上傳代碼,可以很方便的更新代碼和進行回滾,一旦版本更新出Bug我可以借助Git的強大版本管理能力來修復Bug.流程大概是這樣:
PHP實戰本地代碼---->Github---->云服務器
PHP實戰既然要使用git,那么先在云服務器上安裝git:
PHP實戰
sudo apt-get install git
PHP實戰安裝完成就可以使用git了,然后在Github上創建一個私有項目laravel,里面包含所有該Laravel項目所需代碼.
PHP實戰一旦本地代碼都推送到Coding,然后在/var/www目錄下直接使用
PHP實戰
git clone your-project-git-link
PHP實戰your-project-git-link替換為你Github上的laravel項目地址
PHP實戰5.BINGO
PHP實戰在瀏覽器輸入:http://server_domain_or_IP
PHP實戰至此,你可以在服務器上隨意地用Laravel了,keep coding!
PHP實戰以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/553.html