Advanced Vagrant: This article will not explain how to install Vagrant, Python, and PHP in simple Python and PHP development environments.
This article mainly explains how to use a local environment as a development environment. after writing the code, it can be automatically synchronized to the virtual machine (test environment) to improve the test development efficiency.
For test convenience, both Python and PHP use the built-in Web Server instead of Nginx or Apache.
The built-in Web Server using Python and PHP is only suitable for development and testing environments and not for production environments.
1. configure the Vagrant environment first. Edit the Vagrant file, as shown in the following figure. remove the comment.
Config. vm. network: forwarded_port, guest: 3000, host: 8080
In this way, you can access port 8080 on the VM through host machine port 3000. In this step, access testing is performed locally. You do not need to run the test in the virtual machine.
2. how to automatically synchronize local code to the test environment and modify the Vagrant file. As follows.
Config. vm. synced_folder "./data", "/vagrant"
The first parameter stores the local code in the host folder, and the second parameter is the directory corresponding to the virtual machine. when the local data directory is updated, the vagrant directory is synchronized. Is it very convenient.
The data directory structure of the host is as follows:
Localhost: data angel $ ll-R
Total 0
Drwxr-xr-x 3 angel staff 102 4 28 phpTest
Drwxr-xr-x 3 angel staff 102 4 28 pythonTest
./PhpTest:
Total 8
-Rw-r -- 1 angel staff 192 4 28 router. php
./PythonTest:
Total 8
-Rw-r -- 1 angel staff 88 4 28 19:36 index.html
The directory structure on vagrnt on the virtual machine is as follows:
[Vagrant @ localhost vagrant] $ ll-R
.:
Total 0
Drwxr-xr-x 1 vagrant 102 Apr 28 11: 51 phpTest
Drwxr-xr-x 1 vagrant 102 Apr 28 12:52 pythonTest
./PhpTest:
Total 4
-Rw-r -- 1 vagrant 192 Apr 28 11: 51 router.
./PythonTest:
Total 4
-Rw-r -- 1 vagrant 88 Apr 28 12:36 index.html
No. is it exactly the same? it's fully automated. is it very convenient.
3. start the built-in Python Web Server
Run the following command in the pythonTest Directory of the VM:
[Vagrant @ localhost pythonTest] $ python-m SimpleHTTPServer 3000
Serving HTTP on 0.0.0.0 port 3000...
Enter http: // localhost: 8080 in the host browser,
Welcome to Python
This is a simple python environment.
4. start the web Server that comes with php
Run the following command in the phpTest directory on the VM:
[Vagrant @ localhost phpTest] $ php-S 0.0.0.0: 3000 router. php
PHP 5.5.11 Development Server started at Mon Apr 28 13:37:19 2014
Listening on http: // 0.0.0.0: 3000
Document root is/vagrant/phpTest
Enter http: // localhost: 8080 in the host browser,
Welcome to PHP
This is a simple PHP environment.
You have noticed that php starts the built-in Web Server and wants remote access. the host must be 0.0.0.0. I reported an error because I started to use localhost and debugged it for one afternoon.