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
修改完成時,我們再次登錄,就一切正常了。