How to connect mysql and php inside different container? - docker

I had two container called "mysql-1" and "ubuntu-1".
And I connect each other used
docker run -p 127.0.0.1:3010:80 -itd --link mysql-1 --name=ubuntu-1 my-ubuntu-image:latest
Notice that my-ubuntu-image already had nginx and php, and I changed the config inside the ubuntu-1 like following:
default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/test/;
index index.php 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/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9999;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
www.conf
listen = 127.0.0.1:9999
It works, when I try to run a simple php file without mysql.
But now I try to run a php file like following:
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
How to connect to mysql-1 container?
The environment Variables inside ubuntu-1 is like following:
MYSQL_1_PORT_33060_TCP=tcp://172.17.0.4:33060
MYSQL_1_PORT_33060_TCP_PORT=33060
MYSQL_1_PORT_3306_TCP_ADDR=172.17.0.4
HOSTNAME=632b415c540e
OLDPWD=/root
MYSQL_1_ENV_MYSQL_ROOT_PASSWORD=123456
PWD=/var/www/html/test
HOME=/root
MYSQL_1_PORT_3306_TCP_PORT=3306
MYSQL_1_ENV_MYSQL_MAJOR=8.0
MYSQL_1_ENV_GOSU_VERSION=1.7
MYSQL_1_PORT_3306_TCP_PROTO=tcp
MYSQL_1_PORT_33060_TCP_ADDR=172.17.0.4
MYSQL_1_ENV_MYSQL_VERSION=8.0.12-1debian9
MYSQL_1_PORT_33060_TCP_PROTO=tcp
TERM=xterm
SHLVL=1
MYSQL_1_NAME=/ubuntu-4/mysql-1
MYSQL_1_PORT_3306_TCP=tcp://172.17.0.4:3306
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYSQL_1_PORT=tcp://172.17.0.4:3306
_=/usr/bin/env

Related

Apache htaccess rewrite rule works but how do I translate to nginx rewrite directive?

I have a live server running Apache and a dev server running NGINX in docker. I wish to rewrite my sites urls from /index.php?t=123abc to /123abc.
This rule seems to work in .htaccess
RewriteRule ^([0-9a-f]{6})$ index.php?t=$1 [L]
However, I can't get it to translate to an nginx directive. My 'default' site conf is:
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.html index.htm index.php;
server_name _;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
rewrite "^/([0-9a-f]{6})/?$" "/index.php?t=$1";
try_files $uri $uri/ =404;
}
autoindex on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
The /index.php?t=123abc works fine but /123abc gives 404 error.
Please help

How to run Xdebug on custom hostname

I've set up Xdebug with my PhpStorm and I can debug only from 0.0.0.0.
The question is:
How can I force to run debug process (listening for PHP debug connections) from any other hostname (like: app.local)?
Dockerfile:
....
RUN echo 'xdebug.client_port=9003' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.mode=debug' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.discover_client_host=true' >> /usr/local/etc/php/php.ini
...
docker-compose:
...
app:
environment:
PHP_IDE_CONFIG: serverName=docker_app
XDEBUG_CONFIG: remote_host=172.17.0.1
volumes:
- ./app/.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
nginx:
volumes:
- ./app/.docker/nginx/conf.d/:/etc/nginx/conf.d/
...
app.conf:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/app/public;
client_max_body_size 40M;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Servers:

Nginx config file is not configured properly. "This site can’t be reached"

I'm fairly new to Nginx and web servers in general.
My setup is docker, Nginx, PHP/laravel, +let's encrypt.
I have this Nginx config file:
server {
listen 80;
server_name www.example.io;
return 301 https://example.io$request_uri;
}
server {
listen 80;
server_name example.io;
return 301 https://example.io$request_uri;
}
server {
listen 443 ssl;
server_name example.io;
ssl_certificate /etc/nginx/certs/example.io.pem;
ssl_certificate_key /etc/nginx/certs/example.io.key;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
I'm pretty sure my certificates are not what the problem is. because they do work when I have a single port 80 server and it has server_name example.io www.example.io but in that case, I'm unable to access the website through example.io as secure. while the www.example.io is secure. it also acts like two different websites. I believe the cookies in one do not implement in the other.
What I'm trying to achieve is, I wish to redirect both www.example.io and example.io to https://example.io

Docker NGINX won't serve two sites

I'm using Docker for the first time, trying to build an NGINX proxy (also first time with NGINX). I've seen multiple guides that all seem to suggest I'm on the right path, but when I run the image, I get duplicate listen options for [::]:80 in /etc/nginx/conf.d/site.conf.
site.conf:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name gamersplane.local;
root /var/www;
index dispatch.php;
location / {
try_files $uri /dispatch.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /dispatch.php =404;
fastcgi_pass api:9000;
fastcgi_index dispatch.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
api.conf:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name api.gamersplane.local;
root /var/www/api;
index dispatch.php;
location / {
try_files $uri /dispatch.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /dispatch.php =404;
fastcgi_pass api:9000;
fastcgi_index dispatch.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
I have two compose files:
docker-compose.yal
proxy:
build:
context: ./nginx
dockerfile: Dockerfile
container_name: gamersplane-proxy
ports:
- 80:80
volumes:
- ../:/var/www
docker-compose.dev.yml
proxy:
volumes:
- ./nginx/dev/site.conf:/etc/nginx/conf.d/site.conf
- ./nginx/dev/api.conf:/etc/nginx/conf.d/api.conf
You need to remove ipv6_only=on, according to the documentation:
This parameter is turned on by default. It can only be set once on start.
So no need to add it to your config

Issues with configuring a simple hhvm site (hack-site-example) with ngnix on ubuntu14.04

I followed the instructions here at https://github.com/hhvm/hack-example-site and somehow lost my way when setting up hhvm hack site on ngnix over ubunut 14.04 . Please note that I used the appropriate apt-get repo for 14.04.
However after configuring and trying to access 127.0.0.1:9000 I see an error in the /var/log/hhvm/error.log
FastCGI protocol: received an invalid record
My /etc/ngnix/sites-enabled is as follows
-rw-r--r-- 1 root root 0 Aug 30 22:01 default
lrwxrwxrwx 1 root root 44 Aug 30 22:21 hack-example-site -> /etc/nginx/sites-available/hack-example-site
The contents of /etc/ngnix/sites-available/hack-example-site is as follows:
server {
root ~/hack-example-site/hack-example-site;
index index.php;
location ~ \.php$ {
# If fastcgi backend is on another server disable this.
# Read issue #6 about this
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME ~/hack-example-site/hack-example-site/index.php
fastcgi_param ~/hack-example-site/hack-example-site $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Could someone please help me with this hhvm site running on my local host? Thanks.
Oh and please note the ngnix is installed properly and the server responds to localhost.
The configuration file /etc/nginx/sites-available/hack-example-site below:
server {
root /root/hack-example-site;
index index.php;
server_name localhost;
location ~ \.php$ {
# If fastcgi backend is on another server disable this.
# Read issue #6 about this
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME ~/hack-example-site/hack-example-
site/index.php
fastcgi_param /root/hack-example-site
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
~
I did find a work around to test my hack scripts but not really satisfied with this. I created symbolic link
/etc/ngnix/sites-enabled/default->/etc/ngnix/sites-available/default.
Copied test.php which has simple hello world written in hack to /usr/share/nginx/html. Edit the /etc/nginx/sites-available/default as follows:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
#index index.html index.htm;
index test.php;
# Make site accessible from http://localhost/
server_name localhost;
include hhvm.conf;
Now when I visited localhost, the hackscript was executed and Helloworld was shown.
However I would have liked this to work with /etc/nginx/sites-available/hack-site-example instead of default. Not sure what is going there? Can anyone throw some light?

Resources