How to Install WordPress with Apache on Google Cloud with Let’s Encrypt SSL. In this article you are going to learn how to setup and install WordPress on Google Cloud with Ubuntu 18.04 LTS, Apache, PHP 7.3 and Cloud SQL.
This setup is tested on Google Cloud Platform, it will also work fine on other cloud serveices and on any VPS or Dedicated servers running Ubuntu.
Prerequisites
- Your Compute Engine Instance running.
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- For installing Apache, see how to install Apache in Compute Engine Instance.
- Install PHP 7.3 for Apache on Google Cloud Platform.
- Set up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
- Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.
Setup your website
Once you have installed PHP 7.3 and Apache you can proceed to setup your directories.
Your website will be located in the home directory and have the following structure.
Replace yourdomainname.com
with your original domain name.
home
-- yourdomainname.com
---- logs
---- public
The public
directory is your website’s root directory and logs
directory for your error logs
Now we create these directories and set correct permissions
You need to SSH into your VM Instance and run these commands
mkdir -p yourdomainname.com/logs
yourdomainname.com/public
sudo chmod -R 755 yourdomainname.com
Now create a new V host configuration for your website in the sites-available
directory.
sudo nano /etc/apache2/sites-available/yourdomainname.com
Copy and paste the following configuration, ensure that you change the server_name, error_log and root directives to match your domain name. Hit CTRL+X
followed by Y
to save the changes.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.yourdomainname.com
ServerAlias yourdomainname.com
DocumentRoot /home/username/yourdomainname.com/public
<Directory /home/username/yourdomainname.com/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourdomainname.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomainname.com_access.log combined </VirtualHost>
To enable this newly created website configuration, symlink the file that you just created into the sites-enabled
directory.
sudo a2ensite yourdomainname.com
Check your configuration and restart Apache for the changes to take effect
sudo systemctl restart apache2
Install SSL certificate
HTTPS is a protocol for secure communication between a server (instance) and a client (web browser). Due to the introduction of Let’s Encrypt, which provides free SSL certificates, HTTPS are adopted by everyone and also provides trust to your audiences.
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-apache
Now we have installed Cert bot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificates.
sudo certbot --apache -m [email protected] -d yourdomainname.com -d www.yourdomainname.com
Select all the domains you need to receive the certificate and hit Enter
The Cert bot client will automatically generate the new certificate for your domain.
Now you will be asked to create a redirection to https
Select the appropriate option and hit Enter
Renewing SSL Certificate
Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. Now you set up a cronjob to check for the certificate which is due to expire in next 30 days and renew it automatically.
sudo crontab -e
Add this line at the end of the file
0 0,12 * * * certbot renew >/dev/null 2>&1
Hit CTRL+X
followed by Y
to save the changes.
This cronjob will attempt to check for renewing the certificate twice daily.
Download WordPress
Now that our server software is configured, we can download and set up WordPress.
It is always recommended to get the latest version of WordPress from their website.
cd ~/yourdomainname.com/public
curl -LO https://wordpress.org/latest.tar.gz
This command will download the latest version and it needs to be extracted.
tar xzvf latest.tar.gz
Now, you can copy the entire contents of the directory into our document root.
sudo cp -a ~/yourdomainname.com/public/wordpress/. ~/yourdomainname.com/public
Next cleanup your root directory by deleting the wordpress
folder and the downloaded tar
file.
sudo rm -r ~/yourdomainname.com/public/wordpress
sudo rm -f ~/yourdomainname.com/public/latest.tar.gz
Set correct permissions for the root folder. Don’t forget to replace the yourdomainname.com
with your domain name
sudo chown -R www-data:www-data ~/yourdomainname.com
Installing WordPress and setting up Configuration File
Now visit your website in the browser and select the language you would like to use and click continue.
You will be prompted to enter your database
, user
, password
, and hostname
.
Enter the database name we created in Cloud SQL and the user assigned with the database with the password. Finally, enter the IP address of Cloud SQL as the hostname.
Now you can run the installation.
Once the Installation is complete we need to set the method that WordPress should use to write to the file system. Since we’ve given the web server permission to write where it needs to, we can explicitly set the file system method to “direct”. Failure to set this with our current settings would result in WordPress prompting for FTP credentials when we perform some actions like WordPress update, plugin updates, file upload, etc. This setting can be added below the database connection settings in the configuration file.
sudo nano ~/yourdomainname.com/public/wp-config.php
Find the line define('DB_PASSWORD', 'password');
and paste the following line below it.
define('FS_METHOD', 'direct');
Hit Ctrl+X
and Y
to save your configuration file.
Now your WordPress is installed and ready to use.
1 Comment
Hi there, thank you for the above instructions, very clear. However, I followed this and installing the SSH creates some kind of problem with the folder access. I get a 404 error on all requests. Any ideas on how to address this?