Add WordPress site

WordPress setup is relatively straightforward but on my server there are some things that need to be done to keep it tidy.

  • Set up virtual host
~ $ sudo /etc/apache2/addvhost.sh
~ $ sudo systemctl reload apache

If domain is ready then create a certificate.

  • Set up a database
~ $ domain=...
~ $ vhost=${domain//./_}
~ $ sqlvhost=${vhost//_/\\_}
~ $ db_pass=$(pwgen 32 1)
~ $ mysql -uroot -p << SQL
CREATE DATABASE \`$vhost\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER '$vhost'@'localhost' IDENTIFIED BY '$db_pass';
GRANT USAGE ON *.* TO '$vhost'@'localhost';
GRANT ALL PRIVILEGES ON \`$sqlvhost\`.* TO '$vhost'@'localhost' WITH GRANT OPTION;
SQL
  • Get WP and create configuration
~ $ cd /var/www/$vhost
/var/www/VHOST $ wget https://wordpress.org/latest.tar.gz
/var/www/VHOST $ tar xzf latest.tar.gz -C web --strip-components=1
/var/www/VHOST $ rm latest.tar.gz
/var/www/VHOST $ cp -p ../it_fejese_com/web/.htaccess web/
/var/www/VHOST $ cd web
/var/www/VHOST $ chmod g+w wp-content
/var/www/VHOST/web $ sed -r \
    -e "s/(database_name_here|username_here)/$vhost/" \
    -e "s/password_here/$db_pass/" \
    -e "s/\(\s*'DB_CHARSET'\s*, '[^']*'\s*\)/('DB_CHARSET', 'utf8mb4')/" \
    -e "s/\(\s*'DB_COLLATE'\s*, '[^']*'\s*\)/('DB_COLLATE', 'utf8mb4_unicode_ci')/" \
    wp-config-sample.php > wp-config.php
/var/www/VHOST/web $ while grep -q 'put your unique phrase here' wp-config.php; do sed -i "0,/put your unique phrase here/s/put your unique phrase here/$(pwgen 64 1)/" wp-config.php; done
  • Finish WP setup

Go to $domain and finish WP installation.

  • Back up
/var/www/VHOST/web $ cd ..
/var/www/VHOST $ cp -p ../it_fejese_com/backup_db.sh ./
/var/www/VHOST $ cp -p ../it_fejese_com/.gitignore ./
/var/www/VHOST $ ./backup_db.sh
/var/www/VHOST $ git init
/var/www/VHOST $ git add :/
/var/www/VHOST $ git ci -m 'Initial commit'
/var/www/VHOST $ git init --bare /var/git/$USER/$vhost.git
/var/www/VHOST $ git remote add origin /var/git/$USER/$vhost.git/
/var/www/VHOST $ git push origin

That’s mostly it.

Change main domain of wordpress site

Ideally you should be able to do this in the WP admin however you might have trouble logging in even as the site is trying to force the set domain. So in this case it might be easier to fix the db:

UPDATE wp_posts SET
 guid = REPLACE(guid, 'blog.fejese.com', 'it.fejese.com'),
 post_content = REPLACE(post_content, 'blog.fejese.com', 'it.fejese.com')
;
UPDATE wp_options SET
 option_value = REPLACE(option_value, 'blog.fejese.com', 'it.fejese.com')
;

Note thought that some plugins – the ones with integrations to external services – might need to be repaired but normally that should be no problem.