VirutalBox+Vagrant安装Ubuntu系统-快速搭建lnmp环境

win10+VirutlBox6.0+Vagrant 2.2.4+Ubuntu 16.04.6+Nginx1.10.3+PHP7.0.33+Mysql5.7.26

本次的安装教程来源:《PHP高性能开发基础、框架与项目实战》

亲测确实挺方便的也可以根据不同需要打造个性化开发环境,另外vagrant确实挺好用,box环境一打包就可以了。

【一】VirtualBox+vagrant搭建Ubuntu系统

【1】远程添加box
//我这里使用的是远程官网的box直接用原名即可下载
D:\>vagrant box add ununtu/xenial64
可能需要一段时间 下载完毕会看到下面的字样
==> box: Successfully added box 'ubuntu/xenial64' (v20190516.0.0) for 'virtualbox'!
【2】查看已经添加到box的列表
D:\>vagrant box list
ubuntu/xenial64 (virtualbox, 20190516.0.0)
【3】创建虚拟机的系统配置文件
创建一个目录,然后执行初始化命令,会生成Vagrantfile配置文件
D:\>cd Vagrant-ubuntu
D:\Vagrant-ubuntu>vagrant init ubuntu/xenial64
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.
【4】初始化虚拟机
D:\Vagrant-ubuntu>vagrant up
....
==> default: Mounting shared folders...
    default: /vagrant => D:/Vagrant-ubuntu
【5】连接虚拟机
D:\Vagrant-ubuntu>vagrant ssh
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-148-generic x86_64)

 * Documentation:  https://help.ubuntu.com

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@ubuntu-xenial:~$
【6】查看虚拟机基本信息
可以执行df -h命令查看磁盘挂载的信息,其中vagrant目录映射真是的系统中Vagrantfile配置文件所在的目录,可以实现虚拟系统与真实系统的文件共享。
vagrant@ubuntu-xenial:/$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M  3.1M   97M   4% /run
/dev/sda1       9.7G 1015M  8.7G  11% /
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           496M     0  496M   0% /sys/fs/cgroup
vagrant         179G  2.9G  177G   2% /vagrant
tmpfs           100M     0  100M   0% /run/user/1000
【7】设置root密码
由于新安装的系统都是没有root密码,这里我们设置一个。
vagrant@ubuntu-xenial:/$ sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
【8】安装插件
由于VirtualBox设置共享目录时候需要在虚拟机中安装VirtualBox Guest Additions 模块,虽然Vagrant会自动安装,但是因为VirtualBox Guest Additions是VirutalBox的内核模块,当VirtualBox内核升级后,VirtualBox guest additions也会失效,导致挂载目录失败,所以为了解决这个问题,可以安装vagrant-vbguest来解决。这个插件会在虚拟机内核升级后重新安装VirtualBox Guest Additions.
D:\Vagrant-ubuntu>vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.18.0)'!
【9】修改配置文件
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"  #box名称
  config.vm.hostname = "ubuntu"  #系统下的主机名
  config.vm.network :"forwarded_port", guest: 80, host: 8080 #端口映射
  config.vm.network :"private_network", ip: "192.168.10.1"#虚拟机设置IP 
end
【10】重新加载
D:\Vagrant-ubuntu>vagrant reload

【二】搭建lnmp环境

=========================Nginx===========================
【1】安装Nginx
root@ubuntu-xenial:~# apt-get install -y nginx
【2】查看Nginx的版本号
root@ubuntu-xenial:~# nginx -v
nginx version: nginx/1.10.3 (Ubuntu)
【3】启动Nginx
nginx version: nginx/1.10.3 (Ubuntu)
root@ubuntu-xenial:~# /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.
【4】访问Ngin页面
打开本地浏览器输入虚拟机的ip地址然后可以看到nginx正常工作。
Welcome to nginx!

=========================PHP===========================
【1】查看PHP的版本有哪些
root@ubuntu:~# apt-cache search php
【2】安装PHP7
//更新一下
root@ubuntu:~# apt-get update -y
//因为Nginx要依赖PHP-FPM才可以解析PHP脚本,所以要安装php php-fpm
root@ubuntu:~# apt-get install -y php7.0 php7.0-fpm
//安装完毕后,会自动创建一些配置文件*.ini
【3】查看PHP版本
root@ubuntu:~# php -v
PHP 7.0.33-0ubuntu0.16.04.4 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-0ubuntu0.16.04.4, Copyright (c) 1999-2017, by Zend Technologies
【4】配置PHP-FPM
root@ubuntu:~# vim /etc/nginx/sites-available/default
在配置文件39行处添加index.php
 38         # Add index.php to the list if you are using PHP
 39         index index.html index.htm index.nginx-debian.html index.php;
在配置文件51行处开启代码注释
 51         location ~ \.php$ {
 52                 include snippets/fastcgi-php.conf;
 53         #
 54         #       # With php7.0-cgi alone:
 55         #       fastcgi_pass 127.0.0.1:9000;
 56         #       # With php7.0-fpm:
 57                 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
 58         }
【5】检测nginx的配置文件
root@ubuntu:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
【6】确认配置是否监听
root@ubuntu:~# vim /etc/php/7.0/fpm/pool.d/www.conf
在配置文件大概36行出处
 35 ; Note: This value is mandatory.
 36 listen = /run/php/php7.0-fpm.sock
 37
 38 ; Set listen(2) backlog.
【7】重启nginx和PHP-FPM
root@ubuntu:~# nginx -s reload
root@ubuntu:~# /etc/init.d/php7.0-fpm  restart
[ ok ] Restarting php7.0-fpm (via systemctl): php7.0-fpm.service.
【8】查看PHP探针信息
root@ubuntu:~# cd /var/www/html/
root@ubuntu:/var/www/html# ls
index.nginx-debian.html
root@ubuntu:/var/www/html# vim phpinfo.php
在phpinfo.php文件中使用phpinfo()函数
然后在浏览器中访问就可以看到下面的信息。
http://192.168.10.100/phpinfo.php
		PHP Version 7.0.33-0ubuntu0.16.04.4
		
		
=========================Mysql===========================	
【1】安装MySQL
//下面安装MySQL服务端,客户端,以及PHP7.0对于MySQL的支持依赖
root@ubuntu:~# apt-get install -y mysql-server mysql-client php7.0-mysql
安装过程可能有点慢,途中会要求输入mysqlroot的密码,默认使用root
【2】使用MySQL默认的客户端连接
root@ubuntu:~# mysql -uroot -p
输入密码root
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2019, 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>
【3】安装phpmyadmin
root@ubuntu:~# apt-get install -y phpmyadmin
安装过程中选择默认的就可以,可能途中也会要求输入密码root
【4】创建软连接
//方便访问
root@ubuntu:~# ln -s /usr/share/phpmyadmin /var/www/html/mysql

=========================配置虚拟站点===========================
这里我们对PHPmyadmin进行配置虚拟站点
新增Nginx虚拟主机配置文件
【1】先创建一个目录,用来存放其他虚拟主机的配置文件
root@ubuntu:/etc/nginx# cd
root@ubuntu:~# cd /etc/nginx/
root@ubuntu:/etc/nginx# mkdir vhosts
root@ubuntu:/etc/nginx# ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params  win-utf
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       vhosts
【2】然后编辑默认的nginx配置文件,最后面添加一行代码引入我们刚才创建的目录
root@ubuntu:/etc/nginx# vim nginx.conf
//大概63行添加这行代码 include /etc/nginx/vhosts/*;
【3】在vhost目录下面新增www.mysql.conf文件
添加以下配置
server {
	listen 8080;#监听80端口
    root /var/www/html/mysql; #虚拟主机根目录定位到phpmyadmin
    index index.html index.htm index.nginx-debian.html index.php;
    server_name www.mysql.com;#虚拟域名
    location / { #URL隐藏index.php
            if ( !-e $request_filename ) {
                    rewrite ^(.*)$ /index.php/$1 last;
                    break;
            }
    }
    location ~ \.php {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $request_uri;
    }
}
【4】重启Nginx和PHP-fpm后查看效果
完成配置后,保存配置文件,重新载入Nginx配置,重启PHP—FPM,然后再浏览器输入http://192.168.10.100:8080
然后就可以使用虚拟站点。


=========================其他===========================
修改默认时区
//执行下面命令
root@ubuntu:/etc/nginx# dpkg-reconfigure tzdata
//然后会出现图形界面,先选择Asia,然后选择Shanghai.
Current default time zone: 'Asia/Shanghai'
Local time is now:      Wed May 22 08:51:34 CST 2019.
Universal Time is now:  Wed May 22 00:51:34 UTC 2019.

VirutalBox+Vagrant安装Ubuntu系统-快速搭建lnmp环境_第1张图片

VirutalBox+Vagrant安装Ubuntu系统-快速搭建lnmp环境_第2张图片

你可能感兴趣的:(linux,Vagrant,Ubuntu,Nginx,PHP7,lnmp)