Uploads in Symfony's var directory not accessible - symfony

I'm trying to upload files into 'var' directory (successfully). But when I want to take this one file from 'var' I only can observe No route found for "GET /var/uploads/images/....
Structure:
- app
- bin
- src
- var
- cache
- logs
- uploads
- images
- vendor
- web
For saving my files I'm using: '%kernel.project_dir%/var/uploads/images'
For order to take file: '/var/uploads/images/' . $fileName;
Where're my mistake?
P.S. Moreover I try to use volume (I'm using docker) in my docker-compose file like this: - ./data/cabinet/uploads/images:/data/www/cabinet/var/uploads/images
And, unfortunately, no one file didn't copy to this directory. What's wrong?
Thank you!

Since the document root is pointing to the web directory, there's no possibility for path ../var to be accessible. Either store uploads inside the web directory or create a symlink inside it pointing to the var/data/uploads:
// since I don't know your path tree inside the container
// I assume whole application is installed inside /var/www/html
// and this is the default working directory
docker-compose exec YOUR_CONTAINER_NAME \
ln -rs /var/www/html/var/data web/var
Bear in mind that if you are using Apache webserver, the FollowSymLinks option has to be enabled, but most probably is.
As for the second question, most likely you mapped an invalid directory, I guess this should be:
// yet again I assume whole application is inside the /var/www/html
./data/cabinet/uploads/images:/var/www/html/data/cabinet/uploads/images
Is this some kind of production anyway? If it is not and this is merely a work-in-progress, then you rather should map your whole local directory as container volume. This is how it's usually done in dev mode, so the mapping would be:
.:/var/www/html

Related

Nginx - Custom configuration files location

I use Nginx with many domains. Some of these domains have custom configurations. I'm including these files inside the server blocks in the Nginx configurations.
For example:
server {
... some configurations things here...
include /var/somewhere/custom.conf;
etc.. etc..
}
The configuration files of Nginx are inside: /etc/nginx
To try and keep everything in one place and not have my custom configuration files all over the place I would like to place my custom configuration files inside /etc/nginx/some_directory
Can I create a sub directory inside /etc/nginx without it causing any issues with Nginx itself? I want to create /etc/nginx/some_directory/ and place my many custom configuration files inside it and include them.
I'm specifically asking this question because I don't want to break something on my production server.
If nginx doesn't know about a directory, it'll not touch it. You can verify that by greping against such pattern in nginx's codebase.
However, messing with a foreign folder structure might cause problems with permissions and ownership of the files, therefore either just use a pre-defined folders nginx prepared for you (/etc/nginx/sites-enabled and /etc/nginx/sites-available) which you can use with symlinks such as nginx itself does
# ls /etc/nginx/sites-enabled
default -> /etc/nginx/sites-available/default
# ls /etc/nginx/sites-available
default
otherwise you're getting into a situation what C/C++ programmers call an undefined behavior and there's no guarantee that what works now will work in the future / nginx doesn't change as well as the distro maintainers might mess with the folder structure and permissions for the packages in distro package manager.
Example:
Nginx might verify the full /etc/nginx tree's permissions and owners - if your folders/files don't match it might cause a warning or crash even. If it's installed by a package manager, it might cause issues when removing the package itself e.g. if the package manager attempts to remove only a known list of folders + afterwards the parent i.e. /etc/nginx by rmdir or similar. Situations you don't really want to get into and debug when you can use allowed folders or symlinks or your own folders that are not bound to an application or behavior except the one you define.

NGINX - /etc/nginx/conf.d is empty

I'm working with Nginx From Beginner to Pro. The book affirms that:
The /etc/nginx/conf.d folder contains two files, default.conf and
example_ssl.conf .
but when I open my /etc/nginx/conf.d then make the list command I have nothing but just some dots like .. . which appears and are impossible to handle -neither read or open. So I'm wondering what it happens and why I haven't my files appears in the directory. After having made some searches on web I have found nothing relevant to me.
Also it's impossible to create neither a folder or file in this directory.
Currently I have create an another folder and probably will use it to insert some custom configuration.
Today, I also fell into the same issue.
When I was trying to fetch this file using FileZilla, it was not showing up.
But then I logged in through Putty and ran the command
sudo nano /etc/nginx/conf.d
The file got opened up with all its contents.
I hope this helps others.
PS: we should not try to alter this file at beginner level.
I had to find it only to increase the hash_bucket_limit inside http object. (Because the length of my domain name regex was larger than default limit).

Dockerfile - Touch file at container path

docker-compose.yml file:
web:
build: ./code
ports:
- "80:80"
volumes:
- ./mount:/var/www/html
dockerfile in ./code:
FROM wordpress
WORKDIR /var/www/html
RUN touch test.txt
This is a production environment I'm using to set up a simple WordPress blog (omitted other services in docker-compose.yml & Dockerfile for simplicity).
Here's what I'm doing:
Bind mounting host directory at container destination /var/www/html
Create test.txt file during build time
What's NOT working:
When I inspect /www/var/html on the container, I don't find my test.txt file
What I DO understand:
Bind mounting happens at run-time
In this particular case file gets created, but the when you mount the host directory, commands in Dockerfile get overridden
When you use volume mount, It works
What I DON'T understand:
What are the ways in which you can get your latest code into the container which is using a bind mount to persist data?
How can one create a script that can let me achieve this in runtime?
How else can I achieve this considering I HAVE to use a bind mount (AWS ECS persists data only when you use a host directory path for a volume)
Your data will be persisted at runtime. Everything stored in /var/www/html at runtime will be persisted in the host ./mount directory.
At build time, everything happens at docker layer inside the container image.
If you want to do things before anything you could create a script, ADDit to your image and use CMD or ENTRYPOINT to run the script when the container starts.
In summary
What are the ways in which you can get your latest code into the container which is using a bind mount to persist data?
You add your latest code to the image (i.e. git clone, COPY, ADD, or what suits you). A container shouldn't be mutable, so you keep your code versioned and you define persist folder (e.g. for uploads)
How can one create a script that can let me achieve this in runtime?
If you want to do it at runtime, you add your shell script to the image and then you run it. Although, this is not the best approach for this use case.
How else can I achieve this considering I HAVE to use a bind mount (AWS ECS persists data only when you use a host directory path for a volume)
IMHO, you should use your images as your build of your code. Your image should not be mutable and must reflect an instance in your code lifecycle. Define paths with data and those paths would be your mounts at host level.

Docker and bower links

I'm using docker to run a simple static web project, using the nginx official image. As a bower dependence I have a ui lib that is mine and is shared among two of my projects. To facilitate the development process I created a volume to my local machine to serve local files through the /html folder inside the nginx container. It works fine this way.
But, if I try to use bower link to create a link between a local copy of my ui lib and the bower dependence the nginx web server is not able to find the folder, since the link points to my local machine.
I'm running the docker vm in a Mac.
Did someone experienced something similar and have an idea about how to solve it?
Thanks,
I just run into this issue and found a way to solve it nicely.
The problem is that when you mount as a volume the whole /html folder the symlinks created by bower link are copied into your container but not the actual folders they are pointing at. When nginx tries to serve the file, it follows the symlink but now INSIDE the container, where the route is invalid.
To fix this, create another volume that maps the symlink directly. This way, docker-compose will follow the symlink BEFORE mounting into the container, therefore copying the actual folder contents. The nice thing about this is that in your local file system you still have the folder and the symlink working, so you can work as usually :)
Practical example:
My folder structure
/app
|--/bower_components
|--/packageA
|--/packageB -> symlink to /foo/bar/packageB
My compose file:
version: '2'
services:
nginx:
volumes:
- .:/foo
- ./bower_components/packageB:/foo/bower_components/packageB
...
Let me know if it worked, cheers!

Relative paths when uploading image with Symfony 1.4 on shared hosting

I've had a running website on VPS which I've built using Symfony 1.4.20 but then I decided to move it to a shared hosting and the problems arised. I think I've done the essential parts since it's accesible now but when I try to upload any images via backend it shows error message as in the ss below:
Failed to create file upload directory "/../public_html/img".
img folder, which I try to upload the image into, is already created and inside the public_html folder. I also set public_html and img folders' chmod as 777.
When I use the url_for function it shows the correct url of images from public_html/img folder, for example: domain/img/imagename.png
Here's my configuration:
/
/app (Everything except web folder is here)
/public_html (I put everything inside the web folder into this folder)
My ProjectConfiguration.class.php file:
require_once dirname(__FILE__).'/../lib/vendor/symfony-1.4.20/lib/autoload/sfCoreAutoload.class.php';
public function configureDirectories()
{
sfConfig::set('sf_web_dir', '/../public_html');
sfConfig::set('sf_all_images_url', '/img');
sfConfig::set('sf_all_images_dir', sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . 'img');
}
And in my backend.php:
require_once(dirname(__FILE__).'/../app/config/ProjectConfiguration.class.php');
So when I want to save any image I give the relative path like this:
$filepathImage = sfConfig::get('sf_all_images_dir') . '/' . $filenameImage;
It used to work back in the day when my website is configured as default on the private server but now I'm stuck, I need to fix this as soon as possible, so please help me :) I didn't do any url rewriting. If you need further information about any part of the project I can show it. Thanks.
maybe you need to dump ur $filepathImage and see if it's the right path

Resources