In this tutorial, i build vps for subdomain api.example.com
1. Access your Vps
ssh user@ip
2. Install PHP 8.1
sudo add-apt-repository ppa:ondrej/php -y
sudo apt install -y php8.1 php8.1-{cli,gd,curl,mysql,ldap,zip,fileinfo,fpm,xml,mbstring,exif,pspell,imagick,bcmath}
# or install one by one modules
sudo apt install -y php8.1 php8.1-curl php8.1-mysql
# cmd for check status php
sudo nano /etc/php/8.1/fpm/php.ini
sudo systemctl restart php8.1-fpm
3. Install composer
sudo wget -O composer-setup.php https://getcomposer.org/installer
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
4. Clone, build Laravel app
- install laravel project
composer create-project --prefer-dist laravel/laravel api.example.com
cd api.example.com
php artisan
nano .env
sudo mv ~/api.example.com /var/www/api.example.com
sudo chown -R www-data.www-data /var/www/api.example.com/storage
sudo chown -R www-data.www-data /var/www/api.example.com/bootstrap/cache
- Provide access for composer and install packages
sudo chmod -R 775 /var/www/api.example.com/storage
sudo chmod -R 775 /var/www/api.example.com/bootstrap/cache
- create file .env
cp -a .env.example .env
nano .env
php artisan key:generate
4. Config Laravel app in Nginx
- I consume you installed Nginx
- Set Up Nginx Server Blocks (Virtual Hosts) for api.example.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/api.
example.comsudo nano /etc/nginx/sites-available/api.example.com
- Config for /etc/nginx/sites-available/api.example.com
server {
listen 80;
listen [::]:80;
server_name api.example.com;
root /var/www/api.example.com/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;
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;
fastcgi_read_timeout 3600;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
* Now that we have our server block file,
we need to enable it. We can do this by creating symbolic link from
this file to the sites-enabled directory, which Nginx reads from during
startup.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Check nginx config status
# Check NGINX config site-avaiablesudo nginx -t
# Restart NGINX
sudo service nginx restart
7. Add record for domain
* This config for subdomain api.example.com
| Host |
Type |
Value |
TTL
|
api
|
A
|
your-vps-ip
|
3600
|
8. Using Supervisor (process manager for Linux)
- file supervisord.conf
[unix_http_server]
file=/run/supervisord.sock
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///run/supervisord.sock
[program:php-fpm]
command=/usr/local/sbin/php-fpm -F
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# stdout_events_enabled=true
# stderr_events_enabled=true
[program:nginx]
command=/usr/sbin/nginx -g 'daemon off;'
autostart=true
autorestart=true
priority=10
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# stdout_events_enabled=true
# stderr_events_enabled=true
[include]
files = /etc/supervisor.d/*.conf
9. Add SSL with LetsEncrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d api.example.com
# check certbot auto renew
sudo systemctl status certbot.timer
# or manually renew ssl (90 day will expired)
sudo certbot renew --dry-run
* more option features
(1) install PostgreSQL
# install pgsql extension for php8.1
sudo apt-get install php8.1-pgsql
# install postgresSql
sudo apt update
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list > /dev/null
sudo apt update
sudo apt install postgresql-14
sudo ss -atnp | grep 5432
dpkg -l | grep postgresql
sudo systemctl restart postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql
sudo -u postgres psql --version
# using postgre
sudo -i -u postgres
sudo -u postgres psql
sudo -u postgres createuser --interactive
sudo -u postgres createdb sammy
# example create myUser & myDB
sudo -u postgres psql
CREATE USER myUser WITH PASSWORD 'myPassword';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser
# show all DB, all users
\l
\du
# export db
pg_dump -U postgres -d {dbname} -F p -f /{path-to-db}y/DB_backup.sql
# copy file to local
rsync -avz {user}@{IP}:/{path-to-db}y/DB_backup.sql /{path_to_store}
Thank you.
No comments:
Post a Comment