使用vagrant搭建LNMP 环境
准备工作:
- 安装 OracleVMVirtualBox https://www.virtualbox.org/
- 安装 vagrant http://www.vagrantup.com/
- 下载使用的box文件http://www.vagrantbox.es/
把好的box文件(以Ubuntu.16.04box镜像为例)放入一个文件夹
进入cmd终端然后切入刚刚放入Ubuntu.16.04box镜像的文件夹
输入vagrant box add server
用鼠标拉住Ubuntu.16.04box镜像拉入终端(这是告诉vagrant你的box文件路径)回车
使用vagrant box list
查看是否有server (virtualbox, 0)
看到有之后,我们执行vagrant init server
初始化 然后我们的工作目录就生成了一个vagrantfile的配置文件
因为我们虚拟机默认只分配一个访问外网的 IP, 项目使用的IP得手动设置打开 Vagrantfile 找到29行设置一个私有 IP(我自己的在29行),去掉注释
然后我们就可以使用vagrant up 启动 成功后会在当前目录生成一个.vagrant的隐藏文件
打开Oracle VM VirtualBox就发现server的虚拟机已经运行了
使用vagrant ssh直接连接到虚拟机里面 ls查看vagrant是同步本地的文件夹
vagrant box add NAME URL
#添加一个box
vagrant box list
#查看本地已添加的box
vagrant box remove NAME virtualbox
#删除本地已添加的box,如若是版本1.0.x,执行vagrant box remove NAME
(NAME是添加的box名字)
vagrant init NAME
#初始化,实质应是创建Vagrantfile文件(NAME是添加的box名字)
vagrant up
#启动虚拟机
vagrant halt
#关闭虚拟机
vagrant destroy
#销毁虚拟机
vagrant reload
#重启虚拟机
vagrant package
#当前正在运行的VirtualBox虚拟环境打包成一个可重复使用的box
vagrant ssh
#进入虚拟环境
安装PHP7.0 之前,要先进行:
sudo vim /etc/apt/source.list
将里面的内容换成下面内容(换清华源,国内速度较快)
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
执行sudo apt-get update
命令更新
sudo apt-get install -y language-pack-en-base
解决语言冲突
安装完成之后,执行:locale-gen en_US.UTF-8
安装 git ,vim: sudo apt-get install git vim
执行: sudo apt-get install software-properties-common
安装依赖包
执行:sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
再次:sudo apt-get update
然后就可以安装php7.0了:运行 apt-cache search php7.0
安装php7.0:sudo apt-get install php7.0 -y
执行:php -V
查看是否安装成功
继续安装php与mysql的通信模块:sudo apt-get install php7.0-mysql -y
然后安装fpm,这是nginx用来解析php文件的:sudo apt-get install php7.0-fpm -y
其他必备模块:sudo apt-get install php7.0-curl php7.0-xml php7.0-mcrypt php7.0-json php7.0-gd php7.0-mbstring -y
安装MySQL 5.7
运行:wget http://dev.mysql.com/get/mysql-apt-config_0.5.3-1_all.deb
然后使用dpkg命令添加MySQL的源:sudo dpkg -i mysql-apt-config_0.5.3-1_all.deb
选择 Server MySQL 5.7 Apply
执行:sudo apt-get update
更新
安装: sudo apt-get install mysql-server -y
卸载MySQL
sudo apt-get autoremove --purge mysql-server-5.0
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common
这个很重要上面的其实有一些是多余的
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
清理残留数据
安装NGINX
安装:sudo apt-get install nginx -y
安装:sudo apt-get install curl -y
执行:curl localhost
查看是否成功
安装组件:
sudo apt-get install unzip
sudo apt-get install zip
sudo apt-get install composer -y
进入:sudo vim /etc/php/7.0/fpm/php.ini
将cgi.fix_pathinfo=1
改为cgi.fix_pathinfo=0
进入:sudo vim /etc/php/7.0/fpm/pool.d/www.conf
将listen = /run/php/php7.0-fpm.sock
修改为listen = 127.0.0.1:9000
然后重启
关闭:service php7.0-fpm stop
开启:service php7.0-fpm start
进入:sudo vim /etc/nginx/sites-available/default
将server这部分替换:
server {
#listen 80 default_server;
listen 80;
#listen [::]:80 default_server ipv6only=on;
root /var/www/your-project-name/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name lufficc.com www.lufficc.com;
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$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
root:是你的项目的public目录,也就是网站的入口
index:添加了index.php,告诉Nginx先解析index.php文件
server_name:你的域名,没有的话填写localhost
location / try_files修改为了try_files $uri $uri/ /index.php?$query_string;
location ~ .php$部分告诉Nginx怎么解析Php,原封不动复制即可,但注意:fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;的目录要和fpm的配置文件中的listen一致。
sudo service nginx restart
(重启nginx)
sudo service php7.0-fpm restart
(重启fpm)
内容来源:吕倡个人博客