An open API service indexing awesome lists of open source software.

https://github.com/joshmckibbin/wp-create

Create and Destroy local WordPress sites in Ubuntu
https://github.com/joshmckibbin/wp-create

ubuntu wordpress wordpress-development wp-cli wsl-ubuntu wsl2

Last synced: about 2 months ago
JSON representation

Create and Destroy local WordPress sites in Ubuntu

Awesome Lists containing this project

README

          

# WP-Create

This repository contains shell scripts designed to automate the creation and removal of local WordPress sites running on Ubuntu.

## Prerequisites

**Ubuntu (or Ubuntu running on WSL2)** with:
- Apache with mod_rewrite enabled
- MariaDB 10.6+ with root access
- PHP 8.2+ with the curl, imagick, json, intl, mbstring, mysql, xml and zip extensions installed.
- wp-cli (The `wp-create.sh` script will attempt to install it if it isn't installed)

### Installing the prerequisites
With root access, run the following:
```sh
apt update && apt install -y apache2 && systemctl enable apache2 && \
a2enmod rewrite && systemctl restart apache2 && \
apt install -y mariadb-server && mariadb-secure-installation && \
apt install -y php php-curl php-imagick php-json php-intl php-mbstring php-mysql php-xml php-zip && \
systemctl restart apache2
```
Set a root password for MariaDB when asked if you want to change the root password. It should be the same value as `DB_ROOT_PASS` in the `.env` file.

Note: If http://localhost will not resolve after installation in WSL2, you may need to disable Fast Startup in your Windows settings and reboot your computer.

## Scripts

### `wp-create.sh`
This script uses set variables in an `.env` file to automate the creation of local WordPress sites.
See the [sample .env file](.env-sample)

It prompts for a `TITLE` and a `SLUG`. If no `TITLE` is provided, it defaults to 'WordPress'.
If no `SLUG` is provided, it defaults to generating a `SLUG` from the `TITLE` truncated to a maximum of 15 characters.
It then performs the following functions:

- Creates the necessary directory structure at '`DEV_DIR`/`SLUG`/wordpress'
- Installs `wp-cli` if necessary
- Downloads the latest version of WordPress
- Creates the wp-config.php file
- Creates the database user `DB_USER` if they do not already exist
- Creates a new database with a name generated by replacing the dashes in the '`SLUG`' with underscores (`DB_NAME`)
- Grants all permissions for `DB_NAME` to `DB_USER`
- Installs WordPress with a database tables prefix of 'wp_`DB_NAME`_'
- Checks `DB_DUMP_DIR` for an existing database dump file and imports it. In this order:
- `SLUG`-dev.sql
- `SLUG`.sql
- `SLUG`-prod.sql
- If `SMTP_USER` and `SMTP_PASS` are set, it installs and activates the [Simple SMTP Mailer](https://wordpress.org/plugins/simple-smtp-mailer) plugin.

### `wp-destroy.sh`
**This script removes everything and should be used with extreme caution.**

Removes a WordPress site created by running the `wp-create.sh` script:
- Prompts for the site's `SLUG` and then asks for confirmation of deletion
- Asks if you want to delete the site's database
- Asks if you want to delete the Apache config file

## Usage

1. Create and/or navigate to the directory where you want to create WordPress sites (Should be the same as `DEV_DIR`)

```sh
mkdir -p /path/to/dev/directory && cd /path/to/dev/directory
```

2. Clone (or download and extract) the repository into the directory

```sh
git clone git@github.com:joshmckibbin/wp-create.git .
```

3. Make sure you have execute permissions for the scripts

```sh
chmod +x wp-create.sh wp-destroy.sh
```

4. Copy the `.env-sample` file to `.env` and replace the environment values with your own.

```sh
cp .env-sample .env
```

5. Run one of the shell scripts

```sh
./wp-create.sh
```

6. Profit?

Note: If your development directory (`DEV_DIR`) is within your home folder (which is the default), you will need to add executable permissions to your home folder:

```sh
chmod o+x $HOME
```

### Optional: Add the script as a bash alias

```sh
echo "alias wp-create='$HOME/dev/wp-create/wp-create.sh'" >> ~/.bash_aliases
```

Now you can call the script from anywhere within Ubuntu:

```sh
wp-create "My Great WordPress Site"
```

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.