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.
Related
I have an api written in PHP for a backend service that supports my Android app. I'm in the process of setting up different environments - development, staging, production - each with they're own database. The API performs queries to its respective MySQL database and everything is up and running just great in development.
My question is about the proper technique for handling different database connections for the api in each environment? Is there an easier way to change the database that my php->mysql connections use than changing them all by hand manually before deploying from, say, development to staging?
I'm coming from an Android environment, so what comes to mind right away is Gradle's build flavors and how that allows different code to be used based on the specific build variant you want.
I think that's a good use case for Apache environment variables, which can be set in the httpd.conf file (or an .htaccess file).
Basically, you can something like:
SetEnv DATABASE_NAME "MyDB"
in your httpd.conf file, and then in PHP use:
$_SERVER['DATABASE_NAME']
to access it.
In general, it's good practice to keep environment-specific things out of your code so that your code is more portable. Your case is a perfect example.
Edit (by rpm):
Be sure to restart apache after modifying the httpd.conf file. I had issues getting this answer to work, but restarting Apache fixed everything.
What we do is put the database connection parameters in a separate include file, e.g.
$db_host = "xxx";
$db_user = "xxx";
$db_password = "xxx";
$db_database = "xxx";
Then the other scripts can use
require_once('db_params.php');
and use these variables when making the database connection.
Then we just use different contents of the script in different environments.
In our team, I used to be the only developer, coding directly on our remote dev server but we're hiring more developers so we have a need to move the codebase to our local machines so we don't encounter any file collisions.
However, even though I want to use a local codebase, I don't want to use a local database. Syncing the database seems like a hassle and is to be honest not really needed. So we're sticking to multiple codebases, but one database.
How can I run Wordpress on my local machine, while still using our remote server database? How do I set this up in MAMP?
I'm kind of new in setting up servers/ports/databases etc so it's not my strong suite. The problem which I don't know how to get around is the urls.
For instance, our WP sites have the url format:
site1.portal.dev
site2.portal.dev
When I type these URLs I want the codebase to be local but the database to be our remote dev server.
Update the config to point to the remote database. Job done.
As long as the configuration of that database allows you to connect to it, it's very simple.
An example of updating the wordpress config
I work in a multi-developer team. I've found that the best option is to have a full local environment.
We use wp-migrate-db pro for copying the dev database down locally. Once configured (per site) it takes a minute to grab the latest copy from dev.
Having this separation allows your team to make changes for their local development efforts (such as entering bad data, test pages, posts etc) without affecting other developer efforts.
This approach also works well for working with developers of various skill levels...and helps to reduce bigger issues that you may come across later on.
Using your site1.portal.dev should work, as the site_url and home_url. When you click on a link in your dev portal, it will continue to use site1.portal.dev, as WordPress thinks your site resides there. The database just holds the information, it doesn't matter where it resides.
You can connect to your remote mysql instance by providing the correct host, username, password. Usually it would be localhost, but in your case, you will want whatever the IP/DNS address for the machine that is hosting your mysql server is.
You can test the mysql connection with
mysql <database> -h <host> -u <username> -p
add -P <port> if your mysql server is listening on a different port.
I know this is an old question, I just post the answer I found that works for me in case anyone is looking for the solution of "How can I run Wordpress on my local machine, while still using our remote server database"
Install WordPress on your server (http://yourdomain.com)
Configure it with your database
Make sure whitelisting your local machine IP in "Remote MySQL" within your server's hosting panel
Pull down the entire WordPress directory onto your local machine
into a project folder
Add project folder to MAMP or your local web server app of choice
(make a note of your Server Name)
Open your LOCAL copy of wp-config.php Change 'DB_HOST' to
'yourdomain.com'
And add the following just above 'require_once(ABSPATH .
'wp-settings.php'); in wp-config.php file
define('WP_CACHE', true); $currenthost = $_SERVER['HTTP_HOST']; $mypos = strpos($currenthost, 'localhost'); if ($mypos === false) { define('WP_HOME','http://yourdomain.com'); define('WP_SITEURL','http://yourdomain.com'); } else { define('WP_HOME','http://localhost'); define('WP_SITEURL','http://localhost'); }
Replace each instance of 'localhost' with your 'Server Name' in MAMP. (localhost is default)
Source URL: https://coderwall.com/p/ck8v4a/remote-database-with-local-wordpress-instance
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 need to connect to a remote mysql database in PHP.
Specifically I have this as a constant:
define("DATABASE_SERVER", "localhost");
...
$db = #mysql_connect(DB_SERVER, DB_USER, DB_PASS);
I want to copy the site to my local machine but still connect to the main database.
Can I do this? if so, how? All I have is FTP access.
You need the IP/URL of the DB and put that instead of localhost.
You need to make sure the DB configuration allows remote connections/outside the local network.
Once you get the IP of the DB (should be given by the support team, if it is being supported), you will know.
If all you need to do is copy the data and build it locally on your machine, so you don't destroy live data, use the export tab in PHPmyadmin. I assume you don't have too much data.
PHPmyadmin->select the DB (from the left frame)->press the export TAB (top, right frame).
Should be clear from there.
You can export it as SQL and run this SQL on your local machine.
I would guess that your database server is refusing remote connections. If you have cPanel there's a section called something like "Remote Database Access" where you can enable them to certain IP ranges.
I suggest that you export the database in question, and import it locally. This way, you won't destroy live data if you mess up.
Many, if not most, hosting providers also provide a DB interface like phpMyAdmin. You can export the database from there.
+1 gnud. You shouldn't use the production database for development work, even if you could configure it to listen for external access on a public internet interface(*). One slip of the mouse and you're scribbling over important live data. Instead you should be working on a standalone development server of your own, from which you can push the code changes to the live server when you they're working and tested.
(*: Which is in itself a very bad idea: if the passwords are too weak or you're running an old version of MySQL with exploits available you are going to get hacked by automated probes very quickly.)
You'd normally export the database from the shell using the mysqldump dbname > dbdump.sql command, and import it on your local server by piping it into mysql dbname < dbdump.sql. There might also be a MySQL admin web interface installed on the server you could use. But if all you've got is FTP, you haven't really been given the tools you need to do development work. See if you can get an empty or dummy database to test the site with.
(What's more, FTP is a nasty business... really in this century you should be using SFTP.)
The simplest approach is to have DATABASE_SERVER be defined as database (or something equally meaningful) and put an entry in your hosts file. On your local machine this can point to the remote address, on the server this will point to 127.0.0.1.
define('DATABASE_SERVER', database);
On windows your sites file is:
c:\windows\system32\drivers\etc\hosts
with:
database <remote_ip>
Linux:
/etc/hosts
with:
database 127.0.0.1
Side note: is your remote DB a production DB? If so you shouldn't really be using the remote DB for development work, unless you're using a separate dev DB - even then I wouldn't run the risk of rogue dev queries eating up the remote DB resources.
Update: Doh, missed you're limited to FTP access.
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