DEV Community

loading...
Cover image for Create a portfolio site QUICK! Host it free on AWS and get hired- The complete 2021 guide

Create a portfolio site QUICK! Host it free on AWS and get hired- The complete 2021 guide

Drew K
Find more articles at drewlearns.com
・15 min read

Are you a developer?

Don't have a resume website yet?

Is money the main reason you haven't got one?

If you answered yes to the questions above, then this guide is for you and today those excuses are gone! I've done all the hard work for you, just follow this guide and start applying for developer jobs today!

If you just want to take a crack at cheaper self hosted options for your WordPress site then this guide is also for you.

Hopefully this guide will be verbose enough to keep you on track and teach you a thing or two. Follow along! Every section is concluded with a video recap but to see those you'll have to look at the original post at https://drewlearns.com/move-your-wordpress-website-to-aws-from-another-host-for-free/

I created this for myself out of frustration that I couldn't find a single document that accurately walked me from nothing to a working WordPress site on AWS with an SSL and without paying for an AMI on free tier AWS.
If you make it all the way through this guide and would like to know how to add an autoscaling group, place your setup on your own AMI, host your images on an S3, leverage CloudFront's CDN and much more - Let me know in the comments section!

Table of contents:

  1. Creating your resume website (Optional if you are just migrating a site)
  2. Creating Your First EC2 Instance
  3. PHP installation
  4. Install MariaDB (MySQL) and Apache
  5. Install a certificate with auto renewal
  6. Secure your Database
  7. Install WordPress
  8. Set up FTP/SFTP for file transfer
  9. Migrate your site from local to your AWS account

Creating your resume website (Optional if you are just migrating a site)

If you'd like to create a site from scratch, this is a great place to start:

You can build a Resume using the Starter files you created from my video tutorial or my written tutorial. All totally free of charge!

You can access the starter files from that tutorial here: https://share.drewlearns.com/4gujpBpl/ (if that link doesn't work try this one).


Creatinging Your First EC2 Instance

  1. Create Amazon account - it will charge you $1 but that charge will fall off.

  2. Navigate to EC2 service

  3. Select your AZ
    https://share.drewlearns.com/6quQ02GL

  4. Create a new instance
    https://share.drewlearns.com/ApuRNAdn

  5. Select the Amazon Linux 2 AMI (HVM), SSD Volume type.
    https://share.drewlearns.com/E0u9lzpZ

  6. Click "Review and Launch"

  7. Click "Launch".

  8. Click the first drop down and select "Create a new key pair" then name the keypair "wp-keypair".

  9. A file will download called "wp-keypair.pem".

  10. In terminal move that file to your root directory. mv ~/Downloads/wp-key-pair.pem ~/ && cd ~/

  11. We have to add permissions to the file to use it so run chmod 400 wp-key-pair.pem then click "Launch Instance"

  12. You should see your EC2 instance dashboard and an Instance ID with an Instance state saying "Pending" followed by "Running".

  13. Click the Instance ID link in blue and an instance summary will appear. Select the "Security" tab https://share.drewlearns.com/04uJrqdl.

    You can also find your public IP address in this screen. From that IP, you can point your domain to it, I'd strongly recommend doing that to take the full benefit of this guide. If you chose not to, you'll need to use your AWS public domain or IP address in the steps that follow instead of example.com.

  14. Add your IP for inbound SSH connections then save rules ( I skipped this step in the video recap at the bottom of this section but don't worry, we come back to it)https://share.drewlearns.com/jkueyQpQ

  15. Return to the EC2 dashboard and select the Instance you just edited. Click it's check box then Actions > Connect.

  16. Click the "SSH Client" tab. There is an example snippet, copy paste it into your terminal and press return. Type "yes" and press return again to acknowledge connecting. You are now SSH'd into your Instance.

https://share.drewlearns.com/4gu15JQm
  1. Check for updates:

    sudo yum check-update && sudo yum update

PHP installation

PHP is our backend server language for WordPress. Let's get it installed.

  1. Let's confirm amazon-linux-extras, if you don't see a directory output then you'll have to install using sudo yum install -y amazon-linux-extras - I think this comes standard now, though I'm not positive.

  2. Let's make sure that the PHP version we would like to use is available. Run: sudo amazon-linux-extras | grep php.

I personally prefer having my site as secure and performant as possible so I like the latest version where possible. I recognize some plugins/themes don't support latest versions of plugins/themes, in those cases I'd dump the plugin/theme over instead of lay blame on the PHP version as being the issue. I don't want plugins installed that don't operate on currently supported PHP versions myself.

  1. Install PHP 7.4 using the following command:

    sudo amazon-linux-extras enable php7.4 && yum clean metadata && sudo yum install php php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap} -y
  2. Verify PHP is installed by running php -v

    You may also be curious what PHP modules are installed, you can find this using php -m

  3. Make sure PHP is installed, and running and no longer ephemeral by running the following:

    sudo chkconfig php-fpm on && sudo service php-fpm start && sudo service php-fpm status

Install MariaDB (MySQL) & Apache Webserver

  1. Install it, but make sure to agree to installation.

    sudo yum install -y httpd mariadb-server
  2. Enable the database service:

    sudo systemctl enable --now mariadb
  3. Start and create a symlink for your Apache Web Server so that it starts on reboot automatically.

    sudo systemctl start httpd && sudo systemctl enable httpd && sudo systemctl is-enabled httpd
  4. Verify it's running by going to your EC2 Dashboard and clicking the instance ID link and then going to the IP address in your browser, it should look like this:

    https://p289.p2.n0.cdn.getcloudapp.com/items/2NuE28jQ/c22492c4-aa9e-4e3b-bc9b-663567bf5b1c.jpg?v=61c5b56ef82950cdf6aa6c48fc5eeb2a

  5. Update your ec2-user's permissions to have access to manipulate the apache directory

    sudo usermod -a -G apache ec2-user && exit
  6. Use your "Up" key to reuse your ssh connection command.

    If you use groups command, you will see "apache" listed for your user.

  7. Update the ownership of your www directory:

    sudo chown -R ec2-user:apache /var/www && sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \; && find /var/www -type f -exec sudo chmod 0664 {} \;

Install a certificate with auto renewal:

  1. Run the following command to install EPEL (Extra Packages for Enterprise Linux)

    sudo wget -r --no-parent -A 'epel-release-*.rpm' https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/ && sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm && sudo yum-config-manager --enable epel* && sudo yum repolist all
  2. Update your apache config file to show your new domain:

    sudo nano /etc/httpd/conf/httpd.conf
  3. Locate the line that reads "Listen 80" and just after it paste the following:

     <VirtualHost *:80> DocumentRoot "/var/www/html" ServerName "example.com" ServerAlias "www.example.com" </VirtualHost>
  4. Locate the line that starts with <Directory "/var/www/html" and add these 2 lines inside the Directory brackets & ddit the line that says AllowOveride None to say AllowOveride All
  1. Press "Command" + "X", then "Y", then "Return".

  2. Restart Apache:

    sudo systemctl restart httpd

    If you made any mistakes in the httpd.conf file, you will get errors about httpd.service failing. You can get more details about the error by running systemctl status httpd.service

  3. Add your certbot and run it!

    sudo yum install -y certbot python2-certbot-apache && sudo certbot
  4. Enter your email address.

  5. Press return, then "Y" and return again

  6. Again, press return, then "Y" and return again

  7. Type 1, 2. (assuming you are using an alias domain like www you provided in httpd.conf file)

  8. Automate the certbot!

    sudo nano /etc/crontab
    • Add a cron job with root permissions to run certbot renew --no-self-upgrade twice daily at 01:30 & 13:30:

      30      1,13    *       *       *       root    certbot renew --no-self-upgrade
  9. Restart your cron daemon:

    sudo systemctl restart crond

Your site is now secure!


Secure your Database

  1. Secure and test your database connection:

    sudo mysql_secure_installation
  2. When prompted for a password press Return

  3. Change/Set the root password? [Y/n]

    Press "y" and "return".

    New password:  Re-enter new password: 
  4. Remove anonymous users? [Y/n]

    Press "y" and "return".

  5. By default, a MariaDB installation has an anonymous user, allowing anyone
    to log into MariaDB without having to have a user account created for
    them. This is intended only for testing, and to make the installation
    go a bit smoother. You should remove them before moving into a
    production environment.
    Remove anonymous users? [Y/n]

    Press "y" and "return".

  6. Normally, root should only be allowed to connect from 'localhost'. This
    ensures that someone cannot guess at the root password from the network.
    Disallow root login remotely? [Y/n]

    Press "y" and "return".

  7. Disallow root login remotely? [Y/n]

    Press "y" and "return".

  8. Remove test database and access to it? [Y/n] y

    Press "y" and "return".

  9. Reload privilege tables now? [Y/n]

    Press "y" and "return".

  10. Start your MariaDB:

     ​sudo systemctl enable --now mariadb
  11. Make sure you can log in by typing:

    mysql -u root -p
  12. Create a new user, obviously change "wordpress-user" and "your_strong_password" to suite

    CREATE USER 'wordpress-user'@'localhost' IDENTIFIED BY 'your_strong_password';

    Query OK, 0 rows affected (0.001 sec)

  13. Create your database and name it something meaningful:

    CREATE DATABASE `WordPress-db`;

    💡 Don't forget to close that command out with a ;

  14. Provide that database full privileges for your user. Be sure to update the wordpress-db name and the wordpress-user name to match what you just called it in the previous ste.

    GRANT ALL PRIVILEGES ON `WordPress-db`.* TO "wordpress-user"@"localhost";
  15. Flush your privledges to pick up all your new configurations:

    FLUSH PRIVILEGES;
  16. Exit your mysql prompt with exit.


Install WordPress

  1. Let's install a base WordPress image onto our WordPress

The following command downloads, cleans up and creates a wp-config.php file.

            cd /var/www/html/ && sudo wget https://wordpress.org/latest.tar.gz && sudo tar -xzf latest.tar.gz && sudo rm latest.tar.gz && sudo cp -r /var/www/html/wordpress/* /var/www/html/ && cp wp-config-sample.php wp-config.php && sudo rm wp-config-sample.php && nano wp-config.php
  1. Use your d-pad to navigate and edit the following fields with the information you created in the section above.

    Remember this is not your maria-db root password for your DB_PASSWORD field! https://share.drewlearns.com/ApuRNRYX
  2. Edit your Salts:

    https://share.drewlearns.com/OAugRgjR
  3. Change your database table prefix to something more obscure than wp_ and make it something like wp_123456_. Bear in mind, it has to end with an underscore.

  4. Press "Control" + "X" to close the editor and then press "Y" then "Return" to save.

  5. We have to make sure the server has complete access to these files so we need to edit their permissions:

    🚨 If you miss this step or it fails, you'll get a weird ftp credential popup when trying to install plugins/themes.

    sudo chown -R apache:apache /var/www/html
  6. Navigate in a browser to your domain. You should be prompted with a WordPress Welcome screen. Fill out those details.


Set up FTP/SFTP for file transfer

Currently your site is lacking connectivity to the EC2's file system. We need to add FTP or SFTP. This will allow us to install plugins and more. You may be curious or interested in the ability to upload your file system to S3 and since I'm currently over my PUTS limit this month, I'll be holding out on a tutorial about this for a little while. If you are really wanting to do it though, here is a good guide I found: https://docs.aws.amazon.com/codedeploy/latest/userguide/tutorials-wordpress-upload-application.html

  1. Add an FTP plugin to your server:

    sudo yum install vsftpd -y
  2. Restart your FTP server:

    sudo systemctl restart vsftpd
  3. Add a new SFTP user to your site using the following two commands, bear in mind you can choose any username you'd like, these are just placeholders.

    sudo adduser awsftpuser

    &

    sudo passwd awsftpuser

    You'll then be prompted for a password twice.

  4. Grant that user permissions to update the home directory:

    sudo usermod -d /etc/httpd/ awsftpuser && sudo usermod -a -G root awsftpuser && sudo systemctl restart vsftpd && sudo chkconfig --level 345 vsftpd on
  5. Edit your vsftpd conf file by typing:

    sudo nano /etc/vsftpd/vsftpd.conf

    Find the line that reads:

    # Allow anonymous FTP? (Beware - allowed by default if $  anonymous_enable=YES

    to

    # Allow anonymous FTP? (Beware - allowed by default if $  anonymous_enable=NO

    Uncomment out the line that reads:
    chroot_local_user=YES

    At the bottom of the file add:

    pasv_enable=YES pasv_min_port=1024 pasv_max_port=1048 pasv_address=

    🚨Don't forget to change the part of that line. You can grab this from your EC2 dashboard by clicking on the instance ID.

  6. Press "Control" + "X" to close the editor and then press "Y" then "Return" to save.

  7. Restart the FTP pod one more time:

    sudo systemctl restart vsftpd


Migrate your site to the AWS server!


Your WordPress Site is now up and running. If you already have a site on another host, you can migrate it.

The Easiest way to migrate a site is to install a plugin called "Migrate Guru: Migrate & Clone WordPress Free" on both sites and let it do the magic.

If you'd like a more indepth discussion on how to manually migrate your site's files and database from one host to another Let me know in the comments section below.

Discussion (0)