Có thể cài đặt môi trường để deploy và optimize website là những việc mà mỗi lập trình viên nên biết và cần phải biết.
Hôm nay clipdoisong sẽ hướng dẫn các bạn từng bước cài môi trường LAMP để deploy website.
Bước 1: Tạo user deploy và phân quyền
adduser {username} # tạo mới user deploy passwd {username} # Tạo mật khẩu cho user usermod -aG sudo {username} # Gán quyền sudo cho user
Bước 2: Tạo hostname
hostnamectl set-hostname
Bước 3: Cài đặt timezone
sudo timedatectl set-timezone Asia/Ho_Chi_Minh
Bước 4: Thực hiện cài đặt apache
sudo apt update -y && apt upgrade -y sudo apt install apache2
Bước 5: Cài đặt mysql
sudo apt install -y mariadb-server mariadb-client
Thực hiện cấu hình security cho mysql bằng lệnh (Khi thực hiện cần tạo mật khẩu user root của mysql):
sudo mysql_secure_installation
Tạo user mysql và phân quyền
sudo mysql -u root CREATE DATABASE examle_database CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; CREATE USER 'examl_user'@'localhost' IDENTIFIED BY 'examl_pass'; GRANT ALL PRIVILEGES ON examle_database .* TO 'examl_user'@'localhost'; quit;
Bước 6: Cài đặt PHP
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt install -y php8.1 libapache2-mod-php8.1 // cài đặt các extension cần thiết sudo apt install php-net-ldap2 php-net-ldap3 php-imagick php8.1-common php8.1-gd php8.1-imap php8.1-mysql php8.1-curl php8.1-zip php8.1-xml php8.1-mbstring php8.1-bz2 php8.1-intl php8.1-gmp php8.1-redis
Bước 7: Cấu hình apache
sudo a2enmod rewrite sudo ufw app info "Apache Full" sudo ufw allow in "Apache Full" sudo systemctl restart apache2
Bước 8: Pull source code và tạo file apache config
- Upload/Pull source code của bạn về thư mục /var/www/example_website
- Tạo file example.conf trong thư mục /etc/apache2/sites-available với nội dung
<VirtualHost *:80> ServerAdmin webmaster@example_website.vn ServerName example_website.vn ServerAlias example_website.vn DocumentRoot /var/www/example_website # If the requested resource doesn't exist, use index.html RewriteRule ^ /index.html ErrorLog ${APACHE_LOG_DIR}/example_website.vn.error.log CustomLog ${APACHE_LOG_DIR}/example_website.vn.access.log combined <Directory "/var/www/example_website/"> AllowOverride All Allow from All Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, POST, PATCH, PUT, DELETE, OPTIONS" Header set Access-Control-Allow-Headers "Origin, Content-Type, X-Auth-Token" </Directory> </VirtualHost>
- Thực hiện cài đặt SSL
sudo add-apt-repository ppa:certbot/certbot sudo apt install python-certbot-apache sudo certbot --apache -d example_website.vn // chọn các option để kích hoạt ssl
Chúc các bạn thành công!