资讯专栏INFORMATION COLUMN

Laravel5.4 博客部署到阿里云服务器

JowayYoung / 1004人阅读

摘要:前边已经学会在本地用进行开发了,现在就让我们将本地开发的项目部署到阿里云服务器,来次实战操作,阿里云部署环境阿里,,使用服务器,登录。

前边已经学会在本地用Homestead进行开发了,现在就让我们将本地开发的项目部署到阿里云服务器,来次实战操作,阿里云部署环境:阿里ECS,Ubuntu,使用Nginx服务器,SSH登录。
一、服务器配置

如果你的服务器是刚申请的,则必须做一些基础的配置,如安装Nginx,MySQL,然后创建项目目录,然后对Nginx进行配置,我的项目放在 /var/www/ 目录下。

Nginx的默认root文件夹

/usr/share/nginx/html

Nginx的服务器配置文件所在目录

/etc/nginx/sites-available/

上面两个目录记住就好,很常用,先摆出来

配置nginx服务器

> sudo vim /etc/nginx/sites-available/default

重启 nginx:

> sudo service nginx restart;

5.配置新的php.ini

sudo vim /etc/php/7.0/fpm/php.ini

#将cgi.fix_pathinfo=1这一行去掉注释,将1改为0。

6.配置php-fpm

sudo vim /etc/php/7.0/fpm/pool.d/www.conf
#  配置这个 listen = /var/run/php/php7.0-fpm.sock

注意:这个文件php7.0-fpm.sock 的目录每个服务器的安装位置可能不同,我的是在 /var/run/php/php7.0-fpm.sock, 其他的可能是/var/run/php7.0-fpm.sock,具体可以自己查看,之前因为这个坑折腾了很久。
7.nginx 配置

sudo vim /etc/nginx/sites-enabled/default

配置相对应的路径和 location,(以laravel项目为例):

 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;

root your_website_root; # /var/www/weixin
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name your_domain;

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/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

注意这里的 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 要和第6步的 php-fpm 配置:/var/run/php/php7.0-fpm.sock; 一致。

修改laravel项目的访问权限

sudo chown -R :www-data /var/www/laravel

sudo chmod -R 775 /var/www/laravel/storage
二、克隆项目

将我们之前推到 github 仓库的项目,使用 git clone 到我们的服务器,后边就可以用 git pull 拉取github的代码了。

root@im:/var/www# git clone https://github.com/corwien/digtime.git

然后进行项目:

> cd /var/www/digtime

给网站的用户写权限:

root@iZ9:/var/www/digtime# sudo chmod -R 775 /var/www/digtime/storage
三、生成配置文件

项目.env环境配置:

cp .env.example .env
四、安装扩展包依赖
composer install
  Problem 1
    - This package requires php >=5.6.4 but your PHP version (5.5.9) does not satisfy that requirement.

如果在使用命令时出现阿里云服务器PHP版本过低的情况,请参照该博文进行升级:Ubuntu 14 PHP 5.6 升级到PHP 7.0

重启 nginx:

> sudo service nginx restart;

升级后的PHP版本:

root@iZ9:/home# php -v
PHP 7.0.18-1+deb.sury.org~trusty+1 (cli) (built: Apr 11 2017 15:08:38) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.18-1+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies

更新composer版本

// 安装解压软件
> sudo apt-get install zip unzip
// 清除缓存,防止zlib_decode(): data error错误发生
>composer clear-cache

// 更新到最新版本
>sudo composer self-update

使用 Composer 遇到的坑

五、安装前端资源

如果你的服务器端没有安装Node.js,则先要使用下边的命令进行安装:

Ubuntu apt-get命令安装
命令格式如下:

sudo apt-get install nodejs
sudo apt-get install npm

或者使用命令源码编译安装:

以下部分我们将介绍在Ubuntu Linux下安装 Node.js 。 其他的Linux系统,如Centos等类似如下安装步骤。

在 Github 上获取 Node.js 源码:

$ sudo git clone https://github.com/nodejs/node.git
Cloning into "node"...
修改目录权限:

$ sudo chmod -R 755 node
使用 ./configure 创建编译文件,并按照:

$ cd node
$ sudo ./configure
$ sudo make
$ sudo make install

查看 node 版本:

$ node --version
v0.10.25

其他平台安装方法:
Node.js 安装配置
nodejs,npm安装(ubuntu14.04下)

然后执行npm命令安装:

npm install

npm 升级:

// 最新的版本latest 3.10.10
 npm install npm@latest -g

Node相关文章:
Node.js 概述之版本管理工具nvm
如果想在同一台机器,同时安装多个版本的node.js,就需要用到版本管理工具nvm

$ git clone https://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh

安装以后,nvm的执行脚本,每次使用前都要激活,建议将其加入~/.bashrc
文件(假定使用Bash)。激活后,就可以安装指定版本的Node。

nvm 执行脚本加入Bash:
在Linux里设置环境变量的方法(export PATH)

root@iZ:~# vim /root/.bashrc

.bashrc 文件末尾添加这样一行:

# ADD NVM node-NPM 20170502
export PATH="$PATH:/root/.nvm/versions/node/v7.9.0/bin"

查看变量是否添加成功:

root@iZ94:~# echo $PATH
/root/.nvm/versions/node/v7.9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
root@iZ94:~# exec bash
root@iZ94:~# node -v
v7.9.0

NVM 相关操作:

# 安装最新版本
$ nvm install node

# 安装指定版本
$ nvm install 0.12.1

# 使用已安装的最新版本
$ nvm use node

# 使用指定版本的node
$ nvm use 0.12

nvm也允许进入指定版本的REPL环境。

$ nvm run 0.12

如果在项目根目录下新建一个.nvmrc 文件,将版本号写入其中,就只输入nvm use 命令即可,不再需要附加版本号。

下面是其他经常用到的命令。

# 查看本地安装的所有版本
$ nvm ls

# 查看服务器上所有可供安装的版本。
$ nvm ls-remote

# 退出已经激活的nvm,使用deactivate命令。
$ nvm deactivate
六、生成表
php artisan migrate

出现这样的问题:

root@iZ94:/var/www/digtime# php artisan migrate
Migration table created successfully.


  [IlluminateDatabaseQueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max
   key length is 767 bytes (SQL: alter table `users` add unique `users_name_unique`(`name
  `))



  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max
   key length is 767 bytes


root@iZ94:/var/www/digtime#

解决方法:Laravel 5.4 migrate时报错: Specified key was too long error

手动配置迁移命令migrate生成的默认字符串长度,在AppServiceProvider 中调用 Schema::defaultStringLength 方法来实现配置:

use IlluminateSupportFacadesSchema;

    /**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
   Schema::defaultStringLength(191);
}

终于成功了:

root@iZ94:/var/www/digtime# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2017_04_15_235414_create_articles_table
Migrated:  2017_04_15_235414_create_articles_table
root@iZ94:/var/www/digtime#
七、Nginx

Nginx 重启失败,查看原因:

service nginx restart
 * Restarting nginx nginx                                                      [fail]
root@iZ94j7ehy5oZ:/var/www/digtime# sudo nginx -t
nginx: [emerg] unknown directive "i" in /etc/nginx/nginx.conf:71
nginx: configuration file /etc/nginx/nginx.conf test failed
root@iZ94j7ehy5oZ:/var/www/digtime# vim /etc/nginx/nginx.conf

这里的错误提示,原来配置文件在修改时疏忽多加了一个 "i" 字符,删除即可。

又出现了一个坑:

GET http://xxx.cn/ 500 (Internal Server Error)

解决方法:
nginx提示:500 Internal Server Error错误的解决方法

八、pull 拉取github的代码

服务器拉取部署到github的代码

root@iZ94:/var/www/digtime# git pull
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From https://github.com/corwien/digtime
   74785bd..b01ad17  master     -> origin/master
Updating 74785bd..b01ad17
Fast-forward
 app/Models/User.php                                          | 2 ++
 database/migrations/2014_10_12_000000_create_users_table.php | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
root@iZ94:/var/www/digtime#

具体步骤是这样的:
本地开发代码,然后推送到github仓库,服务器端克隆github的项目,再然后服务器从github拉取代码,在线上即看到改变的代码。

九、安装后台

我们本项目使用 laravel-admin 开源的后台:

1.安装laravel-admin
php artisan admin:install
2.换掉谷歌的地图,加载时间过长

由于该前端资源有引入google地图,所以,前端加载会非常慢,这里我们对源码进行一下修改:

/vendor/encore/laravel-admin/src/Form/Field/Map.php

/**
     * Get assets required by this field.
     *
     * @return array
     */
    public static function getAssets()
    {
        // 本项目配置环境使用的语言包是zh-CN,"resources/lang/zh-CN/", 所以这里对zh_CN做出修改【20170501】
        if (config("app.locale") == "zh-CN") {
            $js = "http://map.qq.com/api/js?v=2.exp";
        } else {
            $js = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=".env("GOOGLE_API_KEY");
        }

        return compact("js");
    }
3.重写方法

/vendor/encore/laravel-admin/src/Form.php
重写 store,update 方法

.
.
.
/**
     * Store a new record.
     * $data array  获取到的输入的处理过的数据
     * @return IlluminateHttpRedirectResponse|IlluminateRoutingRedirector|IlluminateHttpJsonResponse
     */
    public function store_v2(array $data)
    {
        $data = $data ? $data : Input::all();

        // Handle validation errors.
        if ($validationMessages = $this->validationMessages($data)) {
            return back()->withInput()->withErrors($validationMessages);
        }

        if (($response = $this->prepare($data)) instanceof Response) {
            return $response;
        }

        DB::transaction(function () {
            $inserts = $this->prepareInsert($this->updates);

            foreach ($inserts as $column => $value) {
                $this->model->setAttribute($column, $value);
            }

            $this->model->save();

            $this->updateRelation($this->relations);
        });

        if (($response = $this->complete($this->saved)) instanceof Response) {
            return $response;
        }

        if ($response = $this->ajaxResponse(trans("admin::lang.save_succeeded"))) {
            return $response;
        }

        return $this->redirectAfterStore();
    }
    
   /**
     * 重写方法【20170501】
     * Handle update.
     *
     * @param int $id
     *
     * @return SymfonyComponentHttpFoundationResponse
     */
    public function update_v2($id, array $data)
    {
        $data = $data ? $data : Input::all();

        $data = $this->handleEditable($data);

        $data = $this->handleFileDelete($data);

        if ($this->handleOrderable($id, $data)) {
            return response([
                "status"  => true,
                "message" => trans("admin::lang.update_succeeded"),
            ]);
        }

        /* @var Model $this->model */
        $this->model = $this->model->with($this->getRelations())->findOrFail($id);

        $this->setFieldOriginalValue();

        // Handle validation errors.
        if ($validationMessages = $this->validationMessages($data)) {
            return back()->withInput()->withErrors($validationMessages);
        }

        if (($response = $this->prepare($data)) instanceof Response) {
            return $response;
        }

        DB::transaction(function () {
            $updates = $this->prepareUpdate($this->updates);

            foreach ($updates as $column => $value) {
                /* @var Model $this->model */
                $this->model->setAttribute($column, $value);
            }

            $this->model->save();

            $this->updateRelation($this->relations);
        });

        if (($result = $this->complete($this->saved)) instanceof Response) {
            return $result;
        }

        if ($response = $this->ajaxResponse(trans("admin::lang.update_succeeded"))) {
            return $response;
        }

        return $this->redirectAfterUpdate();
    } 
    
    

相关文章:
阿里云 ECS 部署:nginx+MySQL+Laravel+PHP7+Redis+Node.js
在阿里云的ECS上部署Laravel项目

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22887.html

相关文章

  • laravel package收集

    摘要:查找保存下载用搭建自己的缓存仓库权限管理的好选择基于封装的后台管理系统,支持手机和端访问支付宝风格的验证器后台系统微信接口的部署脚本开发的博客系统百度推送自动记录用户行为扩展一个项目管理系统根据生成对应导航的状态 1.debug https://github.com/barryvdh/l... showImg(https://segmentfault.com/img/bVmhWL); ...

    psychola 评论0 收藏0
  • 零基础入门—网站建站教程(新手必备)

    摘要:自行建站服务器购买网站搭建网站维护全程自主,弹性灵活。网站部署常见网站类型有以下几种个人博客常用于搭建个人博客网站,尤其适用于首次使用阿里云进行建站的新用户。若您需要在阿里云服务器上部署站点环境安装应用程序,可点此查看网站基础环境搭建服务。相信很多新用户会有这样的疑惑,我要做个网站,到底要使用什么产品,如何能快速完成网站建站呢?搭建网站有两种选择,一种是直接购买建站模板,另一种则是自行建站。...

    booster 评论0 收藏0
  • 零基础入门—网站建站教程(新手必备)

    摘要:自行建站服务器购买网站搭建网站维护全程自主,弹性灵活。网站部署常见网站类型有以下几种个人博客常用于搭建个人博客网站,尤其适用于首次使用阿里云进行建站的新用户。若您需要在阿里云服务器上部署站点环境安装应用程序,可点此查看网站基础环境搭建服务。前言相信很多新用户会有这样的疑惑,我要做个网站,到底要使用什么产品,如何能快速完成网站建站呢?搭建网站有两种选择,一种是直接购买建站模板,另一种则是自行建...

    wemall 评论0 收藏0
  • vue+express+mysql项目总结(node项目部署阿里通用)

    摘要:原文发布于我的个人博客上原文点这里前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了。我的远程连接工具是用的是,文件上传用的是。 原文发布于我的个人博客上:原文点这里   前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了。下面我介绍下我的部署过程: 一、购买服务器和域名   如果需要域名(不用域名通过ip也可以访问,虽然不方便,但可以节约一年几十块钱的...

    dreamGong 评论0 收藏0
  • vue+express+mysql项目总结(node项目部署阿里通用)

    摘要:原文发布于我的个人博客上原文点这里前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了。我的远程连接工具是用的是,文件上传用的是。 原文发布于我的个人博客上:原文点这里   前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了。下面我介绍下我的部署过程: 一、购买服务器和域名   如果需要域名(不用域名通过ip也可以访问,虽然不方便,但可以节约一年几十块钱的...

    newtrek 评论0 收藏0

发表评论

0条评论

JowayYoung

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<