Install server with Apache from A-Z

Tram Ho

I, What to install now

You are a newbie, and on a beautiful day, you received a request from your “boss”: “The situation is that I have a linux server, running ubuntu 20, I need you to help me install the environment to run the project. abc…xyz”, “eh… yes”. Done, even though a long time ago, it “looks” like you’ve installed something like this yourself, but due to a long outsourcing project, you forgot how to install it, Haizzzz. Ignoring complicated things like docker, this will be a “pure” article about installing an environment for a project, specifically a Laravel project. A little introduction about the technologies that the project will use:

The webserver I use here is Apache (you can completely use Ngnix to configure). To start with, I will install LAMP Stack, reintroduce LAMP in order: Linux, Apache, Mysql and PHP. Since this is a Linux server, using LAMP, you can use WAMP on Windows, MAMP on MacOS, and XAMPP on any of the above. Let’s start.

Since Ubuntu 20 already corresponds to the L (LINUX) part of LAMP, we will install it from Apache onwards

Install Apache

Apache is free and open source web server software. It is accounting for about 45% of the worldwide website market share. The official name of Apache is Apache HTTP Server, operated and developed by the Apache Software Foundation. To install it is quite simple, just run the following command:

$ sudo apt-get install apache2 -y

Next, open your browser and type 127.0.0.1 to check if Apache is up and running.

  • When the browser appears as above, Apache has been installed successfully. Next will install MySQL.

Install MySQL

MySQL is an open source relational database management system, widely used in the world. The following can be installed:

To set a password for the root account:

Continue : ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password'; Replace “your password” with the password you want

Then type exit to exit

If you want to change the root password, run the following command:

  • Here there are 2 options, if you choose Y, you will use VALIDATE PASSWORD PLUGIN which means you have to use strong password for the database, such as password length must be more than 8 characters, uppercase, lowercase, special characters, etc… If you choose N, you won’t use VALIDATE PASSWORD PLUGIN. And here installed on a personal machine, so there is no need to use VALIDATE PASSWORD PLUGIN. So here I will choose N for ease of installation.
  • After selecting N, it will ask you to set a password for MySQL. Here to make it easy to remember I will put it: 123456

  • Retype the password again. And press Enter.

  • Next select Y and press Enter.

  • Next select N and press Enter.

  • Next select Y and Enter.

  • Next select Y and Enter.

Changed done, now run

Done enter the new password, if the login is successful, it means you have installed Mysql server, remember to note the password

Install PHP

Check the PHP version with the command php --version

Next we need to install the extension for PHP depending on the php version:

Thus, the LAMP has been set up successfully ^^.

Install Composer

Composer is a tool for managing PHP packages or libraries. Composer will install these libraries into the project you are working on.

Composer requires: php-cli to execute PHP scripts in the command line. unzip to unzip the archive. If not installed, sudo apt install php-cli unzip -y to install it

After the installation is complete, type composer to check as shown below is successful, in the next step just drag the laravel project, cd to the root of the project and run composer install

Reference at: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-20-04

Install Nodejs

To install nodejs we first install nvm, because nvm allows us to switch between nodejs versions, very convenient.

Then we can install the required nodejs version, here I choose node 12.18.3, do the same with other node versions

When we want to change node version (to 14 for example) we just need

If you want to leave the default node version (example 14)

Basically, we have installed the environment, next we drag the code on git to the /var/www/html/ directory (if the directory does not exist, we can create a new directory, we completely You can create any directory, it doesn’t have to be /var/www/html/ ) Suppose I have a directory tree like this: root var www html backend admin user With backend run laravel, used to handle api Admin is the management site, run reactjs User is the end user site, run nextjs For Project User due to using nextjs, we need to install pm2 and start it with a fixed port (here I choose 3001) (this part you gg search yourself to install and run, note that port 3001 will not be registered in /etc/apache2/ports.conf) After installing pm2, enable the proxy, sudo a2enmod proxy , we will use the proxy in the config below for running pm2 We will skip adding the .env variable for each project above, or install the library for it (composer install, npm install, yarn, DB connection, migrate, seeder, …), hereafter I will continue p Apache config section

Config Virtual Host

In this section, we need to pay attention to some of the following folders

  1. /etc/apache2/ports.conf This directory is used to register the port to be used

After adding the required port type “Esc” => “: + wq” => “ENTER” to save

Run “sudo service apache2 restart” to run apache again

  1. /etc/hosts This directory to register Alias

This section will be linked to the following section.

  1. /etc/apache2/sites-availabels Config in this directory helps projects automatically run into the index without having to “php artisan serve” with laravel or “npm run dev” with reactjs

We can edit the default file

But it’s better to copy out a new config file, so that the name is easy to remember

in democonfig we will edit as below:

As seen above, my API part to run on port 8000, admin on port 80, user on port 3000, ports are registered in /etc/apache2/ports.conf, and ServerAlias ​​has the same name as registered name in /etc/hosts After saving the above configuration, we restart apache: sudo service apache2 restart

Done, the next thing we will go out of the browser and enjoy the results, good luck!

Share the news now

Source : Viblo