{
  "slug": "Install-CakePHP-application-on-server-with-the-traditional-way-on-Ubuntu-22-0-server-3228ae49c3c1",
  "title": "Install CakePHP application on server with the traditional way on Ubuntu 22.0 server",
  "subtitle": "In this article, I want to make a Monolithic Server to run the Cakephp application. I know that Microservices and central systems are…",
  "excerpt": "In this article, I want to make a Monolithic Server to run the Cakephp application. I know that Microservices and central systems are…",
  "date": "2022-12-04",
  "tags": [
    "Linux",
    "Microservices",
    "Architecture"
  ],
  "readingTime": "5 min",
  "url": "https://medium.com/@mobinshaterian/install-cakephp-application-on-server-with-the-traditional-way-on-ubuntu-22-0-server-3228ae49c3c1",
  "hero": "https://cdn-images-1.medium.com/max/800/1*Yo30HmX_znWbnvuEbG1vEw.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Install CakePHP application on server with the traditional way on Ubuntu 22.0 server"
    },
    {
      "type": "paragraph",
      "html": "In this article, I want to make a Monolithic Server to run the Cakephp application. I know that Microservices and central systems are ubiquitous today. Still, this company has only one developer, and they don’t have any DevOps team, so I prefer to use the VPS server as a Monolithic server."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Yo30HmX_znWbnvuEbG1vEw.png",
      "alt": "Install CakePHP application on server with the traditional way on Ubuntu 22.0 server",
      "caption": "",
      "width": 1024,
      "height": 576
    },
    {
      "type": "paragraph",
      "html": "I started my configuration based on the below article and then changed my design based on business needs."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nginx"
    },
    {
      "type": "paragraph",
      "html": "Absolutely, we need Nginx to handle large amounts of TPS. on my expression Nginx is hardly robust and can handle a million requests per second and serve server without any problem."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt install nginx"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*hXuuhny2NRwVopbkfz5caw.png",
      "alt": "https://www.freecodecamp.org/news/an-introduction-to-nginx-for-developers-62179b6a458f/",
      "caption": "https://www.freecodecamp.org/news/an-introduction-to-nginx-for-developers-62179b6a458f/",
      "width": 800,
      "height": 465
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mysql"
    },
    {
      "type": "paragraph",
      "html": "Honestly, Mysql or MariaDB is perfect for this project. They maximum has 10K records each year, so they don’t have a severe problem with Mysql."
    },
    {
      "type": "paragraph",
      "html": "How do I handle the below bug whenever I want to run Mysql?"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.nixcraft.com/t/mysql-failed-error-set-password-has-no-significance-for-user-root-localhost-as-the-authentication-method-used-doesnt-store-authentication-data-in-the-mysql-server-please-consider-using-alter-user/4233\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server.</a>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo mysql"
    },
    {
      "type": "code",
      "lang": "sql",
      "code": "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "mysql -u root -pSHOW DATABASES;"
    },
    {
      "type": "paragraph",
      "html": "I use Mysql Extention in Vscode because I want to connect directly via the ssh tunnel to the Mysql tables."
    },
    {
      "type": "paragraph",
      "html": "In another way, we can use ssh on the server and write the direct command in the bash console."
    },
    {
      "type": "code",
      "lang": "sql",
      "code": "CREATE DATABASE db_name;\n\nUSE db_name;\n\nSHOW TABLES;"
    },
    {
      "type": "paragraph",
      "html": "For loading SQL files into the database, easily upload SQL files via FileZilla and then load with the below command."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "mysql -u username -p database_name < file.sql"
    },
    {
      "type": "paragraph",
      "html": "If you want to delete the database using the below command. Be careful to use this command; the database will be terminated after this command."
    },
    {
      "type": "code",
      "lang": "sql",
      "code": "DROP DATABASE [IF EXISTS] database_name;"
    },
    {
      "type": "paragraph",
      "html": "If you use “Group by” in your queries, you will need this command to activate Group by for the query."
    },
    {
      "type": "code",
      "lang": "sql",
      "code": "SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Make sure Mysql DB is secure."
    },
    {
      "type": "paragraph",
      "html": "Security is an essential part of all Databases, According to the following command, Mysql will disconnect the anonymous connection and force you to set up a strong password for the DB."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo mysql_secure_installation"
    },
    {
      "type": "paragraph",
      "html": "PHP 7.4 is so old, and it was deprecated but but my old code only runs on PHP 7.4&nbsp;:(("
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install PHP 7.4"
    },
    {
      "type": "paragraph",
      "html": "The way of installing PHP 7.4 on the server has been written below. You don’t need to tell me PHP is deprecated and this application was about five years ago."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get update\nsudo\napt -y install software-properties-common\nsudo add-apt-repository ppa:ondrej/php\nsudo\napt-get update\nsudo\napt install php7.4-fpm php-mysql"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nginx Folder"
    },
    {
      "type": "paragraph",
      "html": "For running Nginx, it is necessary to make a file and use GIT to pull from the server. As I mentioned, we don’t have a DevOps team to run a pipeline for this problem."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\nmkdir /var/www/your_domain\nsudo\nchown -R $USER:$USER /var/www/your_domain\nsudo\nchown -R www-data:www-data /var/www/your_domain\nsudo vim /etc/nginx/sites-available/your_domain"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "add ssh key to server"
    },
    {
      "type": "paragraph",
      "html": "Instead of using a CI/CD pipeline to deploy the system, we use a traditional way to pull code from the Main branch into the Monolotic server. This method has many disadvantages, but it’s a good solution whenever I have only one developer."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "cat ~/.\nssh/"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "ssh-keygen"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "cd /home/user/.sshcat id_rsa.pub"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "ssh -T git@gitlab.com"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "git add user name and email"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "git config --global user.name \"FIRST_NAME LAST_NAME\""
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "git config --global user.email \"MY_NAME@example.com\""
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nginx Configuration"
    },
    {
      "type": "code",
      "lang": "nginx",
      "code": "server {\nlisten 90;\nserver_name domain.com www.domain.com;\nroot /var/www/path/webroot;\ncharset utf-8;\nlocation / {        add_header 'Access-Control-Allow-Origin' '*' always;        proxy_set_header\nHost $host;\nindex index.php;\ntry_files $uri $uri/ /index.php?$args;    }        # Static files.    # Set expire headers, Turn off\naccess log    location ~* \\favicon.ico$ {      access_log off;      expires 1d;      add_header\nCache-Control public;    }    location ~ ^/(img|cjs|ccss)/ {      access_log off;      expires 7d;\nadd_header Cache-Control public;    } # Deny access to .htaccess files,    # git & svn repositories,\netc    location ~ /(\\.ht|\\.git|\\.svn|\\.ini|\\.log|\\.conf) {      deny  all;    }    location ~ \\.php$\n{        include snippets/fastcgi-php.conf;        fastcgi_pass unix:/run/php/php7.4-fpm.sock;\n}}"
    },
    {
      "type": "paragraph",
      "html": "Activate your configuration by linking to the configuration file from Nginx’s <code>sites-enabled</code> directory:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/"
    },
    {
      "type": "paragraph",
      "html": "Then, unlink the default configuration file from the <code>/sites-enabled/</code> directory:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo unlink /etc/nginx/sites-enabled/default"
    },
    {
      "type": "paragraph",
      "html": "This will tell Nginx to use the configuration next time it is reloaded. You can test your configuration for syntax errors by running the following:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo nginx -t"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file\n/etc/nginx/nginx.conf test is successful"
    },
    {
      "type": "paragraph",
      "html": "Your new website is now active, but the web root <code>/var/www/your_domain</code> is still empty. Create an <code>index.html</code> file in that location so that you can test that your new server block works as expected:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo systemctl reload nginx"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install composer"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://computingforgeeks.com/install-cakephp-framework-on-ubuntu/\" target=\"_blank\" rel=\"noreferrer noopener\">https://computingforgeeks.com/install-cakephp-framework-on-ubuntu/</a>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "composer installcomposer update"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "CakePHP"
    },
    {
      "type": "paragraph",
      "html": "copy app.php in config file"
    },
    {
      "type": "paragraph",
      "html": "Make tmp directory&nbsp;, and cache and grant full permission to them."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Cakephp commands"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "bin/cake cache clear_allbin/cake migrations migrate"
    },
    {
      "type": "paragraph",
      "html": "Makeing API in Cakephp"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "bin/cake bake all productdetails  --prefix adminbin/cake bake all banners  --prefix admin"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "How to Install php-curl in Ubuntu ?"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get install php7.4-curl\nsudo\napt-get install php7.4-intl\nsudo\napt-get install php7.4-mbstring\nsudo\napt-get install php7.4-gd\nsudo\napt install php7.4-xml\nsudo\napt-get install php-mbstring\nsudo\napt-get install php7.4-common\nsudo\napt-get install php7.4-zip\nsudo\napt-get install php7.4-fpm php7.4-cli php7.4-mysql php7.4-\ncurl php7.4-json -y"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Unistall php"
    },
    {
      "type": "paragraph",
      "html": "only just want to clear PHP version and install another version"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get purge 'php*'"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Cronjob"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "crontab -ecrontab -l"
    },
    {
      "type": "code",
      "lang": "properties",
      "code": "CRON_TZ=Europe/London* * * * * /var/www/path/bin/cake command_name >> /var/www/path/ex1.log 2>&1*/5\n* * * * /var/www/path/bin/cake command_name >> /var/www/path/ex2.log 2>&1"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Transfer Data from one server to another server"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "scp -P 44 path/to/local/file.ext user@remote-host:path/to/remote/file.ext\nscp -r source\nuser@target:dest\nscp -P port -r  ./uploads root@ip:/var/www/uploads"
    },
    {
      "type": "paragraph",
      "html": "<strong>change all files permission to 644 and 755</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "find  ./path  -type f -exec chmod 644 '{}' \\;\nsudo find ./path -type d -exec chmod 755 {} \\;\nsudo\nchmod 777 -R tmp\nsudo chmod 777 -R logs"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Git Discard"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "git status\ngit stash save --keep-index --include-untracked"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "git reset <file>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Anti virus ClamAV on Ubuntu"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Cronjob backup database server"
    },
    {
      "type": "paragraph",
      "html": "base on below articles I made cronjob for backup system then I copy via scp to another server."
    },
    {
      "type": "paragraph",
      "html": "I change script.sh to below commands."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "#!/bin/bashnow=$(date +%d%m%Y-%H:%M:%S)filename=$1backupfilename=$1-$nowmysqldump\n--defaults-file=/root/backup/.my.cnf  elaart  > /root/backup/sql$backupfilename.sqlzip -r\n/root/backup/sql$backupfilename.zip /root/backup/sql$backupfilename.sqlrm\n/root/backup/sql$backupfilename.sql"
    },
    {
      "type": "paragraph",
      "html": "add put my user name and password in&nbsp;.my.cnf file"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "[mysqldump]user=myusernamepassword='MYPASSWoRD'"
    },
    {
      "type": "paragraph",
      "html": "Then add time in cronjob execute this shell script"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "*/10 * * * * /root/backup/script.sh"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Set time zone in Ubuntu"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://linuxize.com/post/how-to-set-or-change-timezone-on-ubuntu-20-04/\" target=\"_blank\" rel=\"noreferrer noopener\">https://linuxize.com/post/how-to-set-or-change-timezone-on-ubuntu-20-04/</a>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "timedatectl set-timezone Asia/Tehran"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Get Free domain"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Secure Nginx with Let’s Encrypt on Ubuntu 22.04"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04</a>"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://community.cloudflare.com/t/redirect-example-com-to-www-example-com/78348\" target=\"_blank\" rel=\"noreferrer noopener\">https://community.cloudflare.com/t/redirect-example-com-to-www-example-com/78348</a>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "root .htaccess: <IfModule mod_rewrite.c>   RewriteEngine on   RewriteRule ^(\\.well-known/.*)$ $1 [L]\nRewriteRule    ^$ app/webroot/    [L]   RewriteRule    (.*) app/webroot/$1 [L]   AddDefaultCharset\nUTF-8 </IfModule>app/.htaccess:<IfModule mod_rewrite.c>    RewriteEngine on    RewriteRule\n^(\\.well-known/.*)$ $1 [L]    RewriteRule    ^$    webroot/    [L]    RewriteRule    (.*)\nwebroot/$1    [L]    AddDefaultCharset UTF-8  </IfModule>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo certbot renew --dry-run"
    },
    {
      "type": "paragraph",
      "html": "SMPT Run on server"
    }
  ]
}
