As is usual for a PHP/MySQL project, I have a config file with database credentials.
I test on my own local LAMP set up and do a git push to the live site.
The database name, user and password are different on the two.
What is the best practice for managing the two config.php files?
Could I make local edits to change from prod to test, but not commit that one file? That's assuming there are no other changes that need to be made to the file.
Or is there anything equiv to #ifdef in PHP? I know there's no preprocessor!
Related
I've been developing PHP pages for several weeks now and am getting much more confident in my code. I've obtained a hosting account through Bluehost, and am ready to begin making some live pages. Previously I have been using XAMPP on Windows and developing all of my pages on my local computer. I'm trying to determine the best practice for creating my page locally and then easily moving it to my hosting server. I use Filezilla to transfer my files. Here are my main questions:
1.) How can I ensure my local and live MySQL databases stay in sync? I have been manually creating the database locally, and then taking the same code and applying it to my live server. (I have phpmyadmin locally and on the live server, but dont know how to use it to streamline this process)
2.) I have to change the mysql password and connection credentials on all files before moving them from local server to live server. Is there a way around this?
For question 1 you could dump the database and import it at the production end each time you do an upload, or you could use mysql replication.
For question 2 it is quite common to have the database connection details in a separate php file and then include that file at the top of each page that needs to connect to the database
Then you can just change that one file, or only upload the one with the production database details.
db.php
<?php
$dbuser="";
$dbpass="";
$dbname="";
$dbserver="";
?>
myfile.php
<?php
include("db.php");
$conn = mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db($dbname);
//....
?>
You could point your mysql_connect() host to your new server's IP address. Therefore, no matter where your code is, it will connect to the socket.
$link = mysql_connect('<your ip here>', 'mysql_user', 'mysql_password');
I always make my projects so that I have to change 1 line of code to fluctuate between dev and production.
I have a base file where i specify my environment like this:
$env = 'dev'
Then in my database setup variables i have something like this:
if($env == 'dev') {
specify dev variables
} else if($env == 'prod') {
specify prod variables
}
The beauty of this is that when i want to move between dev and prod, i only have to change 1 variable.
Use a version control system such as svn or git; look at phpmig for database management, look at deployment tools such as capistrano, make sure you test on a replica of the production server (even if you just use a virtual host)
I'm pretty much in the same position as you and here is how I plan to do it.
I don't plan to sync my local database with the live one but just move the local database once to the online one when I'm done. The way I do that is just, in PHPMyAdmin select the database I want and export it as a .sql file. Then on the online server I simply import that file.
The best way I know is to have a separate file where you have a function which looks something like this:
function connectToDB(){
$mysqli = new mysqli("localhost", "root", "123", "myDatabase");
return $mysqli;
}
Then when you want to use that you just import the file in the top of your other PHP files and use it like this:
$mysqli = connectToDB();
This is my way of doing it. If someone knows a better way, I would be happy know it.
Deploying from development to production is not a trivial task and can be very complicated. Companies invest a lot of resources in order to make deployment a logical and stable process. I recommend to break your question into pieces and ask more specific.
As response to your questions, what I'd do:
1) When deployment requires a change in the database structure, write those changes in sql scripts. When you deploy you'll need to run those scripts and update the code (as you can figure out, the time in between will make the application unstable or broken). Before anything, take a copy of the production database onto development and try the scripts first on development. You will get so databases in sync each time you deploy.
2) How you save passwords depends on whether you are using frameworks or not. Following frameworks, it's a good approach to define an environment variable and set passwords according to the environment (dev or production).
As regards to code deployment, even if you are a unique developer, I'd use svn or git to save your projects under versions control. It will make code updates much more simple and help you to roll back if desired provided you create tags for each deployment.
Best practices are to write database migration scripts, version them. Then each time you upload to production, you store the values of these versions in the database. Only run the versions that you have not run before.
Have different local config files outside of your repository. This allows you to maintain the necessary application config in the repo with a main config file, then the local config file overrides database and environment settings.
I'm currently working on a PHP site, and am using Git both locally (development) and on the production site (a repo on my web server uses a post-recieve hook to deploy to the web root), the details of which are outlined at toroid.org's 'Using Git to manage a web site' article.
Issue is that I also have a config.php file (of sorts) that I use for connecting to the database that differs between my local development environment (a local install of MySQL with test data) and my remote 'production' web server (which has it's own 'live' database). Whenever I push my changes to the website however the config file goes with it and replaces my 'live' config with settings connecting to the (nonexistent) development one! I then have to manually SSH to my web server and replace the config.php file, kind of defeating the purpose!
Is there any way I can get Git to... kind of 'desynchronize' the config.php file? The 'development' config.php should never get saved to the git repo, and when the production server deploys the PHP scripts etc to the web root directory, it should also leave the existing 'production' config.php file untouched.
Is there any way of doing this with Git? Thanks a million for your suggestions!
TC
First I would remove config.php from the repository, then I would create
a file named ".gitignore" and add the filename "config.php" to it.
When i've worked on Drupal sites before, if there is internal access to the server, or if remote desktop access is available, i've always developed it on the machine it would be ran from when live, and just not made it public on the server.
However, what is the best thing to do if you don't have access to the server yet, for example if the client hasn't got anything in place?
I need to be able to build and test the solution on my local machine, or on my VPS which I have RDP access to, and be able to move it over with as much ease as possible to the clients server when ready.
Any tips or best practices? As far as i'm aware Drupal doesn't have any specific migration tools? I could be wrong though
I don't work with Drupal, but for Prestashop, Wordpress, Zencart, etc. I always use the same workflow:
I setup a vhost in my virtual sever, usually using a subdomain of my own domain (like customer.mydomain.com). Install the software with its DB etc. on the server. Setup FTP access.
I get a local copy of the files, which I maintain in a local git repository, pushing to github for backup purposes mainly.
I work with ZendStudio and configure a remote server and set it up to upload the files when I save them, so I can check them pretty much as if I were working locally. But the main advantage of this approach is that I can share the project with the customer as it progresses.
When I have to move to final server, at least with Wordpress, I have to search/replace the domain name, which wordpress saves on DB. But I do it locally. I download the entire DB as an SQL file through phpmyadmin, open it, searc-replace and upload it again via phpmyadmin to the permanent server.
With ZenCart and others the problem is the config file, which stores some paths. For long projects or long term customers I modify the config file to use some config details or anothers depending on the server name.
adding to the above comment...
Check the "backup and migrate" module and the "backup files" module. "Backup and migrate" is useful in any setup...
with this I was able to do a barebones drupal install and then migrate/replace the database with the one backed up from my local system... if the databases are named differently you will still need to edit the settings.php
"backup files" is useful for themes and content assets like images etc. but is essentially just a wrapper around gzip
I typically develop on my local machine and then upload to server once complete.
All you need to do is change the folder name in /sites/ and change the settings.php file to reflect the server settings/domain.
Something you should be aware of:
If you are uploading files on your local installation, the file paths will be wrong on the server and you will need to execute a one off mysql replace query.
Make sure you use relative paths in any hard coded links.
I am working on a ecommerce based on MagentoCommerce. I use 3 environments: dev (on my local machine), staging and production (both on my dedicated server).
The problem is that when I want to switch from local to staging, I have to edit my hosts file to point the domain used by Magento to my server's IP. However, this is time consuming and I was wondering what other magento developers out there were using as a strategy to not always have to change the hosts file when switching from an environment to another. Plus, when my ecommerce goes into production, I'll have to deal with 2 environments on the same IP.
The best would be if Magento had the domain hardcoded in only one file. That way I could keep different config files in each environment. Is that the case ? Otherwise, what places are domains "hard coded" ?
I'm running Magento on a Production server and a number of development and test domains.
The domain isn't hardcoded in any file - the domain is all in the database.
You have to change the secure base url and the unsecure base url when you move from server to server. Both of these are stored in the database and can be changed in the web based administration screens. When moving the database from one host to another, I use a script that does little but update these values. Here's the SQL you need to update these values:
update core_config_data
set value='http://whatever.com/'
where path='web/unsecure/base_url';
update core_config_data
set value='https://whatever.com/'
where path='web/secure/base_url';
You may also want to pay attention to the local.xml file where the database connection is configured. I'm sure that you'll want to have the different instances using different databases. In my configuration, I leave this file out of source control, and configure it just once for each instance.
I am currently working on a web application that uses PHP and MySQL, but I do not have shell access to the server (working on that problem already...). Currently, I have source control with subversion on my local computer and I have a database on the local computer that I make all changes to. Then, once I've tested all the updates on my local computer I deploy the site manually. I use filezilla to upload the updated files and then dump my local database and import it on the deployment server.
Obviously, my current solution is not anywhere near ideal. For one major thing, I need a way to avoid copying my .svn files... Does anyone know what the best solution for this particular setup would be? I've looked into Capistrano a bit and Ant, but both of those look like it would be a problem that I do not have shell access...
I'm using Weex to synchronize a server via FTP. Weex is basically a non-interactive FTP client that automatically uploads and deletes files/directories on the remote server. It can be configured to not upload certain paths (like SVN directories), as well as to keep certain remote paths (like Log directories).
Unfortunately I have no solution at hand to synchronize MySQL databases as well...
Maybe you could log your database changes in "SQL patch scripts" (or use complete dumps), upload those with Weex and call a remote PHP script that executes the SQL patches afterwards.
I use rsync in production but you could do this:
Add a config table into your site to hold what level of DB you are currently at.
During development, store each set of SQL changes into a single file (I use something like delta_X-up.sql). These will stay in your SVN as well. So, for example, if you are at delta_5 and add a table between the current release and the new release, all the SQL needed will be put in delta_6-up.sql
When it comes time to build, export the repo instead of using a checkout. This lets you ignore all the SVN cruft that comes along since you won't need that into production.
Use Weex to push those changes into production (this would be were I would use rsync but you don't have that option). Call a remote script that checks your config DB to see what delta level you are currently at, parse the directory with you delta_x-up.sql files and see if there are any new ones. If there are, read them and run the SQL inside.
You can do a subversion export (rather than checkout) to different directory from your working copy, then it will remove all the .svn stuff for you