Deploy Laravel Nginx Mysql Adminer
Deploy Laravel Nginx Mysql Adminer
0. Super user:
Chuyển super user:
sudo us
Thoát:
exit
1. Install php 7.4:
sudo apt update && sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt install php7.4
php -v
sudo apt install php7.4-cli php7.4-fpm php7.4-common php7.4-imap php7.4-redis php7.4-xml php7.4-zip php7.4-mbstring
Kiểm tra:
php -m
Start and Enable PHP-FPM:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
//systemctl status php*-fpm.service
2. Installing nginx:
sudo apt update
sudo apt install nginx
sudo nginx -v
Firewall:
sudo ufw app list
Output:
Available applications: Nginx
Full
Nginx
HTTP
Nginx
HTTPS
OpenSSH
sudo ufw allow 'Nginx HTTP'
Checking:
sudo ufw status
Kiểm tra trạng thái Nginx:
sudo systemctl status nginx
Run app:
=> http://server_domain_or_IP
Tắt Nginx:
sudo service nginx stop
Bật Nginx:
sudo service nginx start
Reload nginx:
sudo systemctl reload nginx
To re-enable the service to start up at boot, you can type:
sudo systemctl enable nginx
3. domain
sudo mkdir -p /var/www/your_domain/html
echo $USER hoặc kiểm tra ls -l:
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain
echo "<h1>Hello</h1>" > /var/www/your_domain/html/index.html
vi /etc/nginx/nginx.conf để xem nó trỏ vào file sites-available hay file sites-enabled.
Giả sử không link mà trỏ thẳng.
sudo vi /etc/nginx/sites-enabled/your_domain
server {
listen 80;
server_name your_domain www.your_domain;
root /var/www/your_domain;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Reload lai:
sudo nginx -t
sudo service nginx reload
4. mysql
sudo apt update
sudo apt install mariadb-server
sudo systemctl start mysql
sudo systemctl status mysql
sudo mysql_secure_installation
sudo mysql hoặc mysql -u root -p
SHOW DATABASES;
CREATE DATABASE aaa;
CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
Kiểm tra người dùng có tồn tại và có quyền truy cập không:
SELECT User, Host FROM mysql.user WHERE User = ‘aaa’;
Kiểm tra quyền truy cập của người dùng:
SHOW GRANTS FOR 'aaa'@'localhost';
server {
listen 80;
server_name server_domain_or_IP;
root /var/www/travellist/public;
#add_header X-Frame-Options "SAMEORIGIN";
#add_header X-XSS-Protection "1; mode=block";
#add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
client_max_body_size 128M;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
error_log /var/log/nginx/laravel-app-error.log;
access_log /var/log/nginx/laravel-app-access.log;
}
// cài extension php nếu cần thiết
sudo apt install php7.4-mysqli php7.4-pdo
sudo systemctl restart nginx
File log của nginx:
/var/log/nginx
4. adminer:
sudo apt install adminer
5. deploy:
FileZilla:
Tải adminer từ trang chủ về. Đưa vào public và up file lên.
Git:
sudo apt install git
git clone link:
Sau đó:
cp .env.example .env
php artisan key:generate
6. 413 Entity Too Large Nginx:
vi /etc/nginx/nginx.conf
add this to http:
client_max_body_size 128M;
và, nếu chưa chạy:
vi /etc/nginx/sites-enabled/your_domain
add this to server:
client_max_body_size 128M;
sudo nginx -t
sudo service nginx reload
7. Upload max size:
Edit: ...php/7.4/cli/php.ini
Edit: ...php/7.4/fpm/php.ini
upload_max_filesize = 128M;
post_max_size = 128M;
# if php-fpm is used for processing php
sudo service php7.4-fpm restart
# for nginx
sudo service nginx restart
8. Vite (React) frontend - Laravel api backend deploy Ubuntu:
Vite (React) config:
vi /etc/nginx/sites-enabled/your_domain_vite
server {
listen 80;
server_name your_domain;
root /var/www/your_domain/vite/dist;
index index.html index.htm index.nginx-debian.html;
client_max_body_size 128M;
location / {
try_files $uri.html $uri $uri/ /index.html;
}
error_log /var/log/nginx/vite-app-error.log;
access_log /var/log/nginx/vite-app-access.log;
}
Laravel api backend config:
vi /etc/nginx/sites-enabled/your_domain_laravel
server {
server_name api.your_domain;
.....
}
9. ssl:
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx / mail, y, y, 1 hoặc....
10. backup data:
sudo apt install rclone
rclone config
new: n
name: xxx
google drive: 17
enter to empty
enter to empty
Comma separated list of scopes that rclone should use when requesting access from drive: 1
Edit advanced config? n
Use web browser to automatically authenticate rclone with remote? n
Token and cmd.
Tải rclone và cài đặt vào máy pc.
Sau đó nhập cmd vào pc.
Paste token vào server.
Configure this as a Shared Drive (Team Drive)? n
Keep this "a" remote? y.
rclone lsl xxx: hiện danh sách liên kết google drive.
rclone lsl xxx:/aaa hiện danh sách liên kết google drive ở trong thư mục aaa.
Tham khảo:
sudo mkdir backup
sudo chown aa:aa backup
sudo mysqldump xxx | gzip > backup/xxx_backup_$(date +"%d_%m_%Y_%H_%M_%S").gz
rclone copy backup/ backup:/backup_data
sudo rm -R backup/
11. crontab:
Trên cài đặt mặc định, các job của cron được ghi vào: /var/log/syslog
Bạn chỉ có thể thấy cron job trong logfile đó bằng cách chạy: grep CRON /var/log/syslog
Tạo file aaa.sh. Viết script trong file đó.
Cấp quyền execute:
chmod +x /path/to/aaa.sh
Chạy crontab:
crontab -e.
0 0 * * * /usr/local/bin/backup.sh > /dev/null 2>&1
-
/dev/nulllà một tệp ảo trong hệ thống UNIX-like, thường được sử dụng để loại bỏ hoặc gửi dữ liệu đến "đâu đó không gian", nghĩa là nó không lưu trữ bất kỳ dữ liệu nào được gửi đến. -
2>&1có nghĩa là gửi stderr (standard error) đến chỗ mà stdout (standard output) được gửi. Trong trường hợp này, stderr cũng sẽ được gửi đến/dev/null. -
Lịch này là cứ đúng 0 giờ sáng hằng ngày sẽ tự động chạy file backup.sh.
Về cơ bản, điều này có nghĩa là bất kỳ đầu ra hoặc lỗi nào từ tệp lệnh /usr/local/bin/backup.sh cũng sẽ được gửi đến /dev/null, nghĩa là không có đầu ra nào được hiển thị hoặc lưu trữ từ lệnh cron này. Điều này thường được sử dụng để làm sạch đầu ra của lệnh cron và giữ cho các tệp nhật ký không bị lạc hậu bởi các thông báo không cần thiết.
12. connection refused (sau khi chạy npm i khác phiên bản node giữa local và server)
service mysql restart