LNMP suggests a solution to the File not found problem
- 2020-06-23 02:32:05
- OfStack
preface
1 under normal circumstances each virtual host is a website, website 1 through the domain name for access. I recently encountered a problem configuring LNMP (Linux Nginx MariaDB ES7en-ES8en) :
Visit the home page of the website to always improve File not found.
After the final solution to sum up 1 is: the site root directory permission issues.
The cause of
Tell me how it started. The system I use is Manjaro Linux. So install the LNMP environment with 1 command: sudo ES24en-ES25en mariadb nginx php. At first, I put the symfony project in my home directory, which is ~/projects. Then I 1 copies of official from symfony symfony nginx configuration file. The conf, on/etc/nginx/sites - available directory, then root parameters changed to/home lrcn/projects/symfony/public, and change the fastcgi_pass parameters for unix: / run php fpm/php - fpm. sock. Ok, run the sudo ES55en-ES56en test and then restart nginx.
But here's the problem. I typed symfony.dev into the browser (add 1 hosts to 127.0.0.1 symfony.dev) and always prompted File not found. Not even if I set the site directory to 777! This question has been bothering me for a day! It was like my heart was going to explode. Finally I took a deep breath and thought about the problem. Sure enough, I got the answer after searching through the Internet resources.
Here's the explanation. The ES70en-FPM program needs 1 user and user group to run the program. This user and user group must have rx permissions on my project files. Some directories must also have rwx permissions, such as upload, log directories, and so on. The Nginx program also requires a user who also has rx permissions for the project file.
The solution
Since my nginx default user is http, I want to change it to nginx and add nginx to the www user group. At the same time, I changed the user base of ES87en-ES88en to www and the user group to www.
1) Create users
sudo useradd -s /sbin/nologin www # create www User, and it will be created automatically www User groups
sudo gpasswd -a nginx www # the nginx Users are added to www User groups
2) Modify the configuration file
Since I'm only dealing with file permissions here, the rest of the configuration itself needs to be adjusted
# grep "^[^;]" /etc/php/php-fpm.d/www.conf
# Just change it when you change it user, group, listen.owner, listen.group , the rest is the default configuration
[www]
user = www
group = www
listen = /run/php-fpm/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
# /etc/nginx/nginx.conf
# In the first 1 Row writes, the rest remains the same
user nginx www;
3) Modify project file permissions
If I put the website directory in the home directory of lrcn users, www users will still be unable to access the project file. Even if I set ~/projects to chown: es1109en ~/ projects-ES112en, it doesn't work. Running the test command ES113en-ES114en stat ~/projects/symfony still indicates that the user has no permissions. I think the /home/lrcn directory is owned by lrcn, even though I changed its subdirectory projects to www:www. That makes sense. So, I put the project directory in /var/www directory again, and then ran ES127en-ES128en www stat /var/www, and got the normal result:
[lrcn@lrcn-pc nginx]$ sudo -u www stat /var/www
[sudo] lrcn Password:
File: /var/www
Size: 4096 Block: 8 IO Block: 4096 directory
Equipment: 801h/2049d Inode : 3671064 Hard links: 3
Jurisdiction: (2775/drwxrwsr-x) Uid : ( 1000/ lrcn) Gid : ( 1001/ lrcn)
Recent Visits: 2017-12-10 00:40:04.274947995 +0800
Last change: 2017-12-10 00:41:34.772321160 +0800
Recent changes: 2017-12-10 10:51:15.811999323 +0800
Creation time: -
If the above information proves that www users have access to the site root, then move project symfony to /var/www.
[lrcn@lrcn-pc var]$ tree www -L 2
www
└ ─ ─ symfony
├ ─ ─ bin
├ ─ ─ composer.json
├ ─ ─ composer.lock
├ ─ ─ config
├ ─ ─ public
├ ─ ─ src
├ ─ ─ symfony.lock
├ ─ ─ var
└ ─ ─ vendor
Ok, modify directory permissions. I changed the /var/www directory to lrcn:www and created a soft link to /var/www/symfony in the ~/projcects directory so That I could work with project files in my home directory. The question is why the owner of /var/www directory should be changed to lrcn, because in this way, I can use lrcn user to operate this project file at will. lrcn user is my user who logs on to the linxu system, which is very convenient in the development project.
sudo chown lrcn:www /var/www -R # Change the permissions on the project
sudo ln -s /var/www/symfony ~/projects/symfony # Home directory access project
sudo chmod 775 /var/www -R # let www Users can manipulate files
sudo chmod g+s /var/www -R # after lrcn Files or folders created by the user are inherited www User groups, not lrcn User groups, this command is very important
Effect after change:
[lrcn@lrcn-pc symfony]$ ls -al
The total amount 108
drwxrwsr-x 10 lrcn www 4096 12 month 10 01:14 .
drwxrwsr-x 3 lrcn www 4096 12 month 10 00:41 ..
drwxrwsr-x 2 lrcn www 4096 12 month 10 00:41 bin
-rwxrwxr-x 1 lrcn www 1146 12 month 10 00:41 composer.json
-rwxrwxr-x 1 lrcn www 48074 12 month 10 00:41 composer.lock
drwxrwsr-x 3 lrcn www 4096 12 month 10 00:41 config
-rwxrwxr-x 1 lrcn www 495 12 month 10 00:41 .env
-rwxrwxr-x 1 lrcn www 495 12 month 10 00:41 .env.dist
drwxrwsr-x 8 lrcn www 4096 12 month 10 00:41 .git
-rwxrwxr-x 1 lrcn www 106 12 month 10 00:41 .gitignore
drwxrwsr-x 4 lrcn www 4096 12 month 10 00:41 .idea
drwxrwsr-x 2 lrcn www 4096 12 month 10 10:42 public
drwxrwsr-x 3 lrcn www 4096 12 month 10 00:41 src
-rwxrwxr-x 1 lrcn www 1889 12 month 10 00:41 symfony.lock
drwxrwsr-x 4 lrcn www 4096 12 month 10 00:41 var
drwxrwsr-x 5 lrcn www 4096 12 month 10 00:41 vendor
Ok, at this point, we have basically solved the problem of File not found.
Then go to the home page
conclusion