Sunday, August 1, 2021

How to host Laravel in Digital Ocean


Prerequisites:


1) Create a Digital Ocean Account


2) ssh set up on your local computer


Steps:


1) Create a Droplet


- Using one click install LAMP .


- select standard → $5/month plan → enter ssh keys → enter host name → create droplet button


2) hit ip on the browser to see the site


-----------------------------------------------------------------------------------------------------------------------

To access the site from local machine:

-----------------------------------------------------------------------------------------------------------------------


1) In a new terminal login as the new user
 
         $ ssh root@IP_ADDRESS


2) Note mysql password from the location mentioned during one click installation
              
3) Generate ssh for the server using command
  
                 $ ssh-keygen 
 To see the generated key enter, 
 
                 $ cat .ssh/id_rsa.pub //to display the key 
 4)Add remote server ssh key to github→settings→ssh key

-----------------------------------------------------------------------------------------------------------------------

To set up DB

-----------------------------------------------------------------------------------------------------------------------

		$ mysql -u root -p;
        $ SELECT user,authentication_string,plugin,host FROM mysql.user;
		$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY  'password';                 //passwort from ./root/digital ocean folder

		$ FLUSH PRIVILEGES;
to check:
		$ SELECT user,authentication_string,plugin,host FROM mysql.user;
		$ create database new_db
		$ use new_db
        $ source path/to/uploaded/sqlfile //to import the uploaded db to mysql
		$ exit //mysql
		$ sudo service mysql restart

-----------------------------------------------------------------------------------------------------------------------

To set up Project

-----------------------------------------------------------------------------------------------------------------------

1) git clone the folder to var/www directory

2) cd /var/www

 	        git clone https://github.com/ammezie/laravel-deploy-demo.git
3) install composer
4) run composer install
5) set up all the missing extensions 

sudo apt-get install -y php7.2-xml

	sudo apt-get install -y php7.2-mbstring
one by one like above or in one go as shown below:
	        sudo apt install openssl php7.2-common php7.2-curl php7.2-json php7.2-
	        mbstring php7.2-mysql php7.2-xml php7.2-zip

6) cd laravel-deploy-demo

              
	        composer install

7) For environment variables

            cp .env.example .env

8)  generate an app key using
              php artisan key:generate

9) open .env
                nano .env
Make the following changes into it

Since we are on production, we set APP_DEBUG to false. Also, we set APP_URL to our domain name or server IP address (if no domain name has been set up). Remember to update YOUR_DATABASE_USERNAME and YOUR_DATABASE_PASSWORD


	APP_NAME=laravel-deploy-demo
	APP_ENV=production
	APP_DEBUG=false
	APP_URL=http://DOMAIN_NAME_OR_IP_ADDRESS

	DB_CONNECTION=mysql
	DB_HOST=127.0.0.1
	DB_PORT=3306
	DB_DATABASE=laravel-deploy-demo
	DB_USERNAME=YOUR_DATABASE_USERNAME
	DB_PASSWORD=YOUR_DATABASE_PASSWORD
  

10) Grand permissions using:

            $ sudo chown -R www-data: storage
            $
sudo chmod -R 755 storage



-----------------------------------------------------------------------------------------------------------------------

To take the Project Live

-----------------------------------------------------------------------------------------------------------------------

17) Finally we need to point our server to the Laravel project public folder. We need to edit our Apache config

            nano /etc/apache2/sites-enabled/000-default.conf

There are two lines in this config file that we need to updated

            DocumentRoot /var/www
            <Directory /var/www>

We need to change them to point the public folder in our project public folder

            DocumentRoot /var/www/project/public
            <Directory /var/www/project/public>

Also we need to add the following two lines inside of that same Directory tag

            RewriteEngine On
            RewriteBase /var/www/project/public

Once that’s finished and saved enter

		a2enmod rewrite

we need to restart Apache

                $ sudo service apache2 restart

In case of error:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message


		$ sudo nano /etc/apache2/apache2.conf

add to the end of file 

        	          ServerName 64.xxx.xx.xxx 
 	          sudo systemctl restart apache2


References:


https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04


https://scotch.io/tutorials/deploying-laravel-to-digitalocean



----------------------------------------------------------------------------------------------------------------------

Additional things you may do:

------------------------------------------------------------------------------------------------------------------------

1)set up phpmyadmin


2) login to the server as non root user with admin prevelege for security.

from local computer enter the command to access server:

        		
		ssh root@IP_ADDRESS

then create a new user using command

         		
		adduser new_user


3) To give new user sabi admin preveleges


                usermod -aG sudo new_user


4)copy public key to your new server

      
		 cat ~/.ssh/id_rsa.pub   //run local to get public key

5) In the server give the command as 
        
		 su -user


6) we need to create a new directory called .ssh and restrict its permission:

        	 mkdir ~/.ssh
        	 chmod 700 ~/.ssh 
 7) Within the .ssh directory, create a new file called authorized_keys:
                
		touch ~/.ssh/authorized_keys

8)open the file using cmd
                  
		vim ~/.ssh/authorized_keys //paste ssh copied from local here
9) esc shift :wq + enter

10) restrict permissions of authorised file using the command:
                
		    chmod 600 ~/.ssh/authorized_keys //restrict permission
          exit //return to root



10) hit ip address
                

No comments:

Post a Comment