Skip to content

Instantly share code, notes, and snippets.

@pmcfernandes
Last active May 31, 2019 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmcfernandes/ca33a1813e4cacd784708578c55f78d4 to your computer and use it in GitHub Desktop.
Save pmcfernandes/ca33a1813e4cacd784708578c55f78d4 to your computer and use it in GitHub Desktop.
# Tool create wordpress website in Windows and install in IIS
# and create database in mysql
# Author: Pedro Fernandes
# create-wp.ps1
param([String]$site = "mywebsite.com", [String]$u='root', [String]$p='P@ssw0rd')
Write-Host "create-wp.ps1- Tool create wordpress website in Windows"
Write-Host "Usage:"
Write-Host " -site: [mywebsite.com] Domain name"
Write-Host " -u: [root] Database username"
Write-Host " -p: [P@ssw0rd] Database password"
Write-Host ""
Write-Host ""
Import-Module WebAdministration
$path = "E:\HostingSpaces"
$sitepath = "$path\$site"
# Create folder if not exists
New-Item -ItemType Directory -Force -Path $sitepath | Out-Null
$output = "$sitepath\latest.zip"
$start_time = Get-Date
Invoke-WebRequest -Uri "https://wordpress.org/latest.zip" -OutFile $output | Out-Null
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($output, $sitepath)
Get-ChildItem -Path "$sitepath\wordpress" -Recurse | Move-Item -Destination $sitepath
Remove-Item $output
Remove-Item "$sitepath\wordpress"
Set-Content $sitepath\wp-config.php @"
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', '$site');
/** MySQL database username */
define('DB_USER', '$u');
/** MySQL database password */
define('DB_PASSWORD', '$p');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8mb4');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define('AUTH_KEY', 'ULsm{|l&jTzG}Pqc|Hvz}:wNnjvD97 -UE[2q[h[4z9WA;6!RrPYFK=.#N2)QypV');
define('SECURE_AUTH_KEY', 'g@`bQ!{ 64XR4lP,oS>uy>F<eAEHNfQJE>=>ktw)]^ba-/R4hw>QO/1xO+n-;4xS');
define('LOGGED_IN_KEY', '*i~ RXV$E(A ~DzgA&0q]bDiXO5Y$N#ngk^Al+ad1NMsrdEVXjw>*j$~e)^JEJux');
define('NONCE_KEY', '?MtrhVyA@ cd9D/0z9f^bsmLQdS;QI8s-+rg~icc?qA4G<<V2oqP[65jTo(yY8;6');
define('AUTH_SALT', 'n|V(*W7lUM,tG4LiHBd1LEao}$e+,F9$`Mc}(VOc|R)EQ+4si:FJu)T5nWJ9wRQA');
define('SECURE_AUTH_SALT', '%?@NPJEJ$~I+b$`OlVzt.!eFQQ]37IuP<iJvvT;FxGmb9YQ!Iv|yZNyZ%<_Bf?Q^');
define('LOGGED_IN_SALT', '!b!`3JLfDJ[3zT$PKmTW-_yJ<~%)@YU<p1`YNW_47+gX@b{USW,F sy9KB:xM@g_');
define('NONCE_SALT', '(O`PlX]l>6zk@ 5J[@ZEObI]BjTYKsCQ4~%5XulyE!1>DX]H~.aHrKq-]Ts&ly#A');
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
'$table_prefix' = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
"@
if (!(Test-Path IIS:\AppPools\$site -pathType container))
{
New-Item IIS:\AppPools\$site
}
if (!(Test-Path IIS:\Sites\$site -pathType container))
{
New-Item IIS:\Sites\$site -physicalPath $sitepath -bindings @{protocol="http";bindingInformation=":80:"+$site}
Set-ItemProperty IIS:\Sites\$site -name applicationPool -value $site
}
try
{
# Load MySql.Data
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
}
catch
{
$e = $_.Exception.Message
Write-Host $e
exit;
}
$connectionString = 'Server=localhost;Database=mysql;Uid=' + $u + ';Pwd=' + $p
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)
$conn.Open()
# Check if database is closed
If ($conn.State -eq "System.Data.ConnectionState.Closed")
{
Write-Host $e
exit;
}
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection = $conn
# Drop Schema if existes
$cmd.CommandText = 'DROP DATABASE IF EXISTS `' + $site + '`'
$cmd.ExecuteNonQuery()
# Create Schema
$cmd.CommandText = 'CREATE SCHEMA `' + $site + '`'
$cmd.ExecuteNonQuery()
# Close connection
$conn.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment