How to optimize WordPress with Ubuntu VPS

Ubuntu Virtual Private Server optimized for WordPress

Category: EC2

EC2

  • Adding SSL certificate from CloudFlare

    CloudFlare.com provides a free valid SSL certificate.
    After installing this certificate, can you change the CloudFlare SSL settings from “Full” to “Strict.”

    Create a .pem file for the server certificate

    nano MyDomain-com-server.pem

    Paste and copy the server certificate from CloudFlare.com into this file. Then save the file.
    Move the server certificate to the correct folder:

    sudo mv MyDomain-com-server.pem /etc/ssl/certs/MyDomain-com-server.pem

    Then create the private certificate:

    nano MyDomain-com-private.pem

    Move the server certificate to the right folder:

    sudo mv MyDomain-com-private.pem /etc/ssl/private/MyDomain-com-private.pem

    Modify the Apache config file to use the certificates

    The config files are located at:

    cd /etc/apache2/sites-available

    Open the config file:

    sudo nano 01-MyDomain1-com.conf

    Find the lines:

    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
    

    Change the lines to reflect your newly created credentials. It would the look like this:

    SSLCertificateFile /etc/ssl/certs/MyDomain-com-server.pem 
    SSLCertificateKeyFile /etc/ssl/private/MyDomain-com-private.pem

     

    Rights and ownership on certificates

    Directory permissions should be 710 for the private keys.

    File permissions on the certificates 644 for the private keys.

    It is recommended that the directory and files should be owned by root

    Change the owner and group:

    sudo chown root:ssl-cert -R /etc/ssl/private

    Change the permissions on files:

    sudo find /etc/ssl/private -type f -exec chmod 644 {} \;

    Change the permissions on folders:

    sudo find /etc/ssl/private -type d -exec chmod 710 {} \;

     

    All the files in the cert folders should be owned by root

    sudo chown root:root -R /etc/ssl/certs/

    Short version of adding the certs

    Private cert
    sudo mv MyDomain-com-private.pem /etc/ssl/MyDomain-com-private.pem
    
    sudo chmod 644 /etc/ssl/private/MyDomain-com-private.pem
    
    sudo chown root:ssl-cert /etc/ssl/private/MyDomain-com-private.pem
    Server cert
    sudo mv MyDomain-com-server.pem /etc/ssl/certs/MyDomain-com-server.pem
    
    sudo chown root:root -R /etc/ssl/certs/MyDomain-com-server.pem
    
    sudo chmod 644 /etc/ssl/certs/MyDomain-com-server.pem

     

    Check the Apache config file

    sudo apachectl configtest

    Restart Apache web server

    sudo service apache2 restart

    Check the different Apache error logs

    It is alway a good habit to have a look at the error logs.

    cd /var/log/apache2/
    sudo nano error.log
    sudo nano 01-mydomain-com-error.log
  • Configure SSH for administration of remote VPS servers with WordShell

    You can manage many EC2’s or VPS servers with WordShell. You need to configure the SSH config file to log in to remote VPS servers with SSH and SFTP. This is a requirement for WordShell.Here is how:

    Configure SSH

    Upload all your .pem files that contain your SSH keys with FTP to:

    /home/ubuntu/

    Then move them to the SSH config folder

    mv *.pem /home/ubuntu/.ssh/

    You receive  an error message unless you change the permission to 600

    chmod 600 /home/ubuntu/.ssh/*.pem

    Now it is time to create the configuration file.

    nano /home/ubuntu/.ssh/config

    Change the IP addresses and domain name to suit your settings. Replace “MyServer1.com” and 888.888.888.888 with your domain and your IP. In this config file, there are 3 EC2 servers.

     

    Host MyServer1.com
    Hostname 888.888.888.888
    User ubuntu
    IdentityFile /home/ubuntu/.ssh/MyServer1.com.pem
    
    Host MyServer2.com
    Hostname 888.888.888.888
    User ubuntu
    IdentityFile /home/ubuntu/.ssh/MyServer2.com.pem
    
    Host MyServer3.com
    Hostname 888.888.888.888
    User ubuntu
    IdentityFile /home/ubuntu/.ssh/MyServer3.com.pem
    

    Save the config file.
    Protect the data:

    chmod 600 /home/ubuntu/.ssh/config

    Verify that you can log onto the servers
    Test that you can connect to the 3 EC2 servers by typing

    ssh MyServer1.com
    ssh MyServer2.com
    ssh MyServer3.com

    Set the correct permissions for the Apache and the user ubuntu 

    To avoid error messages make sure that the files and folders on the remote VPS servers have the right access rights. The user that logs on to the remote VPS server must have permissions to manage the files and folders that make WordPress.

    This post has all the details:

    [themedy_button url=”https://www.wpjeos.no/give-user-ubuntu-server-apache-rights-run/” icon=”” font_awesome_att=”” label=”Set the right permissions for Apache and the ubuntu user” colour=”blue” colour_custom=”” size=”large” edge=”rounded” target=”_self”]

  • How to install WordPress through command-line with WP-CLI

    Setting up WordPress using the command line is fast.
    You can even use a script to automate the process.

    Home » EC2 » Page 6

    Prerequisites and preparations

    Before you begin. Create a MySQL database in Amazon RDS. You also need a working website. Then start creating a list with the following information:

    • Name of your Amazon RDS MySQL database server
    • Database name
    • Database username
    • Database user password
    • The WordPress admin username you intend to use
    • The WordPress admin password you intend to use
    • The WordPress admin e-mail address you plan to use

    If some points are unclear these how-tos should point you in the right direction:
    How to install WP-CLI

    How to set up a virtual host

    How to activate .htaccess

    How to create a MySQL database in Amazon RDS

    Verify the root folder

    Your root folder is beneath

    /var/www/

    In this tutorial are we going to install WordPress in the folder

    /var/www/MyDomain1.com

    Start with going to the root folder:

    cd /var/www/MyDomain1.com

    Verify that there are no files in this folder

    sudo ls

    If here is index.html or another start page file there you should delete this file to avoid problems later on.

    sudo -u www-data rm index.html

    Change ownership and delegate permissions

    The Apache web server needs permission to run WordPress.

    sudo chown -R www-data /var/www/MyDomain1.com

    Download the installations files

    Downloading all the WordPress files is done in a matter of seconds by issuing this command:

    wp core download
    
    

    Create wp-config.php, the main configuration file

    Sometimes WP-CLI is unable  to connect to Amazon RDS MySQL database. Then you can skip the database test by adding the following statement:

     --skip-check

    Creating the configuration file in WordPress. Fill in the values from the list you created previously:

    wp core config --dbname=wordpress --dbuser=user --dbpass=password --dbhost=dcfgvr.1bagob.0002.euw1.cache.amazonaws.com --dbprefix=wp_ --skip-check

    Install WordPress

    wp core install --url="https://www.example.com" --title="Blog Title" --admin_user="adminuser" --admin_password="password" --admin_email="email@domain.com"

    Check the server error logs

    It’s a good practice to examine the error logs. They are here:

    cd /var/log/apache2/
    sudo nano error.log
    sudo nano 01-mydomain1-com-error.log

    Set correct permissions so Apache can access the files and folders

    Basic permission:

    sudo chown -R www-data /var/www/MyDomain1.com

    More thorogh guide on setting permissions is available her:

     
     
     
  • How to run WordPress on your OS X Mac and Windows

    Running WordPress on your Mac OS X or Windows computer has many advantages. There are two different concepts for doing this:

    • Running a virtual private server – VPS – on your Mac or Windows
    • Running Apache, PHP and MySQL on your Mac or Windows

    Both solutions have its advantages and downsides.

    Running PHP, MySQL and Apache on your PC is the easiest and best solution if you are aiming at creating websites. Running a VPS would be the choice if you are developing or programming.

    Do you prefer using WP-CLI and WordShell? Then you should choose a VPS based solution.

    In TBT we currently use MAMP Pro. In the future, we might also be using VMware Fusion for individual projects. In general, we find MAMP Pro faster because we can use the file manager “Finder” in Mac when moving files and folders between the local disk and WordPress.

    Solutions that runs PHP, MySQL, and Apache on your computers OS:

    • Manually installing Apache, MySQL, and PHP
    • MAMP
    • AMPPS Stack
    • VirtualHostX version 6

    Solutions for running VPS on your computer:

    • VirtualHostX version 7
    • Primary Vagrant
    • Varying-Vagrant-Vagrants
    • Oracle VM VirtualBox
    • VMware Fusion for MAC
    • VMware Workstation for Windows
    • Parallels Desktop for Mac
    • Microsoft Hyper-V

    Manually installing Apache, MySQL, and PHP on Mac and Windows

    On both Mac and Windows, you can download Apache, MySQL and PHP and then configure your computer. Mac OS X comes pre-installed natively with Apache and PHP so you only need to install MySQL.

    MAMP

    Apache, MySQL, and PHP for OS X and Windows

    Find the details here:

    https://www.mamp.info/en/

    AMPPS Stack

    AMPPS is a software stack containing Apache, Mysql, PHP. Consist of the Softaculous auto-installer. AMPPS should be used on Desktops and office servers.

    http://www.ampps.com

    VirtualHostX

    VirtualHostX 7.0 lets you host and share as many websites as you like with your Mac. This is a suitable solution when you are creating more than one website simultaneously. With VirtualHostX, you can manage many Apache sites at the same time with just a few clicks.

    https://clickontyler.com/virtualhostx/

    Primary Vagrant

    This is an Apache-based Vagrant configuration for WordPress development.

    Primary Vagrant is a tool for WorPress plugin, theme, and core development. You can also use PV as a general PHP development environment. Primary Vagrant is a replacement for MAMP and XAMPP.

    Varying Vagrant Vagrants usesNGINX webserver. Primary Vagrant uses Apache. PV uses Puppet instead of Bash. PV uses VVV and Puppet as a base and is more focused on WordPress development.

    https://github.com/ChrisWiegman/Primary-Vagrant

    Varying-Vagrant-Vagrants/VVV

    Vagrant is a VPS server that runs on top of VirtualBox of VMWare on your local machine.

     

    https://varyingvagrantvagrants.org

    Oracle VM VirtualBox

    https://www.virtualbox.org

    VMware Fusion

    http://www.vmware.com/products/fusion

    VMware Workstation for Windows

    https://www.vmware.com/products/workstation

    Parallels Desktop for Mac

    http://www.parallels.com/eu/products/business/

     

    Microsoft Hyper-V

    Microsoft Hyper-V is a part of the Microsoft Windows operating system. You can activate Hyper-V in the Windows 10 Control panel. Choose between creating your own or download prebuilt VPS images with WordPres installed and ready to run.

  • How to reboot Ubuntu once a month

    It could be a goo idea to reboot the Ubuntu VPS server once a month.
    Start the cron editor with the command

    sudo crontab -e

    Then add the cron command to restart the server

    15 15 15 * * root /sbin/shutdown -r now >/dev/null 2>&1

    To verify that this works use this command to get the time the server has been up.

    sudo uptime -p

    This command will tell you when the server was booted

    sudo uptime -s
  • How to replace WordPress cron with real cron jobs

    When it is working, don’t fix it!

    If you do not use WPcron, just skip this post.
    If WPcron works for you, don’t change to cron.

    Only use cron if your WPcron does not serve you as needed.

    Step one. Disable WPcron

    Open the WordPress config file:

    sudo nano wp-config.php

    To disable the unreliable WordPress cron function add the following statement

    define('DISABLE_WP_CRON', true);

     

    Step 2. Activate cron in Ubuntu

    Open cron

    sudo crontab -e

    Then insert the following statement for each WordPress site on your VPS. This example is what you could enter for three WordPress sites on your VPS:

    0 */1 * * * wget -q -O - http://site1.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
    1 */1 * * * wget -q -O - http://site2.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
    1 */1 * * * wget -q -O - http://site3.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

    This command is set to run every hour. You can use whatever time you need. Cron requires resources and puts a load on your server. More often than every 15 minutes could be bad. Once every hour should be okay for most scheduling in WordPress.
    If you have many WordPress websites, you should set them to run with 1 minute, 5 minutes or 10 minutes spacing to avoid slowing down the server.