寫在前面的話:
為了將Laravel的項目搬到阿里云上也是花費了不少功夫,記錄下來,留個紀念
安裝Ubuntu系統。在購買阿里云ECS時候選擇Ubuntu14.04.4 64位版本,安裝完成后可以使用WinSCP+putty工具來登陸ubuntu系統
安裝nginx。使用putty登陸系統后,先更新源
sudo apt-get update
,接著安裝nginxsudo apt-get install nginx
配置nginx。可使用WinSCP圖形化界面,打開/etc/nginx/sites-available/default文件,修改root、index和location /,增加location ~ .php$部分,重啟nginx使配置生效
sudo service nginx restart
。配置文件可參考以下案例:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root為項目代碼所在目錄
#index在中間添加index.php
root /var/www/zshanjun/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
#填寫購買的阿里云外網IP或者自己已經解析備案的域名
server_name http://120.24.157.100/;
#更改未被注釋一行
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;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
#添加下面代碼
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4.** 安裝PHP7**
- 添加PPA。PPA,表示 Personal Package Archives,也就是個人軟件包集。
sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
- 安裝PHP7以及所需的一些擴展
sudo apt-get install php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-mysql php7.0-mbstring php7.0-xml
5.** 配置PHP7**
- 打開php.ini配置文件,找到cgi.fix_pathinfo選項,去掉注釋,然后將值設置為0 (vim使用“/”進入查找模式)
sudo vim /etc/php/7.0/fpm/php.ini
- 啟用php7.0-mcrypt
sudo phpenmod mcrypt
- 重啟php7.0-fpm
sudo service php7.0-fpm restart
6.** 安裝Mysql**
sudo apt-get install mysql-server-5.6 mysql-client-5.6
途中會提示設置MySQL的密碼,安裝好后:
mysql -u root -p
然后輸入剛剛設置的密碼,能成功進入即成功安裝。
7.** 安裝Laravel項目**
- 安裝Composer,分別執行以下命令:
sudo apt-get install curl
cd ~
curl -sS https://getcomposer.org/installer| php
sudo mv composer.phar /usr/local/bin/composer
- 安裝壓縮、解壓縮程序
sudo apt-get install zip unzip
- 安裝git
sudo apt-get install git
然后在Coding上創建一個私有項目laravel,里面包含所有該Laravel項目所需代碼。 - 使用git將代碼clone到服務器上
cd /var
mkdir www
cd www
git clone your-project-git-link
- 修改Laravel項目的訪問權限
sudo chown -R :www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage
- 導入Laravel 的vendor目錄
cd /var/www/laravel
composer install
8.訪問網站。在瀏覽器輸入外網IP或者綁定的域名就可訪問網站啦。如果有數據庫,使用mysql -u root -p
進入mysql數據庫,創建數據庫,再使用php artisan migrate
生成數據表。
其他問題
- 如果在阿里云重裝了系統,很可能在更新源的時候就出現問題,即
sudo apt-get update
時候不能更新源,解決辦法是使用如下命令新建一個更新源文件:
cd /etc/apt/
touch sources.list
sudo vim sources.list
再將下面的代碼粘貼進去即可:
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu/ trusty partner
deb http://extras.ubuntu.com/ubuntu/ trusty main
2.在安裝PHP7和擴展的時候可能因為部分組件有問題而不能安裝的,這時候可以分開安裝,逐個排除問題。
更新 2016-6-27
使用Navicat遠程登陸阿里云Mysql數據庫
- 修改
sudo vim /etc/mysql/my.cnf
文件,將#bind-address = 127.0.0.0
一行注釋掉 - 更改Mysql授權,并重啟使配置生效。
mysql -u root -p;
use mysql;
grant all privileges on *.* to 'root'@'%' identified by 'your password' with grant option;
flush privileges;
exit;
sudo service mysql restart;
- 配置Navicat。在Host Name一欄填寫阿里云外網IP,用戶名為root,密碼為你的密碼,其他可保持不變
參考文章