Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junaidulqayyumqureshi/914cd104fd2f9cc1c222f2836f4141e1 to your computer and use it in GitHub Desktop.
Save junaidulqayyumqureshi/914cd104fd2f9cc1c222f2836f4141e1 to your computer and use it in GitHub Desktop.
Research & Findings on CLOUD based website deployments, Development snippets & more
@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Apr 13, 2020

LAMP Stack

Source
LAMP Stack

PHP/FPM/FastCGI Issues
https://askubuntu.com/questions/1319861/how-to-configure-apache-http-to-php-fpm-on-ubuntu-20-10

Php Issues
Php Issues regarding repository

Phpmyadmin User Rights
Source

Type in cli:
mysql
When in mysql cli
mysql> GRANT ALL ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
mysql> flush privileges;

PHP 7.4
Source

INSTALL NPM/NODE
NPM & NodeJS

Laravel Permissions after deployment

sudo chmod a+rwx /var/www/projectname/storage/app
sudo chmod a+rwx /var/www/projectname/storage/app/*
sudo chmod a+rwx /var/www/projectname/storage/app/public/*
sudo chmod a+rwx /var/www/projectname/storage/framework
sudo chmod a+rwx /var/www/projectname/storage/framework/*
sudo chmod a+rwx /var/www/projectname/storage/framework/cache
sudo chmod a+rwx /var/www/projectname/storage/framework/cache/* 
sudo chmod a+rwx /var/www/projectname/storage/logs
sudo chmod a+rwx /var/www/projectname/storage/logs/*

Shared Hosting

Route::get('reset', function () {
    \Artisan::call('route:clear');
    \Artisan::call('config:clear');
    \Artisan::call('cache:clear');
    \Artisan::call('storage:link');
    dd("Done");
});

SSH
LARAVEL clear everything

php artisan optimize:clear
php artisan route:clear
php artisan config:clear
php artisan cache:clear
php artisan view:clear

Apache SSL

sudo apt-get update
sudo apt-get install apache2
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-apache
sudo apachectl stop
letsencrypt --authenticator standalone --installer apache -d domain.com -d www.domain.com
sudo service apache2 start
service apache2 restart

For SSL Expiration information
echo | openssl s_client -connect domain.com:443 2>/dev/null | openssl x509 -noout -dates

For redirection from www.
sudo certbot --apache -d example.com -d www.example.com

NGINX Configuration

Source
Nginx Configuration

Source
Another Source

For node app config in nginx/sites-available
Main domain script

server
{

    listen 80 default_server;
    server_name am7.studio www.am7.studio;

    root /var/www/am7.studio; 
    index index.html index.php index.htm index.nginx-debian.html;
    
    location /
    {
    	# proxy_set_header X-Real-IP $remote_addr
        proxy_pass http://localhost:4567;
    }

}

Sub domain script

server
{

    listen 80;
    listen [::]:80;
    
    root /var/www/testam7; 

    server_name test.am7.studio www.test.am7.studio;
    index index.html index.php index.htm index.nginx-debian.html;
    
    location / {
        try_files $uri /index.php;
    }

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

/etc/apache2/apache.conf

Listen 8080
sudo systemctl reload apache2

To verify port on which apache is running
sudo netstat -tlpn

NGINX - SSL

Source
SSL Installation Source


sudo apt install python3-certbot-nginx
sudo certbot --nginx -d am7.studio -d www.am7.studio
sudo ln -s /etc/nginx/sites-available/testam7 /etc/nginx/sites-enabled/testam7

SSL ON NGINX UBUNTU v20
Source

For firewall purposes
Source
Firewall Issue

sudo ufw enable
disconnect SSH

Goto digitalocean droplet console

sudo ufw default allow
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

Any nginx error
sudo tail -30 /var/log/nginx/error.log

For nodejs multi instances

const port = process.argv[2] || 3000;

pm2 start server/server.js --name "server-3000" -- 3000
pm2 start server/server.js --name "server-3001" -- 3001
pm2 start server/server.js --name "server-3002" -- 3002
pm2 start server/server.js --name "server-3003" -- 3003

upstream mynodeapps {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    server 127.0.0.1:3003;
}
server {
    listen 80;
    listen [::]:80;
    server_name node.am7.studio www.node.am7.studio;
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://mynodeapps;
    }
}.

@junaidulqayyumqureshi
Copy link
Author

Unable to find apache-fpm-fork or something

SOF

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install apache2 apache2-doc apache2-utils

@junaidulqayyumqureshi
Copy link
Author

PHP Max File size limit

$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
$memory_limit = (int)(ini_get('max_execution_time'));
$upload_mb = min($max_upload, $max_post, $memory_limit);

Above enteries can be changed from

/etc/php/7.4
For apache:
/etc/php/7.4/apache/php.ini
For fpm:
/etc/php/7.4/fpm/php.ini

To restart service
Apache:
systemctl reload apache2
FPM:
service php7.4-fpm restart

@junaidulqayyumqureshi
Copy link
Author

Phpmyadmin

If phpmyadmin stops responding or doesn't allow login
or error

#2002 - No such file or directory — The server is not responding (or the local server's socket is not correctly configured).
mysqli_real_connect(): (HY000/2002): No such file or directory
Connection for controluser as defined in your configuration failed.

Screenshot:
image

/etc/phpmyadmin/config.inc.php

Fix#1
Replace this code:

if (empty($dbserver)) $dbserver = 'localhost';

$cfg['Servers'][$i]['host'] = $dbserver;

if (!empty($dbport) || $dbserver != 'localhost') {
    $cfg['Servers'][$i]['connect_type'] = 'tcp';
    $cfg['Servers'][$i]['port'] = $dbport;
}

with

if (empty($dbserver)) $dbserver = '127.0.0.1';

$cfg['Servers'][$i]['host'] = $dbserver;

if (!empty($dbport) || $dbserver != '127.0.0.1') {
    $cfg['Servers'][$i]['connect_type'] = 'tcp';
    $cfg['Servers'][$i]['port'] = $dbport;
}

Fix#2
Maybe your SQL server has been stopped

sudo /etc/init.d/mysql start
or
sudo service mysqld start

and useservice mysql status to check status

UPGRADE PHPMYADMIN

@junaidulqayyumqureshi
Copy link
Author

Restart Ubuntu Server (Digital Ocean droplet)

sudo shutdown -r now

Setup pm2 processes to start if system restarts

const port = process.argv[2] || 3000;
pm2 start server/server.js --name "server-3000" -- 3000
pm2 start server/server.js --name "server-3001" -- 3001
pm2 start server/server.js --name "server-3002" -- 3002
pm2 start server/server.js --name "server-3003" -- 3003

After starting all the processes which you want after startup

pm2 startup ubuntu 
pm2 save

@junaidulqayyumqureshi
Copy link
Author

PHPMyAdmin 413 Request Entity Too Large

nginx/sites-available/apache

server {
    listen 80;
    server_name 139.59.37.163;

    location / {
        client_max_body_size 1024M;
    }
}

@junaidulqayyumqureshi
Copy link
Author

Angular Website Refresh Page Not Found NGINX

location / {
        try_files $uri $uri/ /index.html?$args;
    }

@junaidulqayyumqureshi
Copy link
Author

If Laravel can't find the class and you are sure that your namespace is correct and class exists then:

composer dumpautoload

@junaidulqayyumqureshi
Copy link
Author

Laravel Custom Auth Modifications

laravel-framework-src-illuminate-Auth-EloquentUserProvider
laravel-framework-src-illuminate-Foundation-Auth-AuthenicatesUsers.php

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Nov 17, 2020

Create a new FTP user and Specify one directory permission

sudo adduser yourftpuser
sudo usermod -d /your/path/for/the/user yourftpuser
sudo chown username: myfolder (To give permission of a folder to user)

Two more commands:

chown -R username directory
chmod -R u+rX directory

The first command makes the user own the directory. The second command gives them full read and access permissions. The r gives read permission, the X gives 'execute' permission to directories, and not files.

Source
Source

@junaidulqayyumqureshi
Copy link
Author

Allowed Memory Size Limit Exceeded/Exhausted
COMPOSER_MEMORY_LIMIT=-1 composer require huddledigital/zendesk-laravel

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Dec 23, 2020

GRANT REMOTE ACCESS TO USER

Backup /etc/mysql/mysql.conf.d/mysqld.cnf file and edit the original file

Navigate to the line that begins with the bind-address directive. It will look like this:

lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
bind-address            = 0.0.0.0

Add bind-address = 0.0.0.0 & restart mysql service with sudo systemctl restart mysql

DigitalOceanSource

RUN ALL BELOW FOR 1 USER
1- CREATE USER 'sendy'@'localhost' IDENTIFIED BY 'buzTq7DdgEbhP33a';
2- GRANT ALL PRIVILEGES ON . TO 'sendy'@'localhost' WITH GRANT OPTION;
3- CREATE USER 'sendy'@'%' IDENTIFIED BY 'buzTq7DdgEbhP33a';
4- GRANT ALL PRIVILEGES ON . TO 'sendy'@'%' WITH GRANT OPTION;
5- FLUSH PRIVILEGES;

StackOverflow

@junaidulqayyumqureshi
Copy link
Author

Change MYSQL/Phpmyadmin Password

SET PASSWORD FOR root@localhost = PASSWORD('Password');

@junaidulqayyumqureshi
Copy link
Author

Change Server Root Password

sudo passwd root

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Feb 10, 2021

Check Server Load and Usage

Source

top -c command shows the memory and cpu usage by each application/script

nproc shows number of processors available

free -h shows the RAM memory and show how much is used and how much remains

/var/log/mysql will have a file mysql-slow.log which will have logs of query took minutes which are mentioned in mysqld.cnf file

@junaidulqayyumqureshi
Copy link
Author

Angular Cache Reset for deployment

For Ang 10:
ng build --prod --aot --outputHashing=all

For <Ang 10:
ng build --prod --aot --output-hashing=all

@junaidulqayyumqureshi
Copy link
Author

Recursively Search String in files/folders

grep -Rw stringToSearch /var/www/project/*;

Recursively Replace string in files/folders

find /var/www/project/ -name \*.php -exec sed -i "s/stringToFind/stringToReplace/g" {} \;

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Aug 18, 2021

Timezone Change

To change timezone
timedatectl set-timezone Asia/Karachi

To get timezone info:
timedatectl

systemctl restart mysql
systemctl reload apache2
systemctl restart apache2

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Aug 18, 2021

Disable MYSQL ONLY FULL GROUP BY

Goto /etc/mysql & search for my.cnf whether shortcut of file, open terminal and type:

nano /etc/mysql/my.cnf
& append below includedirs:

[mysqld]  
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Oct 1, 2021

Source

PAID SSL (GoDaddy etc) - Not Lets Encrypt

Create a directory & adjust rights:

mkdir /etc/nginx/ssl
sudo chmod -R 600 /etc/nginx/ssl

Generate 2 keys, 1 example.com.key & 2 for example.com.csr

openssl req -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr

Fill in the informations or skip by pressing enter, except Common Name FQDN etc. Provide your website name i.e example.com

2 files will be generated:

1- example.com.key
2- example.com.csr

Provide content of this csr file by copying > cat example.com.csr and provide to SSL provider.
Once SSL zip file is downloaded from SSL Provider. Unzip it and rename with following:

1- randomnumber.crt to example.com.crt
2- xxxbundle.crt to intermediate.crt

Copy both the files to /etc/nginx/ssl

run command:

cat example.com.crt intermediate.crt > example.com.chained.crt

Then change sites-available/site to following:


server {
    root /var/www/site;
    index index.php index.html index.htm;
    client_max_body_size 120M;

    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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



    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/nginx/ssl/example.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    }


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }
    server_name example.com www.example.com;
    listen 80;
    return 404; # managed by Certbot
}

Test nginx & restart by:

nginx -t
systemctl restart nginx

@junaidulqayyumqureshi
Copy link
Author

Check processes running linux:

ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head

@junaidulqayyumqureshi
Copy link
Author

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented Sep 1, 2022

Setting up another website in a subdirectory

server {
    root /var/www/example.com;
    index index.php index.html index.htm;
    client_max_body_size 120M;

    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ /index.html?$query_string;
    }

    location /blog {
        root /var/www/; //Make sure that project directory is named exactly as "location /blog" i.e. /var/www/blog
        
        try_files $uri $uri/ /blog/index.php?$args;
 
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }

}

@junaidulqayyumqureshi
Copy link
Author

Deployment on DigitalOcean Apps Platform of a docker react app

1- Create file named "Dockerfile" (Without any extension)
2- Create folder called .docker on root and create a file in it with name "prod.Dockerfile"

Copy below content in both the files

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory to /app
WORKDIR /build

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install the dependencies
RUN npm install

# Install the dependencies
RUN npm install -g serve

# Copy the remaining application files to the container
COPY . .

# Build the application
RUN npm run build

# Set the production environment variable
ENV NODE_ENV=production

# Expose port 3000
EXPOSE 3000

# Start the application
CMD ["serve", "-s", "build"]

3- After that, create an app on app platform and choose this repo

4- Remove extra webservice instances, if any, other than Docker WebService.

5- Define a port i.e. 3000 in settings

6- Define start and build scripts in package.json i.e. react-scripts start

Voila!!!

@junaidulqayyumqureshi
Copy link
Author

Recover EC2 instance & Connect without key pair

https://www.youtube.com/watch?v=5btWXn4yWzQ

@junaidulqayyumqureshi
Copy link
Author

Permission Denied EC2 CodeCommit

sudo chown -R ubuntu .git/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment