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安装(共享目录配置)
virtualbox相关下载地址,找到当前virtualbox的版本
VBoxGuestAdditions_5.2.8.iso
当前是5.2.8版本
$ 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
注意: 上面的镜像速度非常慢,可使用 mirrors.ustc.edu.cn/mariadb/yum 替换掉 yum.mariadb.org
安装完成之后
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 root@"%" 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 XXXXX@XXX.com
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
注意 – 是两个减号