Byte Ebi's Logo

Byte Ebi 🍀

A Bit everyday A Byte every week

Environment Initialization and Server Configuration (Docker + Laravel)

Setting up the basic Docker and Laravel execution environment on a new server

Ray

If you are setting up a new cloud server and want to create a Laravel development environment using Docker on it.
And run Laravel’s common composer and artisan commands remotely, here’s a guide for you.

Since these steps are not frequently executed, documenting them makes it convenient for future reference.
It also lays the groundwork for the upcoming article on using Docker to build a Laravel environment.

1. Establish Repository Access Permissions

To allow the remote server to access a specific repository, you need to add an SSH key.

Standard Method:

After SSH logging into the remote server, generate an SSH key on the remote machine using the following command:

ssh-keygen

Then add the public key to the remote repository.

Unrecommended Method:

Alternatively, you can copy an existing key pair with repository access to the remote server.
If the .ssh folder doesn’t exist, create it with permissions set to 700.

mkdir .ssh
chmod 700 .ssh
cd .ssh
vim id_rsa
chmod 600 id_rsa
vim id_rsa.pub
chmod 644 id_rsa.pub

2. Update Linux Packages

Since we’ll be installing packages later, it’s a good practice to update the server’s package list.

sudo apt update

3. Install Docker

Since we’re creating a Docker environment, it’s essential to install Docker.

sudo apt install docker.io

4. Install Docker-Compose

We’ll use docker-compose to orchestrate multiple Docker containers and launch a complete service.
As we’ll be starting various containers like nginx, PHP, redis, mysql for future Laravel projects, managing them with docker-compose will be more convenient.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

5. Install PHP Packages

For Laravel projects, where we don’t need compilation, changes made outside the container will reflect inside it.
Therefore, it’s not always necessary to execute commands within the container.
To run php artisan and phpunit directly on the host and install/execute composer, install PHP based on your requirements.

apt install php7.4-cli php7.4-gd install php7.4-mbstring php7.4-curl php7.4-xml php7.4-zip

If you’re unsure about the package names, you can use tab completion after apt install php7.4 to list all packages starting with “php7.4,” making it easy to find the desired version.

6. Install PHP Package Manager: Composer

Follow the official steps for installation: Download Composer

Example (official website should be the primary reference for the correct commands):

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Move composer to the environment variable for global usage.
It’s recommended to perform this step.
Otherwise, you can only execute commands within the installation path where composer.phar is located.

sudo mv composer.phar /usr/local/bin/composer

To view globally executable commands and their corresponding directories:

echo $PATH

7. Set .sh File as Executable

If there are shell scripts to be executed in the project, you must modify the file permissions.

chmod 755 {target file path}

With these steps, you can now execute the following commands on the host machine:

  • docker
  • composer
  • php artisan

This completes the foundational setup needed for using Docker to build a Laravel environment in the future.

Recent Posts

Categories

Tags