Working With WordPress



  Have you ever wanted to setup a WordPress site, but don't want to use WordPress.com or an expensive paid host ? Well you have come to the right place. In this article I will go over how to setup a WordPress site on Google Cloud.

Step one, setup Google Cloud account


 Before we go through the steps of getting Google Cloud going you need to have access to a Google account such as gmail.Once you have that set and ready to login goto Google Compute. Once there click Go To Console and click Create Instance. While setting it up choose cpu type micro unless you think you will get a lot of traffic from the start. This can always be changed later, but the higher you go the more cost involved. With the micro cpu it will not cost you a whole lot to maintain, the only real cost is a static IP which you will need to use a domain name. The next step will be to choose an OS or choose to upload a premade VM. For the OS I recommend Ubuntu 18.04 or higher if you choose to go unstable. These are minimal installs and you will have to update and install the tools you need as we go, or you can choose to add back in the core utilities once logged into the console. I'm including some more resources at the end of the article for references if you did not get anything I explained in this section.

Lets get gcloud on your local machine


 This part of the process can be skipped if you wish to use the browser console. However I recommend it for ease of access and also helps set yourself up for future projects to deploy off your local machine.
  Most developers use one of the two recommended OS paths to do this is Red Hat / Centos and Debian / Ubuntu, windows and macOS is also available if you live that life. There is also just a Linux general install path if you use other flavors of Linux or want an shell script. My personal flavor is Fedora which can use any of the Red Hat RPM packages. So the install method I choose was with yum and you have to add the repo date before using it. Use the following command to launch and editor:

sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM

Once you have done this it will launch a prompt with ">".

Copy and paste the following into that section and press enter:

[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

If you have done this correctly you should find yourself back at the bash console. Now you can use the following command to fetch the sdk.

yum install google-cloud-sdk

You should note that this installs the minimum files required to use Google Cloud SDK and if you want app engine support or other file support you will have to install them manually. We will not be covering this part here as it is not needed for this project. If you wish to use these tools visit Google Cloud Developers page for more details. Now that we have gcloud setup for use with our local machine we can setup a webserver.

Swap File setup

 Since we are using only 512mb of ram on the this compute engine it is recommended that you install a 1gb swap file to help with performance. Here are the steps to do that. First you need to allocate some space:

sudo fallocate -l 1G /swapfile

Now you can use dd to move the allocated space. Be Careful and only type what I have provided unless your positive you know what you're doing. DD can damage your file system:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Once that is done, it may take a few minutes so please wait until it's done. After this we can change access to the swapfile so that only root can access it, we will use chmod to do so.

sudo chmod 600 /swapfile

Root access is now setup and we can use mkswap to create the file need for swap and fstab.

sudo mkswap /swapfile

You have now created a file to link with swap use the following command to turn it on:

 sudo swapon /swapfile

Be sure to add this to your fstab config to load it in the even you need to restart or your compute engine. below is the command and information to paste into the fstab config.

#command to open fstab.conf

sudo nano /etc/fstab

#Paste this in the fstab file and save it.

/swapfile swap swap defaults 0 0

Web Server (Lamp)


 There is a few methods you can install a LAMP server with a single command or you can choose to install each component individual if you need specific versions or a container setup. The version I choose to do was with apt-get which handles all the dependencies needed to make the basic stack. After this you will have to add a few additional items that we will go over. Use the following command to get started:

sudo apt-get install lamp-server^

If your install does not go well do not remove lamp-server with the caret "^" because it will remove all libraries attached to it and break your system. Also don't forget it when installing as it will not include everything needed. I mentioned a few extra items that need installed to finish out the lamp stack setup. Use the bit below to install these.

sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc

Apache config

 In this second we will setup the apache config so that we can point our web address to the proper ip and apache conf files. First let's get to the proper folder with the following commands.

#Use this to get to the location of the files to be edited

cd /etc/apache2/sites-available

Step two will be coping the default to a new file. This is optional but recommend to organize things if you decide to add additional sites to the server.

sudo cp  000-default.conf yoursitesname.conf

#replace yoursitesname with your web address

Once you have done this you can now edit the new config file

sudo nano yoursitename.conf

#Once inside the file make changes as such below

<Directory /var/www/example.com>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName yoursitename.com
ServerAlias www.yoursitename.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/yoursitename.com
</VirtualHost>

The last step for the apache setup is to enable the new config.

#This turns off the default conf 

sudo a2dissite 000-default.conf
#This enables the new one

sudo a2ensite yoursitename.conf

#You can now type

sudo systemctl reload apache2

If all went well you now have your sites address setup. Make sure you create the directory for the new site if you have not done so already.

sudo  mkdir -p /var/www/yoursite.com

Mysql WordPress Database

 Wordpress has to have a sql database to store post and config information so we will go ahead and create a database. This has to be done as root.

sudo  mysql -u root
> CREATE DATABASE wordpress;
> GRANT ALL ON wordpress.* TO 'wordpressuser' IDENTIFIED BY 'Secure1234!';
> quit

#This last step is need to harden the database so that there is no way to access it without root.

sudo mysql_secure_installation

Php.ini tweaks

 You can edit the "/etc/php/7.2/apache2/php.ini" file if you like to help tweak performance. Open Nano and make the changes below. You can search for the settings with crl + q.

#Edit /etc/php/7.2/apache2/php.ini

max_input_time = 30
upload_max_filesize = 20M
post_max_size = 21M

Install WordPress on your Google cloud

 Visit WordPress.org if you need more help with setup. Use the commands below to install wordpress, and be sure to do this in /var/www/yoursite.com folder so you don't have to move your files around. Lastly you can install a script to help tune your config after its ran a few days. If you made it this far you should now have your own WordPress Site.

#To install WordPress

wget https://wordpress.org/latest.tar.gz
 && tar -xzvf latest.tar.gz

#To run the script for tweaking

wget 
https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl

Sources I used

MySql Chris Titus Chris Titus Website

This Weeks Progress #2


  Another week has gone by and I'm here to tell you a few things I learned.
There's not much to report but I did work on a Udacity course and read some JavaScript articles on Dev.to.

 So one of the articles I read was something that was not new to JavaScript but it was new to me. This feature known as .some( ) is used to check against an array to learn about data in an array. The example used which is at the bottom of this article is what I will be showing you. So lets say you want to know if a price of a certain item in the array goes above 20 dollars. Here is a bit of code to highlight my example.

const prices = [5, 8, 11, 10, 25]; //an array
const aboveTwenty = prices.some((price) => price > 20); //check the 
array for prices over 20

console.log(aboveTwenty); // true 


  Now for some insight into the Udacaity I am working through for JavaScript. To save us both time I will just go over one bit that stumped me a bit due to over thinking and trying to make it more complex (It's a normal trend I am trying to fix). The section was on conditionals such as if, else and else if statements. For the exercise I was suppose to write a block of code that used if, else and else if to find rather a number was odd using the modulo operation. While this should of been simple from the start I miss read an instruction which is where the trouble started. After messing around with it awhile, I decided to start over and reread the requirements to pass the quiz. And wouldn't you know, it just clicked and I got it done. Here is the bit of code I came up with.

var number = 5;

if (number%2 === 1) {
    console.log('odd');
}
else if(number%2 !== 1){
    console.log('even');
}
 else {
    console.log(number);
 }


For those wondering what the modulo function does, it finds the remainder after division. In this block of code I use strict comparing to get the logic to work. The === makes so it states that is has to be equal to be truthy or else it's falsy. That makes !== mean it has to absolutely be not equal to be truthy otherwise it's falsy. Here is a small table to help clarify it more.

1 == true     => true
true == true  => true
1 === true    => false
true === true => true


 That concludes my lessons this week and I hope you enjoy my coding information. If anything seems wrong or you would like to discuss this more hit me up on twitter @officalajsmith.

100 Days Of Code

  • What I wish to accomplish on this challenge

  • I wish to get more consistent work flow
  • Get better at coding and problem solving
  • Show work for what I know to back it up
  • Learn git cli and get better at setting up read me docs.

Here are the rules I wish to hold myself accountable

  • I will code no less than 5 days a week
  • Two days A week I will rest and spend time away from the computer
  • Saturday and Sunday will be the off days unless I need to swap days
  • Once a week I will write an update about my week of coding

Here is a link to the #100DaysOfCode if you wish to read more about the challenge. Everyone's coding journey is unique so find your goals and outline them. This way you can move forward and not feel bad about what others are doing.

This weeks progress

Here is my first post and a week in review progress. This was brought about because of a group known as Let's Code. A few peers and myself decided to work on communication while passing on some information we learned.