Google Cloud Website Setup


How to setup SQL website with LAMP, Apache, and FTP on Google Cloud. Setup LAMP and Apache on Google compute engine, MySQL storage, and FTP with Filezilla.

I’t was quite difficult for me to find out how to setup a basic website on google cloud. I did a lot of research, and decided to list the basics of what I did here.

Setting Up LAMP

https://cloud.google.com/community/tutorials/setting-up-lamp

To set up your VM instance and install Apache and PHP
Create A Google Cloud Compute Engine VM instance And make sure you enable Allow HTTP traffic and Allow HTTPS traffic
Click SSH on your VM instance of choice to open a command prompt.

Run the following Commands in SSH
For Debian 8 and Ubuntu 14
sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5
For Debian 9 and Ubuntu 16/17
sudo apt-get update
sudo apt-get install apache2 php libapache2-mod-php
For CentOS 6 and 7
sudo yum check-update
sudo yum -y install httpd php
sudo service httpd start
sudo chkconfig httpd on

To test Apache and PHP
copy the External IP of your Compute Engine VM instance and past it in your browser. You should see the Apache test page.
Run the following Commands in SSH To Test PHP
sudo sh -c 'echo "this is a php test" > /var/www/html/phptest.php'

Then paste the External IP of your Compute Engine VM instance into your browser with /phptest.php at the end. you should see this is a php test on that page.

To Install MySQL

Run the following Commands in SSH
For Debian/Ubuntu
sudo apt-get install mysql-server php5-mysql php-pear
For Ubuntu 16
sudo apt-get install mysql-server php7.0-mysql php-pear
For CentOS 6
sudo yum -y install httpd mysql-server php php-mysql
sudo service mysqld start
sudo chkconfig mysqld on
For CentOS 7
sudo yum -y install httpd mariadb-server php php-mysql
sudo systemctl start mariadb
sudo systemctl enable mariadb


After Your Done, Run this command For All
sudo mysql_secure_installation

To Install phpmyadmin

Run the following Commands in SSH
For Debian/Ubuntu
sudo apt-get install phpmyadmin
For CentOS 6 and 7
sudo yum install phpMyAdmin


Configure phpmyadmin
- Select apache2
- Select yes to use dbconfig-common for database setup
- Enter the database administrator's password that you chose during MySQL configuration
- Enter a password for the phpMyAdmin application

Run the following Commands in SSH For CentOS 6 and 7
For CentOS 6
sudo service httpd restart
For CentOS 7
sudo systemctl restart httpd



Setting Up VPC Network


Go To Google Cloud VPC network External IP address and make sure Attached to is set to your Compute Engine VM Instance.
Copy the new External IP, and add it to your domain name DNS as A(if its IPv4) or AAAA(if its IPv6).


Setting Up FTP with Filezilla

https://www.onepagezen.com/google-cloud-ftp-filezilla-quick-start/

You will need to download Filezilla and putty key gen

Once Downloaded, Generate A Putty Key, Shake your mouse randomly in the key box to generate a random key. Save Private key to your computer.

In Filezilla, go to Edit(at the top of the window) and click Settings then click SFTP and Add key file and select the putty key you downloaded to your computer.

Then close that tab and go back to putty key gen. Copy the public key. Go to your Compute Engine Metadata sshKeys and click Edit.
Click Add item(at the bottom) and paste your public key. Then click Save.

Go back to putty key gen, and copy the key comment.
Go back to Filezilla, and go to File(at the top of the window) and click Site Manager then click New Site and name it what you want.
Under General Settings, paste your key comment in the box labeled User. Go to your Compute Engine VM Instance and copy the External IP then paste it it the box labeled Host.
Under Advanced Settings, in the box labeled Default remote directory put /var/www/html. Click Connect and you should see your phptest.php file in that folder. This is where you will edit your website php files, like you normally would on another host in public_html when building on cPanel.
After you add permissions to edit files(shown below), you can delete phptest.php and index.html and replace them with your own index.php.


Adding Permissions to edit files with filezilla

https://www.onepagezen.com/fix-ftp-permission-errors-google-cloud/

You may notice a Permission denied message. To fix that, you need to go back to your Compute Engine VM Instance, and open SSH.

Run the following Commands in SSH
stat -c "%a %n" /var/www/html
sudo chmod 777 /var/www/html
sudo service apache2 graceful

You can now edit files in Filezilla.
You may notice that .htaccess files will Not run.

To run .htaccess files, Run the following Commands in SSH
sudo nano /etc/apache2/apache2.conf


Only do this for "/var/www/". The other Directory's have AllowOverride None for security.
Find the following code
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
And replace it with
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>



Adding a cloud SQL Database

Go to Google Cloud SQL and create a new instance. Choose MySQL and configure it how you like. Make sure to include a Root Password(highly recommended).
Once your cloud SQL is done loading, open it, scroll down and click Connect from a Compute Engine VM instance, and follow Googles tutorial.


Connecting to a cloud SQL Database

Before you connect to the database, you need to create a User and Database to connect to.
In your cloud SQL Instance, go to USERS and click Create user account. Put any username and password you want. Check Restrict host by IP address and put the External IP of your Compute Engine VM Instance and click Create.
Go to DATABASES and click Create database. Put any name you want. Character set utf8(recommended).

Go to Filezilla, and connect to your site through ftp.

Create a new file db_connect.php.
Copy the Public IP of your cloud SQL(found on your cloud SQL instance under Connect to this instance).
Add the following code to this new file
$host = "The IP of your cloud SQL";
$user = "The User you created for your cloudSQL";
$pass = "The Password for that User";
$db_in_use = "The Database you created for your cloudSQL";

$mysql = new mysqli($host, $user, $pass, $db_in_use);

$host = '';
$user = '';
$pass = '';
$db_in_use = '';

if($mysql->connect_errno){
echo "Failed to connect to Database";
$mysql->close();
die();
}

Any time you want to connect to the cloud SQL Database, add the following code to the file your connecting from
//connect database
require_once('db_connect.php');

//your code here

//close database
$mysql->close();



Adding SSL Encryption and https connection

https://certbot.eff.org/lets-encrypt/ubuntuother-apache

Go to your Google Cloud Compute Engine VM Instance and open SSH.

Run the following Commands in SSH
wget https://dl.eff.org/certbot-auto
sudo mv certbot-auto /usr/local/bin/certbot-auto
sudo chown root /usr/local/bin/certbot-auto
sudo chmod 0755 /usr/local/bin/certbot-auto
sudo /usr/local/bin/certbot-auto --apache
sudo /usr/local/bin/certbot-auto --apache certonly

Comments

Comments


[b] Bold [/b] [i] Italic [/i] [u] Underlined [/u] [s] Strikethrough [/s] [c="red"] Color [/c]
[url="https://www.swiftninjapro.com"] Url Link [/url]

Comments Must Contain At Least 3 Words
You Must Be Logged In To Leave A Comment