Thumbnail
Category: Server

Build Server Ubuntu A-Z

Date: July 1, 2024
28 views


1. Cài Nginx

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04

sudo apt update
sudo apt install nginx
=>Nhấn Y
sudo ufw app list
sudo ufw allow 'Nginx HTTP'
sudo ufw status
sudo ufw allow ssh


Chạy lên thử sẽ thấy trang welcome nginx

http://your_server_ip

sudo mkdir -p /var/www/your_domain
sudo chown -R www-data:www-data /var/www/your_domain/
sudo chmod -R 755 /var/www/your_domain

sudo vi /var/www/your_domain/index.html

add nội dung bên dưới

<html>
  <head>
    <title>Welcome to your_domain!</title>
  </head>
  <body>
    <h1>Success! The your_domain server block is working!</h1>
  </body>
</html>


sudo vi /etc/nginx/sites-available/your_domain


Change root and server_name

server {
    listen 80;
    listen [::]:80;

    root /var/www/your_domain/html;
    index index.html index.htm index.nginx-debian.html;

    server_name your_domain www.your_domain;

    location / {
        try_files $uri $uri/ =404;
    }
}


sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

sudo vi /etc/nginx/nginx.conf

Find the server_names_hash_bucket_size directive and remove the # symbol

sudo nginx -t

sudo systemctl restart nginx

2. Cài PHP 8.1

https://www.digitalocean.com/community/tutorials/how-to-install-php-8-1-and-set-up-a-local-development-environment-on-ubuntu-22-04

sudo apt update
sudo apt install --no-install-recommends php8.1
=> Chọn Y

php -v

sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

php -m

sudo apt install php8.1-fpm

3. Cài Composer

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

composer

4. Cài Git

https://www.digitalocean.com/community/tutorials/how-to-install-git-on-ubuntu-22-04

Install git:
sudo apt update
sudo apt install git
git --version

5. Change nginx

sudo vi /etc/nginx/sites-available/your_domain


server {
  listen 80;
  server_name your_domain;
  root /var/www/;

  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;

  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/php8.1-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location ~ /\.(?!well-known).* {
    deny all;
  }
}


sudo nginx -t
sudo systemctl restart nginx


6. Cài MySQL

https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04

sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql.service

sudo mysql_secure_installation
=> Chọn 2

sudo MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456aA@';
exit
mysql -u root -p

7. Cài PHPMyadmin

https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-20-04

sudo apt update
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
=> Chọn Apache
mysql -u root -p
UNINSTALL COMPONENT "file://component_validate_password";
exit
sudo apt install phpmyadmin


add thêm vào config:

sudo vi /etc/nginx/sites-available/your_domain


server {
  listen 8080;
  server_name 34.64.145.16;

  root /usr/share/phpmyadmin;
  index index.php index.html index.htm;

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  # Other phpMyAdmin configurations...
}

8. Check log

tail /var/log/nginx/error.log

Copyright © 2025 All Right Reserved