How to Deploy Codeigniter on Google Cloud Platform

Category
Tutorial
Reading
8 mins
Views
3.0K
Posting
10 Oct 2022

Cloud-based hosting services are currently in great demand by many people, such as AWS, Google Cloud Platform or Azure which have many benefits, one of which is being able to set up your own server, database, storage and specifications to meet the needs of a scalable website or application. Cloud is a technology in the digital world to store data for certain needs. Especially in today's cloud-based services, many have been provided such as audio, video, photo media storage and website or mobile applications.

In this article, we will discuss a general tutorial on how to deploy the Codeigniter on Google Cloud Platform such as Google Cloud Platform (GCP) using the easy and complete uBuntu OS.

Google Cloud Platform provides a variety of computing, storage, database and other needs that match your website or application. Before entering the tutorial material, make sure you have registered with the Google Cloud Platform (GCP) to follow the step by step properly.

 

How to deploy codeigniter projects into Google Cloud Platform

 

 

1. Preparation

To create a server in Google Cloud Platform, the first step before creating a compute engine is to create a project first.

Then, click Create Project and type in the [projectname] form with whatever project name you want, then click Create.

 

2. Creating a Virtual Machine in Google Cloud Platform

The next step is to enter the VM instances page with the following steps:

In the top left, look for the hamburger icon or 3 bars, then click on the icon, find the Compute engine section and then select VM instances. After entering the VM instances page, click the create button.

Then, it will display a form to fill in information related to the specifications that will be used such as server name, region, operating system to be used, server specifications, storage and zones.

1. Name

For the name, you can fill it as you like, it can be filled with any name.

2. Regions & zones

For the region, it is recommended to choose the region closest to us, you can choose the Jakarta or Singapore region, but I recommend only Jakarta. For the zone, you can just ignore it.

3. Machine type

In this session, you can choose the server specifications that will be used such as CPU Core and RAM, and also, you can look on the right side to see the monthly price for each of the options provided, just adjust it according to your needs.

4. Boot disk

This boot disk is an operating system that will be used such as Debian, uBuntu, CentOS, SUSE, Windows etc., it is recommended to choose an OS that you have mastered, for example you are familiar with uBuntu, select uBuntu, as well as others.

Then, in the version session and boot disk type, it's up to you to choose which version or you can choose the newest version. And the type of boot disk, it is recommended to choose SSD.

5. Firewall

For the firewall section, just check the HTTP and HTTPS sections for ports 80 and 443.

Then click the create button, and wait until the VM instances are finished installing. After the VM instances are complete, a new line will appear on the VM instances page where in each line there is information such as the name of your VM instance, Zone, private IP/internal IP, public IP/external IP and SSH.

In the SSH section, this will be used to give commands to the server (according to the selected OS) in creating a web server such as NGINX or Apache. The advantage of Google Cloud is that it can access SSH directly in the browser or through third-party applications such as puTTY.

 

3. Creating a web server in Google Cloud Platform

After everything is okay, you can just access SSH by using a browser to install the web server, just click on SSH, wait until a new window appears and finish connecting to the server that has been created.

For uBuntu users, you can follow some of the command lines that we will give below to update the OS, upgrade the OS, install a webserver like Apache or NGINX, the following commands are:

sudo apt update
sudo apt upgrade

// choose one to install apache or nginx
sudo apt install apache2
sudo apt install nginx

After the web server has been installed and upgraded in SSH, you can check whether the web server is installed or not, by copying and pasting the public IP into the address bar of the browser.

To accommodate all your project application files in Google Cloud Platform, all these files will be in the var/www/html directory.

 

4. Upload the project to Google Cloud Platform

To upload our project into Google Cloud Platform, it takes several steps and tools used such as puTTY and filezilla. puTTY is an SSH key generator that will be used to connect between Google Cloud Platform and Filezilla. Here are the steps:

  • You can install the puTTY software, then select the generate button to create a public, private and comment key for the username in FileZilla (It is recommended not to close the puTTY application until the process is complete).
  • Copy the public key authorized keys which will later be pasted into the Google Cloud Platform SSH settings. And don't forget to download the private key by clicking the save private key button.
  • Then paste the public key into GCP settings by going to Compute engine > Metadata > SSH Keys. Select edit.
  • Click add item and paste the puTTY public key.
  • Next is to download the FileZilla software, open the application, select settings, navigate to Connection > SFTP and select the Add key file button, locate and select the public key file that has been downloaded in puTTY then click ok.
  • In the Google Cloud Platform dashboard, copy and paste the external IP into the FileZilla host field with the format sftp://[External IP] and for the username, you can copy and paste the key comment in puTTY.
  • After okay, then click Quickconnect, wait until the connection process is complete.
  • If successful, you can upload your compressed project file into zip format and save it in the directory: var/www/html (it is recommended that all project files not be in one folder, so that when the unzip extract process is directly in the var/www/html directory)
  • In the process of unzipping the project, it will be done in SSH.


5. How to unzip a project in Google Cloud Platform

At this stage, to unzip the project that has been uploaded to the server, you can log back into SSH Google Cloud Platform, Note: but before unzipping your project file, you are required to first install the zip unzip package on the server by running the command:

sudo apt install zip unzip

When finished, you can go directly to the unzip project stage by directing your project zip file into the var/www/html directory, namely with the command:

sudo unzip projectname.zip -d /var/www/html

When finished, you can immediately try to access your project in a browser with http://[External IP]. However, at this stage, the Codeigniter application cannot be accessed, because it has not installed PHP, some of the required PHP libraries, MySQL database and phpmyadmin.


6. Install PHP, MySQL and phpmyadmin on Google Cloud Platform

To install PHP, MySQL and phpmyadmin, you can enter the following command in SSH:

// install php and libraries
sudo apt install php libapache2-mod-php php-mysql php-mbstring php-zip php-gd php-json php-curl php-intl php-gd

// install mysql
sudo apt install mysql-server

// install phpmyadmin
sudo apt install phpmyadmin

Phpmyadmin is needed to make managing the database easier. During the process of installing phpmyadmin, several options will appear, such as which webserver to use and options for dbconfig-common.

  • For the webserver, you can just choose the one that matches the installed web server.
  • Then for the dbconfig-common option, just select No.

After the process is complete, please check whether phpmyadmin has been installed or not by entering http://[external IP]/phpmyadmin.

To change the password on phpmyadmin, you can run the command into SSH, namely:

sudo mysql -u root -p

Later, an enter password will appear, just enter it, and enter the following command to change the password to root

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

When finished, please try to access the database again with the root username and root password. If you have successfully entered, you can import the created sql into phpmyadmin.


7. Configure Codeigniter in Google Cloud Platform

After all stages of installing PHP, MySQL and phpmyadmin have been completed, you can directly configure your Codeigniter application such as base url, database name, database username, database password etc. However, to enter the file to be addressed, such as config.php and database.php, you can use the following SSH command:

// to enter the application folder
cd application

// to enter the config folder
cd config

// to access the contents of the config.php file
sudo nano config.php

// to access the contents of the database.php file
sudo nano database.php

To save changes to the file, the trick is to press the ctrl + x keys, then type Y and enter.


8. Setting htaccess on apache

The last step is to enable htaccess on apache to run our codeigniter application on Google Cloud Platform by running the command on SSH:

sudo a2enmod rewrite

When finished then enter the next command, namely:

sudo nano /etc/apache2/apache2.conf
  • Look for the AllowOverride line of code in the directory tag: <Directory /var/www/>
  • Change the value of none in AllowOverride, to all.
  • Save with ctrl + x, then type Y and enter.

And finally, enter the following command in SSH to restart the web server:

sudo service apache2 restart

After everything is done, please try to test your Codeigniter application project in the browser.

 

​Conclusion

Although to use cloud services such as Google Cloud Platform requires expertise in running a terminal for every command, cloud services provide more benefits compared to hosting services in general, because the advantage of cloud services is that they have good flexibility and can manage everything according to what we want. want.

Share