美文网首页
Ubuntu-16.04 使用LAMP一键安装包搭建larave

Ubuntu-16.04 使用LAMP一键安装包搭建larave

作者: Red_zhang | 来源:发表于2017-09-21 10:16 被阅读0次
    1、事前准备(安装 wget、screen、unzip,创建 screen 会话)
        apt-get -y install wget screen git 
    
    2、git clone 并赋予脚本执行权限
        git clone https://github.com/teddysun/lamp.git
        cd lamp
        chmod +x *.sh
    
    3、开始安装
        screen -S lamp
        ./lamp.sh
    

    安装完成时,访问localhost出现以下页面,lamp环境就搭建好了:


    image.png
    4、如何卸载
        ./uninstall.sh
    
    5、程序目录
        MySQL 安装目录: /usr/local/mysql
        MySQL 数据库目录:/usr/local/mysql/data(默认,安装时可更改路径)
        PHP 安装目录: /usr/local/php
        Apache 安装目录: /usr/local/apache
    
    6、命令一览
        MySQL 命令
        /etc/init.d/mysqld (start|stop|restart|status)
        Apache 命令
        /etc/init.d/httpd (start|stop|restart|status)
    
    7、网站根目录
        默认的网站根目录: /data/www/default
    
    以上是lamp环境搭建的介绍,接下来就开始搭建laravel项目:
    • lamp add 创建虚拟主机
    root@zhangshu-virtual-machine:/# lamp add       /*输入创建虚拟主机的命令*/
    Please enter server names(like this:www.lamp.sh lamp.sh): zhangshu.sz     /*输入要解析的域名*/
    Please enter website root directory(default:/data/www/zhangshu.sz):       /*默认根目录,直接按回车*/
    Do you want to create database?[y/n]:n
    Don't create database.
    Congratulations. vhost [zhangshu.sz] had created.
    Website root directory is: /data/www/zhangshu.sz/
    Reloading the apache config file...
    Syntax OK
    Reload success.
    root@zhangshu-virtual-machine:/# 
    
    • 从coding.net拉代码到本地
    root@zhangshu-virtual-machine:/# cd /data/www/zhangshu.sz/           /*切换到根目录*/
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# git clone https://git.coding.net/s××××c/n××××c.git .             /*从coding.net克隆项目到本地*/
    Cloning into '.'...
    Username for 'https://git.coding.net': s××××c           /*输入coding用户名*/
    Password for 'https://sfabric@git.coding.net':          /*输入密码按回车*/
    remote: Counting objects: 20520, done.
    remote: Compressing objects: 100% (13948/13948), done.
    remote: Total 20520 (delta 10829), reused 14356 (delta 6042)
    Receiving objects: 100% (20520/20520), 32.40 MiB | 2.00 MiB/s, done.
    Resolving deltas: 100% (10829/10829), done.
    Checking connectivity... done.
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# 
    
    • 创建数据库和导入数据

    可以通过在浏览器地址栏输入localhost/phpmyadmin访问的方式添加数据库和导入数据,也可以通过命令的方式导入数据:

    root@zhangshu-virtual-machine:/# mysql -u root -p    /*root用户登录mysql数据库*/
    Enter password:                                      /*输入密码后按回车键*/
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 34
    Server version: 5.7.19 MySQL Community Server (GPL)
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql> 
    
    mysql> create database demo_test;      /*创建一个数据库demo_test*/
    Query OK, 1 row affected (0.00 sec)
    mysql> show databases;                 /*显示所有数据库*/
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | demo_sfabric       |
    | demo_test          |
    | mysql              |
    | performance_schema |
    | phpmyadmin         |
    | sys                |
    +--------------------+
    7 rows in set (0.00 sec)
    mysql> 
    
    mysql> use demo_test;               /*选择数据库*/
    Database changed
    mysql> set foreign_key_checks=0;    /*设置取消外键约束*/
    Query OK, 0 rows affected (0.00 sec)
    mysql> source ./abc.sql             /*假如abc.sql是我们当前要导入的数据库文件*/
    
    

    通过上述的一些步骤,我已经创建好了数据库。

    • 修改环境配置文件.env,重新配置缓存
    .env 文件:
    DB_HOST=localhost            /*填写数据库主机*/
    DB_DATABASE=demo_sfabric     /*填写数据库名称*/
    DB_USERNAME=root             /*数据库用户名*/
    DB_PASSWORD=root             /*数据库密码*/
    
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# cp .env.example .env
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# vi .env
    
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# php artisan config:clear   /*清除缓存配置文件*/
    Configuration cache cleared!
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# php artisan key:generate   /*重新设置key值*/
    Application key [base64:7dMhev8iv1cwgkR5iKjQ3g==] set successfully.
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# php artisan config:cache   /*重新缓存配置文件*/
    Configuration cache cleared!
    Configuration cached successfully!
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# php artisan passport:install  /*安装passport登录验证*/
    Encryption keys generated successfully.
    Personal access client created successfully.
    Client ID: 13
    Client Secret: yrhQOXYmd54OLzgPQGEFpZmxZ2WzrDHRlp7NfCe0
    Password grant client created successfully.
    Client ID: 14
    Client Secret: xrhD1OAqDsY14iO0jFg91SrKcvWYcfbDOSKQrOyk
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# 
    
    • 修改storage目录的权限,要不然会出现500错误,访问出现空白

    这一步非常重要:

    root@zhangshu-virtual-machine:/data/www/zhangshu.sz# chmod -R 777 storage/
    
    • 进入/usr/local/apache/conf/vhost目录,修改conf 文件
    root@zhangshu-virtual-machine:/usr/local/apache/conf/vhost# ls
    none.conf  zhangshu.sz.conf
    root@zhangshu-virtual-machine:/usr/local/apache/conf/vhost# vi zhangshu.sz.conf   /*编辑conf文件*/
    root@zhangshu-virtual-machine:/usr/local/apache/conf/vhost# /etc/init.d/httpd restart   /*编辑完成时,重启httpd服务*/
    root@zhangshu-virtual-machine:/usr/local/apache/conf/vhost# 
    
    /*zhangshu.sz.conf文件*/
     <VirtualHost *:80>
        ServerName zhangshu.sz
        ServerAlias zhangshu.sz
        DocumentRoot /data/www/zhangshu.sz/public     /*在根目录这里加一个public目录,其他先不变*/  
        DirectoryIndex index.php index.html index.htm
        <Directory /data/www/zhangshu.sz>
        Options +Includes -Indexes
        AllowOverride All
        Order Deny,Allow
        Require all granted
        php_admin_value open_basedir /data/www/zhangshu.sz:/tmp:/proc
        </Directory>
        ErrorLog  /data/wwwlog/zhangshu.sz/error.log
        TransferLog  /data/wwwlog/zhangshu.sz/access.log
        </VirtualHost>
    
    • 修改hosts文件
    root@zhangshu-virtual-machine:/etc# vi hosts   /*进入/etc 目录编辑hosts文件*/
    root@zhangshu-virtual-machine:/etc# init.d/networking restart   /*重启网络*/
    [ ok ] Restarting networking (via systemctl): networking.service.
    root@zhangshu-virtual-machine:/etc# 
    
    /*hosts文件:*/
    127.0.0.1       localhost
    127.0.1.1       zhangshu-virtual-machine
    127.0.0.1       zhangshu.sz                         /*在hosts文件里面新加这条记录*/
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    
    • 用刚添加的域名访问

    如果访问出现错误或者空白,建议把php.ini文件的错误提示打开,方便调试:

    首先进入到/usr/local/php/etc目录,编辑php.ini文件,把错误显示打开:
    root@zhangshu-virtual-machine:/usr/local/php/etc# ls  
    php.ini
    root@zhangshu-virtual-machine:/usr/local/php/etc# vi php.ini
    
    display_errors = On    /*把php.ini里面的错误提示打开*/
    
    • 最后,配置前端登录passport验证

    不是passport登录的可以忽略这一步
    配置到这一步时,从前端登录的时候会出现500的错误,token请求失败,现在要做的就是去storage目录,修改两个key文件的权限。
    通过命令ls -al可以查看到oauth-private.key和oauth-public.key的所有者都是root

    root@zhangshu-virtual-machine:/data/www/zhangshu.sz/storage# ls -al
    total 40
    drwxrwxrwx  7 root   root   4096 9月  21 09:38 .
    drwxr-xr-x 13 apache apache 4096 9月  21 09:23 ..
    drwxrwxrwx  2 root   root   4096 9月  20 19:10 app
    drwxrwxrwx  2 root   root   4096 9月  20 19:10 debugbar
    drwxrwxrwx  3 root   root   4096 9月  20 19:10 excel
    drwxrwxrwx  5 root   root   4096 9月  20 19:10 framework
    -rwxrwxrwx  1 root   root     11 9月  20 19:10 .gitignore
    drwxrwxrwx  2 root   root   4096 9月  21 10:08 logs
    -rwxrwxrwx  1 root   root   3292 9月  21 09:38 oauth-private.key
    -rwxrwxrwx  1 root   root    812 9月  21 09:38 oauth-public.key
    root@zhangshu-virtual-machine:/data/www/zhangshu.sz/storage# 
    
    

    我们需要把这两个文件的所有者改为apache:

    root@zhangshu-virtual-machine:/data/www/zhangshu.sz/storage# chown apache:apache oauth-*.key
    

    修改完成时,我们再次登录,就一切正常了。

    参考资料:https://lamp.sh/install.html

    相关文章

      网友评论

          本文标题:Ubuntu-16.04 使用LAMP一键安装包搭建larave

          本文链接:https://www.haomeiwen.com/subject/ndyosxtx.html