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

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