Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Notes

System info

  • Ubuntu 22.04

Install nginx

sudo apt update
sudo apt install nginx

Adjusting the firewall

# enable fireewall
sudo ufw enable
# allow port 22 for ssh
sudo ufw allow 22
# check status
sudo ufw status
# enable for Nginx
sudo ufw allow 'Nginx HTTP'
# sudo ufw app list

Mysql

Install

sudo apt install mysql-server

Run security script

sudo mysql_secure_installation

if you run into error, check this

(Optional) Set password for root

By default, you can access root by

sudo mysql -u root

It use auth_socket, but, if you like, you can set password for root

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Create user

CREATE USER '<user_name>'@'localhost' IDENTIFIED BY '<password>';

Granting a User Permissions

USE my_db;
-- GRANT ALL ON my_db.* TO 'my_user'@'%';
-- If you create user with localhost, use below 
GRANT ALL ON my_db.* TO 'my_user'@'localhost';

List all user

SELECT user FROM mysql.user;

[[mysql-commands]]

Clone repo to /var/www

Copy .env

Change file permission

check here

# This work for me
sudo chown -R $USER:www-data storage
sudo chmod -R 775 storage

PHP

If your ubuntu not have php version you want, then

Add PHP Repository

sudo apt install ca-certificates apt-transport-https software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Now check apt

# replace X.X by php version you want
apt search phpX.X 

If success, move on next step

Install PHP

Base

sudo apt install php8.3 php8.3-fpm php8.3-dom php8.3-xml php8.3-intl php8.3-zip php8.3-mysql php8.3-mbstring

For Mysql database

Install Composer

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer 

Setup nginx

cd /etc/nginx/sites-available 
touch <app-name>
vi <app-name>

Example [[bus_online]] See error below [[#Problem: File not found, display nginx 404 page (instead laravel 404 page)]]

server {
    listen 80;
    server_name bus_online;
    root /var/www/bus_online_server/public;

    index index.html index.htm index.php;

    location / {
       # try_files $uri $uri/ =404;
	try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
       expires max;
       log_not_found off;
    }    
}

Restart Nginx

sudo systemctl restart nginx

Setup .env

Troubleshoting

Set up max size file upload

upload-file-size

Problem: The POST method is not supported for route admin/login. Supported methods: GET, HEAD.

Solution:

php artisan vendor:publish --force --tag=livewire:assets

link

Problem: File not found, display nginx 404 page (instead laravel 404 page)

Solution: I dont know how this work for me, I remove the code snip in nginx config

server {
    listen 80;
    server_name bus_online;
    root /var/www/bus_online_server/public;

    index index.html index.htm index.php;

    location / {
       # try_files $uri $uri/ =404;
       try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    # --------- COMMENT THIS BLOCK -----------
    # location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    #    expires max;
    #    log_not_found off;
    # }    
    # ----------------------------------------
}

set up octane swoole

Remember correct the version

sudo apt install php7.4-swoole
server {
    listen 80;
    server_name bus_online;
    root /var/www/bus_online_server/public;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
       # try_files $uri $uri/ =404;
       # try_files $uri $uri/ /index.php$is_args$args;
       try_files $uri $uri/ @octane;
    }
    
    location /index.php {
        try_files /not_exists @octane;
    }

# location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#       fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
#    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location @octane {
        set $suffix "";
 
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
 
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
 
        proxy_pass http://127.0.0.1:8000$suffix;
    }

    # --------- COMMENT THIS BLOCK -----------
    # location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    #    expires max;
    #    log_not_found off;
    # }    
    # ----------------------------------------
}

Git update for Windows

git update-git-for-windows

Khi commit rồi nhưng cần phải thay đổi file

Lệnh này sẽ gộp file trong Staging area với file của commit gần nhất thành 1 commit, có thể thay đổi hoặc giữ nguyên message

git commit --amend

Bỏ bớt file đã add

git reset HEAD <file cần unstage>

ví dụ

git reset HEAD src/controller/auth.controller.ts

Unstage toàn bộ

git reset

Về thao tác Remote repo

Xem tất cả các URL của remote repo

git remote -v

Thêm URL remote repo vào local repo

git remote add origin <url>

Thay đổi URL remote repo

git remote set-url origin <url>

Sao chép remote repo

git clone <url>

Cập nhật local repo

git pull origin <branch>

Về user name và email

Xem user name hoặc email

Cách 1: git config <user.name hoặc user.email>

git config user.name

Cách 2: git config --list

Git graph

git log --oneline --decorate --graph --all

Git stash

git stash giúp bạn "giữ lại" những thay đổi mà bạn đã tạo ra trên local repo nhưng chưa commit, để bạn có thể chuyển branch hay làm công việc gì khác, sau đó bạn có thể áp nó vào lại

git stash

Lưu ý: git stash chỉ stash:

  • Những thay đổi trên file đã được stage (đã được git add)
  • Những thay đổi trên file chưa được stage nhưng đang được quản lí bởi git (chưa git add, nhưng file đã nằm trong repo từ những lần commit trước)

Và sẽ không stash

  • File mới được tạo chưa nằm dưới sự quản lí của git
  • File bị ignore (bởi .gitignore)
git stash pop
# Reapply stash and removes the changes from stash
git stash apply 
# Reapply stash, keeps the changes in stash 

Docker service

Start

sudo service docker start

hoặc

sudo systemctl start docker

Stop

sudo systemctl stop docker.socket

Status

sudo service docker status

hoặc

sudo systemctl status docker

Docker container

Docker run

docker run -it -d <image_name>

Docker stop

docker stop <container_id>

Docker exec

docker exec -it <container_id> bash

Docker kill

Stop immediately

docker kill <container_id>

Docker rm

Remove stopped container

docker rm <container_id>

Misc

System inspect

docker system df

Remove all image

docker rmi $(docker images -a -q) --force

Remove all volume

docker volume rm $(docker volume list -q) 

Remove image not used

docker image prune

Remove volume not used

docker volume prune

Clear build cache

docker builder prune