Deploying Web Application Stack Apache, PHP, MYSQL, Domain Name and SSL

 

Web application server stack Apache, PHP, MySQL

I’ve just used GCP recently, so I created one Virtual machine, and as usual I try to develop my own web server manually as I always did in the old school ways, by using SSH.

Although GCP has some tools for deploying button click web servers from the marketplace, for example LAMP, you can install those and your web server will be ready quickly, but i am not familiar with that so i decided to do the old ways.

And besides, the prices of those marketplace solutions can be 5 times higher than if you just installed your own server stack by yourself. All the web application components are all open source and free from the Operating System (which i choose Debian 10), the web server (Apache), Database (MariaDB) and PHP. Except extra cost for Domain name and SSL.

Turns out, I am still not that much familiar with bare metal web application development either. I still struggle to remember what to do, but I have the big picture so I can install a web application from scratch. 

Installing an Apache web server is easy, but then apache is not gonna be enough. You need this 3 basic web application criteria:

  • Able to be access via domain name
  • Have an SSL
  • Server side programming
  • Database connection

Usually I use PHP because you can install wordpress later and it is very simple to connect to the database which is either MariaDB or React. Although I am familiar with Spring and ReactJS, it's overkill to create those types of apps for just my semi personal project.

This is a summary if i am going to do this bare metal web development again, i will look at this post as a reference so i am not wasting my time on doing something that is supposed to be straightforward and easy.

Actually Google also posted this https://cloud.google.com/community/tutorials/setting-up-lamp, and probably has a better clearer explanation. But anyway these are sets of steps you need to follow to develop web Application on GCP Compute Engine VM Instance.

1. Open Cloud Console, navigate to the VM Instances.

2. Create new VM instance

3. Set Machine type to the very small one, or up you, e2-micro for beginner is enough

4. Check the Firewall section to Allow HTTP traffic and HTTPS traffic.

5. Click Create and wait

6. After your VM is created, Click on the SSH.

7. At this point you are gonna install Apache,MariaDB, and PHP using the SSH

sudo apt-get install apache2 php libapache2-mod-php

sudo apt-get install mariadb-server php-mysql

8. Test accessing our server from our browser

http://YOUR_VM_IP_ADDRESS

Note : don’t use https, we are not enable that feature yet

9. Secure our MySQL / MariaDB server

sudo mysql_secure_installation

10. Setting the root password for our MariaDB, MySQL database

- Firstable we need to be able to switch between our default user and root, in debian you can type su - root and will be prompted with your root password. And that’s the problem, we didn’t even know our root password. Luckily in debian we can set new root password by using sudo passwd

- Then switch to root user su - root

- Now you can login to maria db server without password, just type mysql

- Then we can start to change it

- For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

- For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

- Then

FLUSH PRIVILEGES;

- Then restart MySQL

sudo service mariadb restart

11. Now switch back to our default user: su - our_username

12. At this point we are gonna pointing our domain from Namecheap to our VM

- After purchase domain, click manage the domain

- Set nameservers to Namecheap Basic DNS

- Then switch to Advanced DNS tab

- Set 3 row for DNS record

Type

Host

Value

TTL

A Record

*

34.101.80.100

Automatic

A Record

@

34.101.80.100

Automatic

A Record

www

34.101.80.100

Automatic

Note: set value to your VM IP address, and also wait for couple of minutes for so called DNS propagation

- Now you can test from web browser by acces the http://yourdomain.com

13. This step, we are going to install SSL

14. Open SSH, generate certificate key and certificate CSR

openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr

Note: remember this location, this will create two files, server.key and server.csr

15. Open the server.csr file and copy those CSR

16. Back to Namecheap and activate your certificate that you purchase

17. Pasted out your copied CSR

18. In the next step, select your designated email for SSL verification

19. Then you will be emailed the certificate, then download your certificate download it

20. Upload those CRT certificate to your VM

This is the part that I am struggling with, at first i didn't know how to upload files from local to remote computer via SSH. But then actually this Google SSH comes to be very handy, it turns out they have an Upload file feature on the Cog icon located at the right top of the web based console.

21. Now configure Apache to allow 443 or TLS protocol

- Create new apache virtual host

cd /etc/apache2/sites-available

sudo vim yourdomain.conf

- With this content:

<VirtualHost *:80>

        ServerName yourdomain.com

        ServerAlias www.yourdomain.com

        ServerAdmin webmaster@yourdomain.com

        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<VirtualHost *:443>

        ServerName kamusin.com

        ServerAlias www.kamusin.com

        ServerAdmin webmaster@yourdomain.com

        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLCertificateFile /where_you_put_the_certificate/certificate.crt

        SSLCertificateKeyFile /where_you_put_the_certificate_key/server.key

</VirtualHost>

22. Set and enable those virtual host by using the command

sudo a2ensite yourdomain

23. Also we need to enable the Apache  ssl extension

sudo a2enmod ssl

sudo a2enmod headers

24. Now restart the Apache

sudo service apache2 restart

25. At this point your web stack application is fully setup with this criteria:

- You can access your web app via web browser with domain name with https protocol

- Your web application path is set to /var/www/html, but you can change it in the configuration as you desired

- PHP is also linked with apache and MySQL, so any PHP codes will be executed as PHP application, also you can connect to your MySQL database using PHP

If you failed to restart or reload apache, then always look at the apache error log which is ini /var/log/apache2, it will give you some explanation of what you are doing wrong. Then you can search on google for the solution, it always have an answer

For me, because I have done this several times, it’s not that hard to repeat this process. My usual problem is within the transfer file between my local computer and the remote server. There’s one good user interface for uploading data to your server using one single PHP file called Tinyfilemanager.php or Onefilecms.php.

There’s also one single PHP file that can manage your MySQL database called Adminer. The PHP world is simple and quick to do simple things that’s why I like it.

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously