-1

I created an CentOS 7 EC2 instance on AWS and installed nginx as my web server. However unlike Apache, it has not created a /var/www folder and instead created /usr/share/nginx/html.

I'd prefer to upload my website files to /var/www. While I have some experience with the Linux terminal (I am primarily a Windows developer), I'd like to know what is the right way in creating the /var/www folder for a PHP website (Yii2 Framework).

I did use mkdir, but Filezilla gets a permission denied error when uploading files and creating sub-directories. I can do chown on the directory, but I am not sure whether that is secure or the right thing to do. What permissions would I set the folder?

EDIT

Perhaps the question is a bit confusing...

Filezilla is configured properly and I can upload to other directories. It is a file/folder permission problem, where I need to prefix sudo before mkdir to create the /var/www folder or any folders within, and this is something that Filezilla doesn't do.

The question is not how to reconfigure nginx folder, but what should I keep in mind (security) while creating a /var/www folder for nginx. What permissions should I give so that I don't need to sudo mkdir. I am assuming it is best not to give ownership (chown) to the root or something.

Just want to be sure, since this would be a production server.

  • Filezilla is an FTP client and would not have anything to do with the webserver that you use. If Filezilla is giving you permission problems, you should make sure your FTP server is configured properly. As for the nginx configuration, you should be able to replace /usr/share/nginx/html with /var/www. Post any nginx errors you get when you trying the new configurations. – GracefulRestart Oct 30 '18 at 21:27
  • @GracefulRestart please see my edits – Shahid Thaika Oct 30 '18 at 21:47
  • @GracefulRestart, just as an FYI, FileZilla is also an SFTP and FTPS client, but your point largely stands. – DopeGhoti Oct 30 '18 at 22:02
  • @ShahidThaika You need to be more clear. Do you just want to be able to upload your website files to /var/www via FTP or do you also want nginx to use /var/www as its root? If you want to do that latter then you will indeed need to reconfigure the nginx.conf file to change where the root is for your website directories and files. If you only want the former then you need to use chown to make the ftp user the owner of /var/www so that it can read and write to that directory. – Nasir Riley Oct 30 '18 at 22:08
1

On a typical system with Apache installed, /var/www does exist as you expect, but it is owned by the root user:

~$ ls -ld /var/www
drwxr-xr-x 3 root root 4096 Aug  7  2016 /var/www

If you want your web site's contents to be owned by a user other than root, you will need to either create such a directory (as root) owned by the user whom you wish to administer the web site files, or create a symbolic link at /var/www/html pointing at the document root which ngnix has already created for you. Either way, that directory is probably also owned by the root user. You can either chown it to the role user meant to be administering the web site contents, create a virtual host (to use Apache's parlance) pointing at a different directory owned by the role user, or find a similar solution.

If the document root directory is owned by root, only the root user can alter its contents, which means you will have to authenticate as root to upload new files. Changing the owner or serving from a different location are probably the best options.

In ngnix' parlance, a Virtual Host is also known as a Server Block; you can find examples (usually) in /etc/ngnix/sites-available/ which you can link into /etc/ngnix/sites-enabled. They key when using Server Blocks is to use the server_name (ngnix) or ServerName (Apache) directive to indicate which hostname(s) to look for in the HTTP request header. An example ngnix Server Block file might look something like:

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

The root directive specifies where on the filesystem to look for the actual files to serve. You could, if you had a user websiteadmin, specify that to be /home/websiteadmin/var/www/html or whatever directory you prefer.

  • On the EC2 AMI, I was given centos as the user, and will chown to that. Can you tell me what permission I'd need to set on the html folder under /var/www (where the php files will reside)? I'd do that recursively, yes? Also, what about the .php files themselves? – Shahid Thaika Oct 30 '18 at 22:15
  • 1
    sudo chown -R username /var/www followed by sudo chmod 0755 /var/www /var/www/html. – DopeGhoti Oct 30 '18 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.