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:
1 2 3 4 5 | Backend : Laravel 8, PHP 8.1 Admin: Reactjs End-User: Nextjs DB: Mysql |
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:
1 2 | $ sudo apt-get install mysql-server -y |
To set a password for the root account:
1 2 | $ sudo mysql |
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:
1 2 | $ sudo mysql_secure_installation |
- 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
1 2 | $ mysql -u root -p |
Done enter the new password, if the login is successful, it means you have installed Mysql server, remember to note the password
Install PHP
1 2 3 4 | sudo apt install software-properties-common && sudo add-apt-repository ppa:ondrej/php -y sudo apt update sudo apt install php8.1 libapache2-mod-php8.1 |
Check the PHP version with the command php --version
Next we need to install the extension for PHP depending on the php version:
1 2 | sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl -y |
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
1 2 3 4 5 6 7 | $ cd ~ $ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php $ HASH=`curl -sS https://composer.github.io/installer.sig` $ echo $HASH $ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" $ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer |
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.
1 2 3 4 | sudo apt install curl curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash source ~/.bashrc |
Then we can install the required nodejs version, here I choose node 12.18.3, do the same with other node versions
1 2 | nvm install 12.18.3 |
When we want to change node version (to 14 for example) we just need
1 2 3 | nvm install 14 nvm use 14 |
If you want to leave the default node version (example 14)
1 2 | nvm alias default 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
- /etc/apache2/ports.conf This directory is used to register the port to be used
1 2 | sudo vi /etc/apache2/ports.conf |
After adding the required port type “Esc” => “: + wq” => “ENTER” to save
Run “sudo service apache2 restart” to run apache again
- /etc/hosts This directory to register Alias
1 2 | sudo vi /etc/hosts |
This section will be linked to the following section.
- /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
1 2 | sudo nano /etc/apache2/sites-available/000-default.conf |
But it’s better to copy out a new config file, so that the name is easy to remember
1 2 3 4 5 6 7 8 | sudo su a2enmod rewrite // bật chế độ ghi đè cd /etc/apache2/sites-available/ // di chuyển cp 000-default.conf democonfig.conf // sao chép file a2dissite 000-default.conf // disable a2ensite democonfig.conf // enable vi democonfig.conf // mở file để chỉnh sửa |
in democonfig we will edit as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <span class="token operator"><</span> VirtualHost <span class="token operator">*</span> <span class="token punctuation">:</span> <span class="token number">8000</span> <span class="token operator">></span> ServerName <span class="token constant">_</span> ServerAlias project <span class="token operator">-</span> dev <span class="token comment">// phải trùng tên đã đăng kí trong /etc/hosts</span> DocumentRoot <span class="token operator">/</span> <span class="token keyword">var</span> <span class="token operator">/</span> www <span class="token operator">/</span> html <span class="token operator">/</span> backend <span class="token operator">/</span> <span class="token keyword">public</span> <span class="token operator"><</span> Directory <span class="token operator">/</span> <span class="token keyword">var</span> <span class="token operator">/</span> www <span class="token operator">/</span> html <span class="token operator">/</span> backend <span class="token operator">/</span> <span class="token keyword">public</span> <span class="token operator">/</span> <span class="token operator">></span> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow <span class="token punctuation">,</span> deny allow from all <span class="token keyword">Require</span> all granted <span class="token operator"><</span> <span class="token operator">/</span> Directory <span class="token operator">></span> LogLevel debug <span class="token class-name type-declaration">ErrorLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> error <span class="token operator">.</span> log <span class="token class-name type-declaration">CustomLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> access <span class="token operator">.</span> log combinedwq <span class="token operator"><</span> <span class="token operator">/</span> VirtualHost <span class="token operator">></span> <span class="token operator"><</span> VirtualHost <span class="token operator">*</span> <span class="token punctuation">:</span> <span class="token number">80</span> <span class="token operator">></span> ServerName <span class="token constant">_</span> DocumentRoot <span class="token operator">/</span> <span class="token keyword">var</span> <span class="token operator">/</span> www <span class="token operator">/</span> html <span class="token operator">/</span> admin <span class="token operator">/</span> dist <span class="token operator"><</span> Directory <span class="token operator">/</span> <span class="token keyword">var</span> <span class="token operator">/</span> www <span class="token operator">/</span> html <span class="token operator">/</span> admin <span class="token operator">/</span> dist <span class="token operator">></span> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow <span class="token punctuation">,</span> deny allow from all <span class="token keyword">Require</span> all granted <span class="token operator"><</span> <span class="token operator">/</span> Directory <span class="token operator">></span> <span class="token class-name type-declaration">ErrorLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> error <span class="token operator">.</span> log <span class="token class-name type-declaration">CustomLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> access <span class="token operator">.</span> log combinedwq <span class="token operator"><</span> <span class="token operator">/</span> VirtualHost <span class="token operator">></span> <span class="token operator"><</span> VirtualHost <span class="token operator">*</span> <span class="token punctuation">:</span> <span class="token number">3000</span> <span class="token operator">></span> ServerName <span class="token constant">_</span> ProxyPreserveHost On ProxyPass <span class="token operator">/</span> http <span class="token punctuation">:</span> <span class="token comment">//localhost:3001/ //phần chạy pm2 nè</span> ProxyPassReverse <span class="token operator">/</span> http <span class="token punctuation">:</span> <span class="token comment">//localhost:3001/ //phần chạy pm2 nè</span> <span class="token class-name type-declaration">ErrorLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> error <span class="token operator">.</span> log <span class="token class-name type-declaration">CustomLog</span> <span class="token variable">$</span> <span class="token punctuation">{</span> <span class="token constant">APACHE_LOG_DIR</span> <span class="token punctuation">}</span> <span class="token operator">/</span> access <span class="token operator">.</span> log combinedwq <span class="token operator"><</span> <span class="token operator">/</span> VirtualHost <span class="token operator">></span> |
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!