WordPress auto install single php

Hi friend, are you a developer or just a WordPress blogger? Do you want to install WordPress more quickly and more easily? You can use Softaculous but it is too huge for us and we want to ensure that our WordPress is installed step by step like the way human do. So i wrote a script myself. This is final result, you can download and change settings to meet your requirements and test it.
download wordpress_installer.zip
Let me show you my solution to create it, you will see there some steps we can make auto:

  • Create MySQL database, MySQL user and Grant privileges
  • Unzip wordpress
  • Rename wordpress folder
  • Setup config.php
  • Install site

Fist we need to connect to MySql to prepare a database for new WordPress.
Each steps if there is error exit function will prevent the executing of next step.

/* Connect mysql*/
$con=mysqli_connect("localhost","root","8ib9Bv4F18");
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error()."<br/>";
	exit;
}

Create new MySQL user if not exist (just MySQL query)

/* Create user */
$sql = "CREATE USER '".$dbname."'@'localhost' IDENTIFIED BY '".$mysql_password."';";
if (mysqli_query($con,$sql)) {
	echo "Create user successfully<br/>";
} else {
	echo "Error creating user: " . mysqli_error($con)."<br/>";
	echo '<a href="http://vps.nguoivietcode.com/phpmyadmin/" target="blank">Delete old user</a> to continue';
	exit;
}

Create new MySQL database if not exist (just MySQL query)

/* Create database */
$sql="CREATE DATABASE ".$dbname;
if (mysqli_query($con,$sql)) {
	echo "Create database successfully<br/>";
} else {
	echo "Error creating database: " . mysqli_error($con)."<br/>";
	echo '<a href="http://vps.nguoivietcode.com/phpmyadmin/" target="blank">Delete old database</a> to continue';
	exit;
}

Grant privileges on database to the user created (MySQL query again)

/* Grant privileges */
$sql = "GRANT ALL ON ".$dbname.".* TO '".$dbname."'@'localhost';";
if (mysqli_query($con,$sql)) {
	echo "Grant privileges successfully<br/>";
} else {
	echo "Error Grant privileges: " . mysqli_error($con)."<br/>";
}

Download wordpress from wordpress.org and place the zip file in the same folder with the script file.
Use extractTo function of ZipArchive class to unzip wordpress file.

/*Unzip wordpress*/
$zip = new ZipArchive($wordpressfile);
$res = $zip->open($wordpressfile);
if ($res === TRUE) {
	$zip->extractTo(dirname(__FILE__));
	$zip->close();
	echo 'Unzip successfully <b>'.$wordpressfile.'</b><br/>';
} else {
	echo 'Unzip fail! <b>'.$wordpressfile.'</b><br/>';
}

Rename wordpress folder.

/*Rename folder*/
if(rename($wpfoldername, $foldername)){
	echo 'Rename successfully from <b>'.$wpfoldername.'</b> to <b>'.$foldername.'</b><br/>';
}else{
	echo 'Can not rename <b>'.$wpfoldername.'</b> to <b>'.$foldername.'</b><br/>';
	exit;
}

Chmod if cannot write to files and folders

/*chmod 777 if need*/
chmod($foldername, 0777);

Display some information to debug if you need

/*display information*/
echo 'dbname=dbuser='.$dbname.'<br/>';
echo 'mysql_password='.$mysql_password.'<br/>';

Setup config.php. We use CURL to post the data to setup-config.php?step=2, like the way we enter database connection details and click submit button.

/*Setup config*/
$url = 'http://nguoivietcode.com/'.$foldername.'/wp-admin/setup-config.php?step=2';
echo 'Setup config : <b>'.$url.'</b><br/>';
$fields = array(
	'dbname' => urlencode($dbname),
	'uname' => urlencode($dbname),
	'pwd' => urlencode($mysql_password),
	'dbhost' => urlencode('localhost'),
	'prefix' => urlencode('wp_')
);
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);

Finally we use CURL again to install WordPress site

/*Install site*/
$url = 'http://nguoivietcode.com/'.$foldername.'/wp-admin/install.php?step=2';
echo 'Install website : <b>'.$url.'</b><br/>';
$fields = array(
	'weblog_title' => urlencode($sitetitle),
	'user_name' => urlencode('admin'),
	'admin_password' => urlencode('somepassword'),
	'admin_password2' => urlencode('somepassword'), 'admin_email' => urlencode('adminemail@gmail.com'), 'blog_public' => urlencode('1') ); $fields_string=''; foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); $result = curl_exec($ch); curl_close($ch); echo 'Login: <a href="http://nguoivietcode.com/'.$foldername.'/wp-admin" target="blank">http://nguoivietcode.com/'.$foldername.'/wp-admin</a>'; 

We also use hard-coded password and cookie to secure the script. If some one want to run the script, he have to enter correct username and password.

Hard work so far. Download, change and use it as you want.
download wordpress_installer.zip

Don't be shellfish...Share on FacebookShare on Google+Tweet about this on TwitterPin on Pinterest

Leave a Reply