Give total permissions is not correct, especially in production. However, to assign those permissions recursively you can use:
chmod -R 777 /var/www
although I do not recommend those permissions.
You can give read permissions ( r ) and write ( w ) for files and read, write, and execute permissions ( x ), that is rwx , for folders.
Never give execution permission to files on your server.
Give total permissions recursively:
chmod -R 777 /var/www
Remove execution permissions only for regular files:
find /var/www -type f -exec chmod -x {} \;
With that configuration you should not have problems to create your file.
but I recommend you remove write permissions in folders where you will not upload or create files with php.
A recommended configuration for Apache in production is:
Assuming that:
- The folder on your site is / web
- The apache user is www-data
1.- You must have a development group
groupadd development
2.- All files and folders must belong to apache and the group must be the development group
chown -R www-data:development /web
3.- For the user apache to put read-only permissions for files and read, execution for folders does not assign permissions for other users.
chmod -R 570 /web
4.- For folders in which you want to upload files, delete or create files give write permissions for Apache
chmod -R u + w / web / uploads
5.- For the development group put s
chmod -R g+rwxs /web/
6.- Remove execution permissions for the files the point
find /web -type f -exec chmod -x {} \;
7.- Add your user and all the necessary ones to the development group.
groups username
usermod -G development username
8.- Add umask in the profile for users who connect by ssh /home/user/.profile
umask 0002
9.- If you use sftp to upload files to the server add the umask in the subsystem line of the configuration file of sshd /etc/ssh/sshd_config
Subsystem sftp internal-sftp -u 0002
In point 3 and 5 the files are left with execution permissions but point 6 solves it
In point 7 with considering that if the user has other groups add them in comma-separated usermod the exit of the groups will tell you which groups to keep
usermod -G otrogrupo,development,... username