Pada saat develop aplikasi terkadang saya membutuhkan nginx buat mempermudah routing aplikasi yang di develop. Memang sih di environtment saya sudah ada apache tapi saya lebih senang menggunakan nginx karena sudah terbiasa dan menurut saya lebih mudah confignya. Tapi pada terkadang saya juga membutuhkan apache yang sudah terconfigurasi dengan php di environtment saya. Karena kebutuhan ini saya coba cari aja bagaimana agar bisa nginx bisa jalan berbarengan dan bisa apache di proxykan saja di nginx.
Pada tulisan ini saya akan sedikit mengulas tentang routing nginx ke apache. Anda harus sudah menginstal apache dan php sebagai sarana testing.
Buka terminal dan jalankan sudo vi /etc/apache2/sites-enabled/000-default.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<VirtualHost *:8090> | |
# The ServerName directive sets the request scheme, hostname and port that | |
# the server uses to identify itself. This is used when creating | |
# redirection URLs. In the context of virtual hosts, the ServerName | |
# specifies what hostname must appear in the request's Host: header to | |
# match this virtual host. For the default virtual host (this file) this | |
# value is not decisive as it is used as a last resort host regardless. | |
# However, you must set it for any further virtual host explicitly. | |
#ServerName www.example.com | |
ServerAdmin webmaster@localhost | |
DocumentRoot /var/www/html/apache | |
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn, | |
# error, crit, alert, emerg. | |
# It is also possible to configure the loglevel for particular | |
# modules, e.g. | |
#LogLevel info ssl:warn | |
ErrorLog ${APACHE_LOG_DIR}/error.log | |
CustomLog ${APACHE_LOG_DIR}/access.log combined | |
# For most configuration files from conf-available/, which are | |
# enabled or disabled at a global level, it is possible to | |
# include a line for only one particular virtual host. For example the | |
# following line enables the CGI configuration for this host only | |
# after it has been globally disabled with "a2disconf". | |
#Include conf-available/serve-cgi-bin.conf | |
</VirtualHost> |
Kedua mengubah DocumentRoot apache. buat dulu folder apache di dalam folder /var/www/html. Kemudian ubah domcument rootnya seperti pada code diatas baris 12. Jangan lupa semua file yang yang berkaitan dengan apchenya di pindahin ke directory yang barusan dibuat /var/www/html/apache.
Kemudian simpan dan restart apachenya sudo systemctl restart apache2.
Untuk melihat berhasil atau tidaknya merubah port dan DocumentRoot dari apachenya bisa akases localhost:8089 (8089 adalah port yang dikasih saat mengubah port di langkah sebelumnya)
Untuk config apache sudah selesai. Sekarang tinggal install nginx. Jalankan perintah berikut di terminal sudo apt-get install nginx
Setlah instalasi selesai, Anda dapat mengeceknya di localhost. Jika berhasil, hasilnya seperti gambar berikut:
Selanjutnya melakukan configurasi pada nginx. Jalankan perintah sudo vi /etc/nginx/sites-available/default dan edit seperti source berikut:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# You should look at the following URL's in order to grasp a solid understanding | |
# of Nginx configuration files in order to fully unleash the power of Nginx. | |
# http://wiki.nginx.org/Pitfalls | |
# http://wiki.nginx.org/QuickStart | |
# http://wiki.nginx.org/Configuration | |
# | |
# Generally, you will want to move this file somewhere, and start with a clean | |
# file but keep this around for reference. Or just disable in sites-enabled. | |
# | |
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. | |
## | |
# Default server configuration | |
# | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
# SSL configuration | |
# | |
# listen 443 ssl default_server; | |
# listen [::]:443 ssl default_server; | |
# | |
# Note: You should disable gzip for SSL traffic. | |
# See: https://bugs.debian.org/773332 | |
# | |
# Read up on ssl_ciphers to ensure a secure configuration. | |
# See: https://bugs.debian.org/765782 | |
# | |
# Self signed certs generated by the ssl-cert package | |
# Don't use them in a production server! | |
# | |
# include snippets/snakeoil.conf; | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files $uri $uri/ =404; | |
} | |
location /test { | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $remote_addr; | |
proxy_set_header Host $host; | |
proxy_pass http://127.0.0.1:8090/test; | |
} | |
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 | |
# | |
#location ~ \.php$ { | |
# include snippets/fastcgi-php.conf; | |
# | |
# # With php7.0-cgi alone: | |
# fastcgi_pass 127.0.0.1:9000; | |
# # With php7.0-fpm: | |
# fastcgi_pass unix:/run/php/php7.0-fpm.sock; | |
#} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
#location ~ /\.ht { | |
# deny all; | |
#} | |
} | |
# Virtual Host configuration for example.com | |
# | |
# You can move that to a different file under sites-available/ and symlink that | |
# to sites-enabled/ to enable it. | |
# | |
#server { | |
# listen 80; | |
# listen [::]:80; | |
# | |
# server_name example.com; | |
# | |
# root /var/www/example.com; | |
# index index.html; | |
# | |
# location / { | |
# try_files $uri $uri/ =404; | |
# } | |
#} |
Selanjutnya buat folder test dalam DirectoryRoot apache yang diset diatas.
buat sample file dengan nama index.php.
lihat pada code berikut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
phpinfo(); | |
?> |
selanjutnya akses url localhost/test. Hasilnya akan seperti gambar berikut
Kalau ingin mengakses langsung dari apachenya bisa tambahkan port saja sesuai port yang di seting pada apachenya localhost:8090/test.
Semoga bermanfaat.
0 coment�rios:
Post a Comment
### Saya akan coba merespon komentar baru secepatnya.
### Jangan berkomentar SPAM (promosi, dll,)
### Jangan komentar yang berisi link aktif,
### Mohon Maaf Jika Komentar saya moderasi karena ingin mengantisipasi kemungkinan Spam atau link Aktif