MacOS下搭建Vagrant 和 LAMP開發環境(2018.10.13)
MacOS中用Vagrant搭建開發環境 LAMP(Linux-Centos7 Apache 2.4.6 Mariadb 10 PHP 7.2)
下載
box
box 就是虛擬機器的映象,可以在 Vagrant Cloud 裡面下載到,你可以選擇自己想要的作業系統。 這裡是需要Centos 7。
Vagrant Cloud中找到 centos/7 這個映象,準備使用它.
$ vagrant box add centos/7 ==> box: Loading metadata for box 'centos/7' box: URL: https://vagrantcloud.com/centos/7 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) hyperv 2) libvirt 3) virtualbox 4) vmware_desktop Enter your choice: 3 ==> box: Adding box 'centos/7' (v1802.01) for provider: virtualbox box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1802.01/providers/virtualbox.box box: Download redirected to host: cloud.centos.org box: Progress: 25% (Rate: 292k/s, Estimated time remaining: 0:05:34) ==> box: Successfully added box 'centos/7' (v1802.01) for 'virtualbox'!
$ vagrant box list
centos/7 (virtualbox, 1802.01)
現在,就可以用 CentOS-7 這個版本的作業系統來執行我們要開發的 Web 應用了。
初始化
建立工作目錄 LAMP 並進入
$ vagrant init centos/7 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
上面的命令就是使用之前我們下載並新增的 centos 7 這個 box 去初始化一下專案,也就是,我們的專案會執行在 centos 7 這個版本的作業系統的虛擬機器上。
執行了上面這行命令,會在專案的目錄裡面,生成一個叫 Vagrantfile 的檔案,這個檔案告訴了 vagrant 怎麼樣去執行這個虛擬機器。
vagrantfile中,取消config.vm.network “private_network”, ip: "192.168.33.10"的註釋(個人習慣是使用192.168.33.200這個ip地址) 使用私有網路
個人習慣是把code目錄與LAMP目錄分離,建立code目錄並在VagrantFile中配置成為虛擬機器的/var/www/html的對映。類似下面的目錄結構:
/User/使用者名稱/workroot/LAMP
/User/使用者名稱/workroot/code
在vagrantFile中新增下面的配置
config.vm.synced_folder "../code", "/var/www/html",:mount_options => ["dmode=777", "fmode=777"]
環境配置
常用工具的安裝
sudo yum install kernel-devel gcc
sudo yum install gcc-c++
sudo yum install wget
virtualbox增強功能-VBoxGuestAdditions安裝(共享目錄配置)
$ wget http://download.virtualbox.org/virtualbox/5.2.8/VBoxGuestAdditions_5.2.8.iso
$ sudo mount -o loop ./VBoxGuestAdditions_5.2.8.iso /mnt
$ cd /mnt
$ sudo ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.2.8 Guest Additions for Linux........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules.
VirtualBox Guest Additions: Starting.
vagrant reload後,可以看到
==> default: Mounting shared folders...
default: /var/www/html => /Users/使用者名稱/workroot/code
已可以正常掛載
Mariadb 安裝配置
$ cd /etc/yum.repo.d/
$ sudo vi Mariadb.repo
# MariaDB 10.3 CentOS repository list - created 2018-04-02 03:37 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
$ cd ~
$ sudo yum install MariaDB-server MariaDB-client
安裝完成之後
sudo systemctl start mariadb
sudo systemctl enable mariadb //開機啟動mariadb
mysql_secure_installation //配置root密碼等
//開發環境中,允許以下三個埠被訪問
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
sudo /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
mysql -u root -p
MariaDB [(none)]> use mysql
MariaDB [(none)]> select user,password,host from user;
MariaDB [mysql]> grant all privileges on *.* to [email protected]"%" identified by "xxx";
xxx 為root使用者遠端登入密碼
Apache 安裝配置
yum list | grep httpd
sudo yum install httpd
sudo systemctl enable httpd //允許開機啟動
sudo systemctl status httpd
sudo systemctl start httpd
yum erase httpd.x86_64 解除安裝命令
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/XXX/public
ServerName local.XXX.net
ErrorLog logs/XXX.local-error.log
CustomLog logs/XXX.local-access.log common
</VirtualHost>
PHP 7 安裝
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils
yum-config-manager --enable remi-php72
yum update
yum list | grep php
yum install php.x86_64
yum install php-common php-pear php-pecl-imagick php-gd php-process php-mcrypt php-intl php-mbstring php-recode php-tidy php-xml php-soap php-xmlrpc php-mysqlnd php-pdo php-pecl-redis php-pgsql php-openssl php-tokenizer php-pecl-zip unzip
composer 安裝配置
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
如果發生需要認證的情況可修改源映象 composer config -g repo.packagist composer https://packagist.laravel-china.org/
laravel 安裝器
//如果網路有問題,可更改安裝包的全域性映象網址
composer config -g repo.packagist composer https://packagist.phpcomposer.com
$ composer global require "laravel/installer"
Changed current directory to /home/vagrant/.config/composer
Using version ^2.0 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
- Installing symfony/process (v4.0.6): Downloading (100%)
- Installing symfony/filesystem (v4.0.6): Downloading (100%)
- Installing symfony/polyfill-mbstring (v1.7.0): Downloading (100%)
- Installing symfony/console (v4.0.6): Downloading (100%)
- Installing guzzlehttp/promises (v1.3.1): Downloading (100%)
- Installing psr/http-message (1.0.1): Downloading (100%)
- Installing guzzlehttp/psr7 (1.4.2): Downloading (100%)
- Installing guzzlehttp/guzzle (6.3.2): Downloading (100%)
- Installing laravel/installer (v2.0.1): Downloading (100%)
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/lock ()
symfony/console suggests installing psr/log (For using the console logger)
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files
其中注意laravel安裝器的目錄是在 /home/vagrant/.config/composer 下面,需要確保環境變數中包括這個路徑才能在任何地方使用laravel
sudo vi /etc/profile
//在最後一行加上
PATH=$HOME/.config/composer/vendor/bin:$PATH
export PATH
如果要配置檔案馬上生效,執行下面的命令
source /etc/profile
node.js 安裝配置
獲取原始碼下載url
cd /usr/local/src/
wget http://cdn.npm.taobao.org/dist/node/v8.9.3/node-v8.9.3.tar.gz
tar zxvf node-v8.9.3.tar.gz
cd node-vv8.9.3
./configure --prefix=/usr/local/node/8.9.3
sudo make
//需要比較長的時間編譯
sudo make install
sudo vi /etc/profile
profile檔案最後加上以下程式碼 高雷環境變數
#set for nodejs
export NODE_HOME=/usr/local/node/8.9.3
export PATH=$NODE_HOME/bin:$PATH
使配置生效
$ source /etc/profile
$ node -v
v8.9.3
$ npm -v
5.5.1
Git
sudo yum install git
另一種是編譯安裝方式(適合非windows環境)
關於常備安裝庫
在CentOS安裝軟體的時候,可能缺少一部分支援庫,而報錯。這裡首先安裝系統常用的支援庫。那麼在安裝的時候就會減少很多的錯誤的出現。
yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel
時區設定
sudo timedatectl set-timezone Asia/Shanghai
注意事項
sudo vi /etc/sysconfig/selinux
編輯 SELINUX=disabled 永久關閉防寫入功能,否則apache不能正常運作
Vagrant 打包
vagrant package -–output centos7lamp_20181014.box
注意 – 是兩個減號