写在前面的话:
为了将Laravel的项目搬到阿里云上也是花费了不少功夫,记录下来,留个纪念


  1. 安装Ubuntu系统。在购买阿里云ECS时候选择Ubuntu14.04.4 64位版本,安装完成后可以使用WinSCP+putty工具来登陆ubuntu系统

  2. 安装nginx。使用putty登陆系统后,先更新源sudo apt-get update,接着安装nginxsudo apt-get install nginx

  3. 配置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

直接使用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生成数据表。


其他问题

  1. 如果在阿里云重装了系统,很可能在更新源的时候就出现问题,即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数据库

  1. 修改 sudo vim /etc/mysql/my.cnf文件,将#bind-address = 127.0.0.0一行注释掉

  2. 更改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;
  1. 配置Navicat。在Host Name一栏填写阿里云外网IP,用户名为root,密码为你的密码,其他可保持不变


参考文章


Laravel

登陆发表评论