0

I am starting to learn server management with a CentOS Virtual Machine. I am trying to make a simple website.

Because the Apache default file is in /var/www it is hard to write/make/change files. Having said that, I would like to make the home page in the folder /var/www and all other pages will be in the folder /home/$USER/Documents/Website.

The code on the home page is just an href to the other pages. When I run the code on the machine, it works, but if I go the the home page (type in the IP in a browser) the link doesn't work, but the home page is still there.

The error that I get when I click the link is 404 file not found.

If I copy the file location from that error code and, on the Server, and type

cat {file_location}

It prints the file no problem. I have changed the Apache group to the owner of the file. How can I allow the Apache server to find the files in here? Would I need to edit the http.conf?

EDIT: How would I just make it so the server could read and write in a folder called /Website?

EDIT 2: I do not know which version of Apache I am running. I used the CentOS installer to install the Apache. The /var/www/html/index.html is the webpage pulled up when I type in the IP address, as it should. It very well could be a SELinux problem as after I installed the Server with GUI packages, a SELinux pop-up told me of a 'security' problem. I ran the scripts that it said and I could write the the /var/www/html folder. Now I am wanting to add folders in a different location and I don't know how to do so. Other than SELinux being in the way of writing to the /var/www/html folder, I also changed owner and ran chmod 755 if I remember correctly.

  • You need to provide more details. What kind of Apache configuration are you using? What DocumentRoot is used there? – sebasth Sep 21 '18 at 21:09
  • Initially I would recommend you take the easier route of learning how to set permissions to allow you to create/edit files under /var/www. Really. If you care to extend your question to permit that as an acceptable alternative then someone here (if not me) can explain about chmod. – roaima Sep 21 '18 at 23:13
  • This could be an selinux problem as well, which would be far more complicated than simple file permissions. Its been my experience that if selinux is enabled and enforcing, then placing httpd content anywhere outside of normal locations results in a 404 unless you also update your selinux policies. – guzzijason Sep 22 '18 at 2:26
  • 1
    You still haven't provided any details on your Apache configuration? What do you mean by "home page is just an href to the other pages."? Are you using mod_userdir for example? Why do you think server can't read/write to /var/www/html (what have you tried)? – sebasth Sep 23 '18 at 7:01
2

If you only want Apache to serve files in /Website, edit your Apache configuration /etc/httpd/conf/httpd.conf and change DocumentRoot to /Website. Then reload the server systemctl reload httpd.service.

You also need to change SELinux file context of /Website and files within. You likely want to use same file contexts as in /var/www (httpd_sys_content_t or httpd_sys_rw_content_t). Add new file labeling rule with semanage and then use restorecon to apply it:

semanage fcontext -a -t httpd_sys_rw_content_t "/Website(/.*)?"
restorecon -R -v /Website

If you want to configure Apache to serve user content in home directories, eg. http://server/~user, you can use mod_userdir. Enable it in /etc/httpd/conf.d/userdir.conf and reload the server.

Enable httpd_read_user_content SELinux boolean to allow access:

semanage boolean -m --on httpd_read_user_content 

Files in directory public_html should already be configured in correct httpd_user_content_t type. If not, you can run restorecon -R -v /home/user/public_html to apply correct file contexts.

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.