您面临一个世纪难题:在本地主机上工作,但在服务器上不工作。解决方案很简单,使用服务器环境进行开发。
例如我有 3 台电脑,一台是 Mac,一台是 Windows,还有一台天知道。有一天 Ubuntu,另一个 kubuntu 或任何其他花哨的操作系统。如果您的服务器是 Debian 7,无论您在工作机器上使用什么操作系统,您的代码始终必须由 Debian 7 处理,并且 PHP 配置应该与您的真机一样接近。这样你就不会遇到像不同的文件路径、不同的行尾符号和其他一些疯狂的东西这样的错误。
我建议你试试 Vagrant 形式 https://www.vagrantup.com/downloads.html
你的步骤是这样的:
- 下载https://www.virtualbox.org并安装
- 下载https://www.vagrantup.com/downloads.html并安装
- 根据您的需要配置 vagrant。带有一些 php 扩展的 Debian 7 或您在生产服务器上拥有的任何东西。
- 只需使用终端、命令行或 windows power shell 导航到 vagrant 文件所在的目录,然后键入“vagrant up --provision”
- 编辑您的主要操作系统主机文件(在大多数 linux 上应该在此处:/etc/hosts)添加行:xxx.xxx.xx.xxx domain.vm (xxx.xxx.xxx.xxx - 您的虚拟机 IP 地址来自vagrant 配置文件。domain.vm 是你的域,它将为你的本地开发工作。输入你想要的任何东西,我只是喜欢使用 .vm tld 作为虚拟机)
现在 vagrant 将读取您的配置文件,下载所需的虚拟盒,安装 php,使您的主操作系统文件夹与虚拟盒文件夹保持同步。你会像往常一样编辑你的文件,vagrant 会确保在虚拟盒子上你有相同的文件。
您应该能够访问http://domain.vm/ 并在您的 kubuntu、windows、mac... 操作系统上查看您的站点在本地使用 debian 7 的虚拟机上运行。什么是最好的 - 您不必担心您在本地和服务器上使用的操作系统之间的差异。
此外,如果您将所有这些内容添加到 git 版本控制中,您将能够在 ±15 分钟内使用任何计算机处理您的项目(取决于下载 1 次虚拟机操作系统的互联网速度)。
也许我应该制作一个关于这一切的视频教程。但你应该可以在 Youtube 上找到一个。
只是为了让您的生活更轻松,这是我的工作 Vagrant 配置。我使用 ubuntu + PhalconPHP 框架 + MongoDb 和 Mysql。有2个文件。一个用于 vagrant 的主要内容:下载操作系统、挂载同步文件夹等,一个用于启动 vagrant 后的安装。如果你想重新运行安装脚本,只需输入 vagrant provision
Vagrantfile 文件(文件名只是“Vagrantfile”)
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Base Box
# --------------------
config.vm.box = "precise32"
config.vm.box_url = "http://files.vagrantup.com/precise32.box"
# Connect to IP
# --------------------
config.vm.network :private_network, ip: "192.168.5.0"
# Forward to Port
# --------------------
#config.vm.network :forwarded_port, guest: 80, host: 8080
# Optional (Remove if desired)
config.vm.provider :virtualbox do |v|
# How much RAM to give the VM (in MB)
# -----------------------------------
v.customize ["modifyvm", :id, "--memory", "700"]
# Uncomment the Bottom two lines to enable muli-core in the VM
#v.customize ["modifyvm", :id, "--cpus", "2"]
#v.customize ["modifyvm", :id, "--ioapic", "on"]
end
# Provisioning Script
# --------------------
config.vm.provision "shell", path: "init.sh"
# Synced Folder
# --------------------
config.vm.synced_folder "./", "/var/www/", :mount_options => [ "dmode=775", "fmode=644" ], :owner => 'www-data', :group => 'www-data'
end
以及安装文件(文件名为“init.sh”):
#!/bin/bash
# Using Precise32 Ubuntu
# to use closest ubuntu mirror by geographic location
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-updates main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-backports main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
echo 'deb mirror://mirrors.ubuntu.com/mirrors.txt precise-security main restricted universe multiverse' | cat - /etc/apt/sources.list > temp && mv temp /etc/apt/sources.list
sudo apt-get update
sudo apt-get update
#
# For PHP 5.5
#
sudo apt-get install -y python-software-properties
sudo add-apt-repository ppa:ondrej/php5
sudo apt-get update
#
# MySQL with root:<no password>
#
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server
#
# PHP
#
sudo apt-get install -y php5 php5-dev apache2 libapache2-mod-php5 php5-mysql php5-curl php5-mcrypt php5-gd php5-imagick
#
# Redis
#
sudo apt-get install -y redis-server
#
# MongoDB
#
sudo apt-get install mongodb-clients mongodb-server
#
# Utilities
#
sudo apt-get install -y curl htop git-core gcc autoconf
sudo apt-get install -y libpcre3-dev
#
# Redis Configuration
# Allow us to Remote from Vagrant with Port
#
sudo cp /etc/redis/redis.conf /etc/redis/redis.bkup.conf
sudo sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf
sudo /etc/init.d/redis-server restart
#
# MySQL Configuration
# Allow us to Remote from Vagrant with Port
#
sudo cp /etc/mysql/my.cnf /etc/mysql/my.bkup.cnf
# Note: Since the MySQL bind-address has a tab character I comment out the end line
sudo sed -i 's/bind-address/bind-address = 0.0.0.0#/' /etc/mysql/my.cnf
#
# Grant All Priveleges to ROOT for remote access
#
mysql -u root -Bse "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION;"
sudo service mysql restart
#
# Composer for PHP
#
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
#
# Apache VHost
#
cd ~
echo '<VirtualHost *:80>
DocumentRoot /var/www/public
SetEnv APPLICATION_ENV "development"
</VirtualHost>
<Directory "/var/www/public">
Options Indexes Followsymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/logs/error.log
' > vagrant.conf
sudo mv vagrant.conf /etc/apache2/sites-available
sudo a2enmod rewrite
#
# Install PhalconPHP
# Enable it
#
cd ~
git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install
echo "extension=phalcon.so" > phalcon.ini
sudo mv phalcon.ini /etc/php5/mods-available
sudo php5enmod phalcon
sudo php5enmod curl
#
# Install PhalconPHP DevTools
#
cd ~
echo '{"require": {"phalcon/devtools": "dev-master"}}' > composer.json
composer install
rm composer.json
sudo mkdir /opt/phalcon-tools
sudo mv ~/vendor/phalcon/devtools/* /opt/phalcon-tools
sudo ln -s /opt/phalcon-tools/phalcon.php /usr/bin/phalcon
sudo rm -rf ~vendor
#
# PHP.ini params edits
#
sudo echo "; ######### PHP.ini modifications from vagrant init.sh #######" >> /etc/php5/apache2/php.ini
sudo echo "error_reporting = E_ALL | E_STRICT" >> /etc/php5/apache2/php.ini
sudo echo "display_errors = On" >> /etc/php5/apache2/php.ini
#
# Reload apache
#
sudo a2ensite vagrant
sudo a2dissite 000-default
sudo service apache2 reload
sudo service apache2 restart
sudo service mongodb restart
#echo -e "----------------------------------------"
#echo -e "To create a Phalcon Project:\n"
#echo -e "----------------------------------------"
#echo -e "$ cd /var/www"
#echo -e "$ phalcon project projectname\n"
#echo -e
#echo -e "Then follow the README.md to copy/paste the VirtualHost!\n"
#echo -e "----------------------------------------"
#echo -e "Default Site: http://192.168.5.0"
#echo -e "----------------------------------------"
####### writable Volt directory
sudo mkdir /vagrant/cache/volt/
sudo chmod 777 /vagrant/cache/volt/
当然,如果您需要 Debian 7,安装脚本应该适合您的需要,并根据您的操作系统编辑有关 ubuntu 的 Vagrantfile 行。以下是所有受支持操作系统的列表:http://www.vagrantbox.es
在 Vagrant 文件中更改这两行:
config.vm.box = "precise32"
config.vm.box_url = "http://files.vagrantup.com/precise32.box"
而且还要更改安装脚本,我不使用 Debian,不知道会出什么问题。