Host a laravel project to digital ocean droplets or any other VPS.
In the beginning, a remote server or VPS was expensive. nowadays, the digital ocean makes it less expensive by introducing their droplet package.
We can host our project in multiple ways. The most used scenario is using Command tools such as git bash. For hosting a laravel project we need to complete several steps & need to install some tools and software.
At first, log into your droplet with the help of ssh and git bash.
If you did not create any user by default your user is root and the password is the password you created during digital ocean droplet creation. In this tutorial we are assuming, our VPS operating system is ubuntu 20.04.
Step Two: Install Apache2
After successful login, you can see a screen that shows all the system introductory text. now you can install anything in your logged remote server using bash tools as you can do with your local machine. for this tutorial our system operating system are ubuntu.
Now, we have to install a web server tool either Apache or Nginx. For this tutorial, we are going to install Apache.
sudo apt update
sudo apt install apache2
Step Three: Install PHP 7.2 and Related Modules
After installing apache, now it's time to install PHP and its library.
sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-cli php7.2-zip php7.2-curl php7.2-imap php7.2-mysql
Here, 7.2 is our PHP version. you can install any version such as 7.4,7.3. just make sure you replace the version number in every word that contains the version number.
Step Four: Enable Rewrite Module
After completion of apache installation, enable rewrite permission and restart your apache.
sudo a2enmod rewrite
sudo systemctl restart apache2
Step Five: Install MySQL
sudo apt install mysql-server
Step Six: Mysql User and Password
Now, it's time to create a user for MySQL. you can either create a new one or you can alter the root user password. In this tutorial, I’ll alter the root user with mysql_native_password by running the below command. Don’t forget to flush privileges.
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘pass4321’;
Step Seven: Import your database
Import your database through MySQL workbench or PHP my admin or in any other way. You can search on google “Connect MySQL workbench to remote database”. By using ssh protocol you can connect easily. Or you can import databases in many other ways.
Step Eight: Create project directory in VPS/ Server
By default in apache environment folder structure looks like /var/www/html.
you can check your setup status by browsing your_ip/html. If your setup is ok then you will see a default apache page like the attached image.
where var/www is for apache server structure and html is the default folder for hosting any project. we can name it anything or we can delete this html folder and can make a new directory. just make sure your conf file is according to your new folder structure.
For this tutorial we’ll delete html folder and will create a new folder name will be myLaravelProject
rm -r html
sudo mkdir myLaravelProject
here, cd /var/www command will put you into the www folder. Then we removed html folder by the rm command which was a subfolder of the www folder. and at last, created our new directory using the mkdir command.
Step Nine: Project File Transfering.
Transfer your project file into the myLaravelProject folder using git or any other FTP tools. You can search on google “How to transfer file from local machine to remote server”. you will find there are lots of tools. File Zilla is the most popular tool for file transfer. For Developers, using git will be a good decision.
Step Ten: Change Apache Configuration
By default, Apache’s default configuration file name is 000-default.conf. We can create a new config file or can edit the existing one.
Now, we are going to edit the default config file using nano editor. you can use vim also.
sudo nano /etc/apache2/sites-available/000-default.conf
Now, You can see their default config like the attached picture. See there document root is /var/www/html . But in our case, in the beginning, we removed the html folder and created a new myLaravelProject folder. So now we will replace this document root “/var/www/myLaravelProject/public” We use public in the root path because we all know laravel bootstrap starts from the public folder.
<Directory /var/www/myLaravelProject >
Options Indexes FollowSymLinks MultiViews
Require all granted
Then add these new lines before ending the virtual host tag. now press ctrl+o then press enter and then quit from nano editor ctrl+x.
Sometimes it may not be enough so along with it you can change the default apache config file by “nano /etc/apache2/apache2.conf” command.
find <Directory /var/www/> then replace AllowOverride None to AllowOverride All.
Step 11: Restart apache by below command
sudo systemctl restart apache2
Step 12: Download & Install Composer then permit to some laravel file and folder
Run these below commands sequentially.
curl -sS https://getcomposer.org/installer | sudo php — — install-dir=/usr/local/bin — filename=composer
sudo chmod -R 777 storage
chmod 777 -R /var/www/myLaravelProject/.env
chmod 777 -R /var/www/myLaravelProject/storage/
chmod 777 -R /var/www/myLaravelProject/bootstrap/cache/
Congratulations! Now you can access your project by your_ip/myLaravelProject URL.
If you are new to VPS then installing laravel will look difficult and not interesting. But trust me your thinking will be change when you explore the fun of VPS and. You may face some problems for a different reason. most of the problems are related to the Apache installation and configuration.
**don’t lose hope.**