Skip to content

Instantly share code, notes, and snippets.

@mufid
Last active October 24, 2020 09:28
Show Gist options
  • Save mufid/f879fa5e5accf4dcc6fb75f0312d525c to your computer and use it in GitHub Desktop.
Save mufid/f879fa5e5accf4dcc6fb75f0312d525c to your computer and use it in GitHub Desktop.
NginxPhp1804.md

Memindahkan Pemasangan Wordpress (Ubuntu 18.04)

Cara-cara:

  • Siapkan mesin baru
  • Backup data dari mesin lama
  • Restore data ke mesin baru
  • Done

Menyiapkan Mesin Baru

Sebelum memulai, pastikan DNS sudah propagate ke mesin baru.

Spin up mesin baru kemudian perbaru dulu

# apt-get update
# apt-get upgrade

Lakukan instalasi Maridb

# apt-get install mariadb-server

Konfigurasikan Mariadb ke default yang aman:

# mysql_secure_installation

Lakukan instalasi Certbot

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot python-certbot-nginx 

Instalasi nginx SSL dari Certbot:

$ sudo certbot --nginx

Lakukan instalasi PHP 7.2 FPM:

# sudo apt install php7.2-fpm && \
                   php7.2-common && \
	       php7.2-mbstring && \
	       php7.2-xmlrpc && \
	       php7.2-gd && \
	       php7.2-xml && \
	       php7.2-mysql && \
	       php7.2-cli && \
	       php7.2-zip && \
	       php7.2-curl && \
	       php7.2-bcmath && \
	       php-imagick

Sunting konfigurasi /etc/nginx/sites-enabled/default menjadi sebagai berikut. Ganti yoursite.com menjadi FQDN situs web Anda.

server {
    root /mnt/volume_nyc1_01/nginx-sites;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name yoursite.com www.yoursite.com;
    location / {
        try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?$args;
    }
    # pass PHP scripts to FastCGI server
    #

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            # With php-cgi (or other tcp sockets):
            # fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
    }
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #

    location ~ /\.ht {
            deny all;         
    }         

    listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/yoursite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/yoursite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.yoursite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

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


    listen 80;
    listen [::]:80 ;
    return 404; # managed by Certbot
}

Buka /etc/nginx/fastcgi_params, pastikan isinya adalah sebagai berikut:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Backup dari mesin lama

Backup HTML:

$ cd /var/www
$ tar cvf www-backup.tar.gz www

Kirim ke mesin baru:

$ scp www-backup.tar.gz root@mesinbaru:~

Backup SQL

$ mysqldump -u [user] -p [database_name] > mysqlbackup.sql

Kirim ke mesin baru:

$ scp mysqlbackup.sql root@mesinbaru:~

Restore Mesin Baru

Extract dan salin arsip HTML:

$ tar xvf www-backup.tar.gz
$ cp html /var/www -r
$ cd /var/www
$ chown www-data:www-data www -r

Buat database wordpress-nya:

$ mariadb
mariadb> CREATE DATABASE databasenya;

Buat akun Mariadb:

$ mariadb
mariadb> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'passwordyangdiinginkan';
mariadb> GRANT ALL PRIVILEGES ON databasenya.* TO 'newuser'@'localhost';
mariadb> FLUSH PRIVILEGES;

Restore database:

$ mysql -u newuser -p databasenya < mysqlbackup.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment