images | 5 years ago | ||
README.md | 5 years ago |
Dokumentasi untuk Lab Reverse Proxy with Nginx Centos 7
Instalasi & Konfigurasi:
Ketentuan:
# useradd wordpress # useradd prestashop # useradd nextcloud
Kemudian tambahkan host untuk domain kita dengan listen 172.0.0.1 untuk mengarahkan nginx ke apache
# vim /etc/hosts
# yum -y install httpd
Ubah agar apache listen pada 127.0.0.1 dan pada port 8080
# vim /etc/httpd/conf/httpd.conf
Disini kita merubah listen menjadi 127.0.0.1 agar apache tidak bisa diakses dari luar, karena webserver dan reverse proxy dalam 1 server yang sama dan kita merubah port menjadi 8080 agar tidak bentrok dengan nginx yang akan berjalan pada port 80
Tambahkan repo mariadb
# vim /etc/yum.repos.d/mariadb.repo
# MariaDB 10.1 CentOS repository # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
Simpan dan keluar, selanjut nya install mariadb
# yum -y install mariadb-server
Start dan enable MariaDB
# systemctl start mariadb # systemctl enable mariadb
Kemudian setup MariaDB
# mysql_secure_installation
* Enter current password for root (enter for none): Just press the Enter * Set root password? [Y/n]: Y * New password: Enter password * Re-enter new password: Repeat password * Remove anonymous users? [Y/n]: Y * Disallow root login remotely? [Y/n]: Y * Remove test database and access to it? [Y/n]: Y * Reload privilege tables now? [Y/n]: Y
Selanjutnya install php, karena disini kita akan menggunakan 3 php dengan versi berbeda sekaligus, maka kita akan menggunakan php-fpm dan kita akan mengubah setiap port default ketiga php aga bisa berjalan secara bersamaan diwaktu yang sama
Install repositori php remi
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
Lalu install php satu per satu
php 7.1
# yum install php71 php71-php-fpm php71-php-mysql php71-php-pdo php71-php-gd php71-php-mbstring php71-php-mcrypt php71-php-xml php71-php-zip php71-php-intl php71-php-process php71-php-apcu -y
php 7.2
# yum -y install php72 php72-php-fpm php72-php-pdo php72-php-mysqlnd php72-php-opcache php72-php-xml php72-php-gd php72-php-devel php72-php-mysql php72-php-intl php72-php-mbstring php72-php-bcmath php72-php-json php72-php-iconv php72-php-soap php72-php-zip php72-php-mcrypt
php 7.3
# yum -y install php73 php73-php-fpm php73-php-pdo php73-php-mysqlnd php73-php-opcache php73-php-xml php73-php-gd php73-php-devel php73-php-mysql php73-php-intl php73-php-mbstring php73-php-bcmath php73-php-json php73-php-iconv php73-php-soap php73-php-zip php73-php-mcrypt
Konfigurasi php71. , buka www.conf
# vim /etc/opt/remi/php71/php-fpm.d/www.conf
Lalu tambahkan user dan group prestashop setelah apache, disini kita membuat agar webserver dapat mengakses direktory user
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd user = apache user = prestashop ; RPM: Keep a group allowed to write in log dir. group = apache group = prestashop
Kemudian ubah listen port :9000 menjadi :9071
; Note: This value is mandatory. listen = 127.0.0.1:9071
Lakukan untuk kedua user lainnya juga, sesuaikan dengan task
Ubah kepemilikan directory session
chown -R prestashop:prestashop /var/opt/remi/php71/lib/php/session/ chown -R wordpress:wordpress /var/opt/remi/php72/lib/php/session/ chown -R nextcloud:nextcloud /var/opt/remi/php73/lib/php/session/
Ubah settingan selinux dari enforcing menjadi permissive
# setenforce 0
Jalankan Service php
# systemctl start php71-php-fpm # systemctl enable php71-php-fpm # systemctl start php72-php-fpm # systemctl enable php72-php-fpm # systemctl start php73-php-fpm # systemctl enable php73-php-fpm
# yum -y install nginx
Start dan enable Nginx
# systemctl start nginx # systemctl enable nginx
Pertama kita harus mengubah userdir.conf agar apache dapat mengakses user direktory
# vim /etc/httpd/conf.d/userdir.conf
Ubah UserDir Disabled menjadi UserDir Enabled [nama user], jika lebih dari satu user maka tambahkan [nama user] setelah [nama user] sebelumnya. lihat gambar dibawah agar lebih jelas
Kemudian scrol kebawah, di bagian
<Directory "/home/*/public_html"> AllowOverride FileInfo AuthConfig Limit Indexes Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS </Directory>
Hapus public_html, lihat gambar di bawah
Simpan dan keluar, lalu restart service apache
# systemctl restart httpd
Kemudian kita akan membuat virtualhost untuk masing-masing website
1. Virtualhost untuk prestashop
# vim /etc/httpd/conf.d/prestashop.conf
Kemudian isikan
<VirtualHost *:8080> ServerName prestashop.task.my.id ServerAdmin admin@task.my.id DocumentRoot /home/prestashop ErrorLog /home/prestashop/error.log CustomLog /home/prestashop/requests.log combined <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9071" </FilesMatch> <Directory /home/prestashop> <IfModule mod_dir.c> DirectoryIndex index.php index.html index.htm </IfModule> AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost>
Disini saya menggunakan domain prestashop.task.my.id untuk website prestashop nantinya, silakan ubah dengan domain kalian dan jangan lupa untuk menambahkan A record
Kemudian perhatikan SetHandler “proxy:fcgi://127.0.0.1:9071” , disini kita mengarahkan setiap pemrosesan php ke php7.1, ingat tadi pada konfigurasi php7.1 kita mengubah listen port menjadi 9071
2. Virtualhost untuk wordpress
<VirtualHost *:8080> ServerName wordpress.task.my.id ServerAdmin admin@task.my.id DocumentRoot /home/wordpress ErrorLog /home/wordpress/error.log CustomLog /home/wordpress/requests.log combined <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9072" </FilesMatch> <Directory /home/wordpress> <IfModule mod_dir.c> DirectoryIndex index.php index.html index.htm </IfModule> AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost>
Disini kita sesuaikan juga user dan listen portnya menjadi :9072
3. Virtualhost untuk nextcloud
<VirtualHost *:8080> ServerName nextcloud.task.my.id ServerAdmin admin@task.my.id DocumentRoot /home/nextcloud ErrorLog /home/nextcloud/error.log CustomLog /home/nextcloud/requests.log combined <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9073" </FilesMatch> <Directory /home/nextcloud> <IfModule mod_dir.c> DirectoryIndex index.php index.html index.htm </IfModule> AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost>
Jika sudah semua cek apakah ada konfigurasi yang salah
# httpd -t
Restart apache
# systemctl restart httpd
Selanjutnya lakukan pengecekan php menggunakan curl
Pertama buat file index.php di setiap dir root website
# echo "<?php phpinfo();" > /home/nextcloud/test.php # echo "<?php phpinfo();" > /home/wordpress/test.php # echo "<?php phpinfo();" > /home/prestashop/test.php
Selanjut nya ubah kepemilikan dan permission setiap dir root website
# chown -R nextcloud:nextcloud /home/nextcloud # chmod -R 755 /home/nextcloud # chown -R wordpress:wordpress /home/wordpress # chmod -R 755 /home/wordpress # chown -R prestashop:prestashop /home/prestashop # chmod -R 755 /home/prestashop
Cek versi php dengan curl Prestashop
# curl -I prestashop.task.my.id:8080/test.php
Output:
HTTP/1.1 200 OK Date: Sun, 08 Mar 2020 07:27:22 GMT Server: Apache/2.4.6 (CentOS) X-Powered-By: PHP/7.1.33 Content-Type: text/html; charset=UTF-8
Wordpress
# curl -I wordpress.task.my.id:8080/test.php
Output:
HTTP/1.1 200 OK Date: Sun, 08 Mar 2020 07:29:15 GMT Server: Apache/2.4.6 (CentOS) X-Powered-By: PHP/7.2.28 Content-Type: text/html; charset=UTF-8
Nextcloud
# curl -I nextcloud.task.my.id:8080/test.php
Output:
HTTP/1.1 200 OK Date: Sun, 08 Mar 2020 07:30:08 GMT Server: Apache/2.4.6 (CentOS) X-Powered-By: PHP/7.3.15 Content-Type: text/html; charset=UTF-8
Perhatikan pada bagian X-Powered-By: PHP/ pastikan versi php nya sudah cocok dengan yang kita inginkan
Buat server block yang akan di gunakan sebagai reverse proxy yang mengarak pada masing-masing website
1. Prestashop
# vim /etc/nginx/conf.d/prestashop.conf
Kemudian isikan
server { listen 80; server_name prestashop.task.my.id; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://prestashop.task.my.id:8080; } }
2. Wordpress
server { listen 80; server_name wordpress.task.my.id; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://wordpress.task.my.id:8080; } }
3. Nextcloud
server { listen 80; server_name nextcloud.task.my.id; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://nextcloud.task.my.id:8080; } }
Cek apakah ada error pada konfigurasi kita
# nginx -t
Restart nginx
# systemctl restart nginx
Buat file index.html di setiap root dir website untuk pengecekan
# echo "ini directory prestashop" > /home/prestashop/index.html # echo "ini directory wordpress" > /home/wordpress/index.html # echo "ini directory nextcloud" > /home/nextcloud/index.html
Jika sudah lakukan pengecekan kembali dengan curl
# curl -I prestashop.task.my.id
Output:
HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Sun, 08 Mar 2020 08:01:26 GMT Content-Type: text/html; charset=UTF-8 Content-Length: 25 Connection: keep-alive Last-Modified: Sun, 08 Mar 2020 07:41:33 GMT ETag: "19-5a05303fcdfb0" Accept-Ranges: bytes
Perhatikan pada bagian Server, pastikan server nya nginx
lalu coba curl website lainnya juga dan pastikan server nya nginx
# mysql -u root -p
CREATE DATABASE prestashop; GRANT ALL PRIVILEGES ON prestashop.* TO 'prestashopuser'@'localhost' IDENTIFIED BY 'prestashoppass'; FLUSH PRIVILEGES; EXIT;
Buka konfigurasi php.ini
vim /etc/opt/remi/php71/php.ini
Lalu ubah beberapa bagian menjadi
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 64M
Masuk ke dir root prestashop
# cd /home/prestashop
Kemudian hapus file index.html dan test.php yang sudah kita buat
# rm -f index.html test.php
Download prestashop
# curl -O https://download.prestashop.com/download/releases/prestashop_1.7.6.4.zip
Unzip prestashop dan hapus archive
# unzip prestashop_1.7.2.4.zip # rm -f prestashop_1.7.2.4.zip
Ubah permission dan kepilikan directory
# chown -R prestashop:prestashop /home/prestashop # chmod -R 755 /home/prestashop
Setup prestashop dengan mengakses prestashop.task.my.id di browser
Tunggu hingga selesai
Lalu setup seperti biasa
Selamat prestashop berhasil diinstall
# mysql -u root -p
CREATE DATABASE wordpress; GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'wordpresspass' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
Buka konfigurasi php.ini
# vim /etc/opt/remi/php72/php.ini
Lalu ubah beberapa bagian menjadi
file_uploads = On max_execution_time = 180 memory_limit = 256M upload_max_filesize = 64M
Restart php dan apache
# systemctl restart php72-php-fpm # systemctl restart httpd
Masuk ke dir root wordpress
# cd /home/wordpress
Kemudian hapus file index.html dan test.php yang sudah kita buat
# rm -f index.html test.php
Download wordpress
# wget https://wordpress.org/latest.tar.gz
Untar wordpress dan hapus archive
# tar -xzvf latest.tar.gz # rm -f latest.tar.gz
Kemudian pindahkan isi wordpress ke /home/wordpress karena dir root weabsite kita berada pada /home/wordpress, dan hapus directory kosong wordpress
# mv wordpress/* . # rm -rf wordpress
Ubah permission dan kepilikan directory
# chown -R wordpress:wordpress /home/wordpress # chmod -R 755 /home/wordpress
Copy config sample wordpress
# cp /home/wordpress/wp-config-sample.php /home/wordpress/wp-config.php
Kemudian edit isi config.php
# vim /home/wordpress/wp-config.php
Ganti databse,user dan password dibawah dengan database yang sudah dibuat tadi
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpressuser'); /** MySQL database password */ define('DB_PASSWORD', 'wordpresspass'); /** MySQL hostname */ define('DB_HOST', 'localhost'); /** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', '');
Simpan dan keluar, kemudian akses wordpress.task.my.id
Setup seperti biasa
Selamat wordpress berhasil diinstall
# mysql -u root -p
CREATE DATABASE nextcloud; GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost' IDENTIFIED BY 'nextcloudpass' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
Masuk ke dir root nextcloud
# cd /home/nextcloud
Kemudian hapus file index.html dan test.php yang sudah kita buat
# rm -f index.html test.php
Download nextcloud
# wget https://download.nextcloud.com/server/releases/latest-17.zip
Unzip nextcloud dan hapus archive
# unzip latest-17.zip # rm -f latest-17.zip
Kemudian pindahkan isi nextcloud ke /home/nextcloud
# mv nextcloud/* . # rm -rf nextcloud
Ubah permission dan kepilikan directory
# chown -R nextcloud:nextcloud /home/nextcloud # chmod -R 755 /home/nextcloud
Selanjutnya akses nextcloud.task.my.id dan lakukan setup
Selamat nextcloud berhasil diinstall
# yum -y install phpmyadmin
Link phpmyadmin
ln -s /usr/share/phpMyAdmin
1.Konfigurasi virtualhost
# vim /etc/httpd/conf.d/phpmyadmin.conf
<VirtualHost *:8080> ServerName console-sql.task.my.id ServerAdmin admin@task.my.id DocumentRoot /var/www/html/phpmyadmin ErrorLog /var/www/html/phpmyadmin/error.log CustomLog /var/www/html/phpmyadmin/requests.log combined <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9071" </FilesMatch> <Directory /var/www/html/phpmyadmin> <IfModule mod_dir.c> DirectoryIndex index.php index.html index.htm </IfModule> AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost>
2.Konfigurasi server block
# vim /etc/nginx/conf.d/
server { listen 80; server_name console-sql.task.my.id; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://console-sql.task.my.id:8080; } }
Kemudian coba akses console-sql.task.my.id
Selamat phpmyadmin sudah terinstall
3. Buat user untuk phpmyadmin
# mysql -u root -p
CREATE USER 'admin'@'%' IDENTIFIED BY 'password_here'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
Selanjunya coba login dengan user yang sudah di buat
Pastikan sudah mendapat ssl dari sslforfree.com dan certificate sudah tersimpan di server
Buka dan ubah server block nextcloud, dan aktifkan ssl
# vim /etc/nginx/conf.d/nextcloud.conf
Lalu ubah seperti di bawah ini
server{ listen 80; server_name nextcloud.task.my.id; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name nextcloud.task.my.id; ssl_certificate /etc/nginx/ssl/nextcloud/certificate.crt; ssl_certificate_key /etc/nginx/ssl/nextcloud/private.key; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://nextcloud.task.my.id:8080; } }
Pada konfigurasi di atas, kita mengaktifkan ssl dan meredirect semua trafic ke https
Cek apakah ada yang error pada konfigurasi
# nginx -t
Restart nginx
# systemctl restart nginx
Cek apakah SSL sudah aktif
Selamat SSL sudah aktif
Buka dan ubah server block phpmyadmin, dan aktifkan ssl
# vim /etc/nginx/conf.d/phpmyadmin.conf
Lalu ubah seperti di bawah ini
server{ listen 80; server_name console-sql.task.my.id; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name console-sql.task.my.id; ssl_certificate /etc/nginx/ssl/phpmyadmin/certificate.crt; ssl_certificate_key /etc/nginx/ssl/phpmyadmin/private.key; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://console-sql.task.my.id:8080; } }
Pada konfigurasi di atas, kita mengaktifkan ssl dan meredirect semua trafic ke https
Cek apakah ada yang error pada konfigurasi
# nginx -t
Restart nginx
# systemctl restart nginx
Cek apakah SSL sudah aktif
Selamat SSL sudah aktif
Buka dan ubah server block prestashop, dan aktifkan ssl
# vim /etc/nginx/conf.d/prestashop.conf
Lalu ubah seperti di bawah ini
server{ listen 80; server_name prestashop.task.my.id; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://prestashop.task.my.id:8080; } } server { listen 443 ssl; server_name prestashop.task.my.id; ssl_certificate /etc/nginx/ssl/prestashop/certificate.crt; ssl_certificate_key /etc/nginx/ssl/prestashop/private.key; location / { proxy_set_header Host $host; proxy_set_header Accept-Encoding ""; proxy_pass http://prestashop.task.my.id:8080; } }
Untuk prestashop kita tidak akan melakukan redirect ke https di server tapi akan kita lakukan pada website nya
Cek apakah ada yang error pada konfigurasi
# nginx -t
Restart nginx
# systemctl restart nginx