Create a new virtual host via php fails on Permission denied - php

I have 2 problems:
when I run:
file_put_contents("/etc/apache2/sites-enabled/www.mydomain.com.conf",$vh);
I get the error: failed to open stream: Permission denied
How do I safely reload apache (after adding a domain) from PHP.
I don't think $res = shell_exec("sudo service apache2 reload") is working, I dont get anything in return.

Its a bad practice, why don't you go and create a default virtual host that will answer to any domain.
In the app level you can do some protections about it.
This way you are shortening your code to the minimum.
He is an example (dont use it as it is, needs to be tested):
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/the_site>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You definitely do not want to do as PHP, but you might try the following approach: modify the sudoers file to grant your apache the ability to sudo a single command:
root ALL=(ALL) NOPASSWD: /usr/local/bin/php_add_vhost
Then you create an executable script (either bash or php, python or whatever) which reads the data from a specific location, puts it in the /etc directory and does a "service apache2 reload".
This approach has many security problems, but if you are asking this question you are probably good to go anyway.

Related

Change Apache directory to show Wordpress homepage

I have Wordpress installed using an AWS EC2 instance. The public IP is as here. I used LetsEncrypt to get SSL, that worked fine. But after that, my homepage now shows the 'Apache2 Ubuntu Default Page'. It should be showing me the Wordpress homepage. I still have ssh access to the EC2 (Bitnami Wordpress), so my data is supposedly still there.
I've been doing some research at it seems that I need to change something with Apache so it direct to the Wordpress directory/page.
Any help in the matter would be most appreciated :)
Bitnami Engineer here,
It seems you installed the Apache2 system's service in the machine and it got started at boot time. The Bitnami apps don't use the system's services. That's why the Bitnami's Apache service can't be started because other service is already running in the 80 port. In order to stop and disable it, please run these commands
sudo service apache2 stop
sudo service apache2 disable
sudo /opt/bitnami/ctlscript.sh start apache
Happy to help!
The fact that you're getting the Apache default page is a good sign, it means everything from a networking standpoint is working correctly. Now, you just need to show Apache where to serve your files.
Apache stores their default configuration typically in /etc/httpd/httpd.conf or /etc/apache2/sites-available/default and looks something like below.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Before making changes to this file (whenever you find it), you will also need to know where the DocumentRoot is. This is essentially the directory that your index.php is located. In the example above it's located in /var/www, and that's typically a good place to start looking.
If you're having a hard time finding your root directory, you can do something like find / -type f -name "index.php".
Assuming your index.php is in /var/www/wordpress your configuration could look as simple as this.
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/wordpress
</VirtualHost>

Need some assistance with my apache2 site config file

I'm trying to get apache2 to point to a PHP based application (word press in this case but the config needs to be generic enough to work for any php application).
and it either displays some basic HTML file access page or errors with "You don't have permission". I don't really know apache and I don't know PHP at all. Here's my current site_config file as it stands(with retractions replaced with ):
<VirtualHost *:80>
ServerAdmin <app_user>#localhost
ServerName amazonaws.com/<app_name>
ServerAlias *.amazonaws.com/<app_name>
DocumentRoot /home/<app_user>/<app_location>/staging/current
<Directory /home/<app_user>/<app_location>/staging/current >
AllowOverride All
Options -Indexes
Order allow,deny
Allow from all
</Directory>
LogLevel error
ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>
I would also like to make it so you can have multiple websites on the same box but I'm not sure how to change the VirtualHost arg *:80 to account for that, I just get loads of ignoring errors.
I also have the following line in my apache2.conf:
DirectoryIndex index.php index.html
Folder permissions are set to 0755 for all files & folders in the project directory
output of apache2 -v:
Server version: Apache/2.4.7 (Ubuntu)
P.s. I know nothing about PHP and very little about apache2 so for this, speak to me as a total noob.
To solve The first problem I had to change the following lines:
Order allow,deny
Allow from all
to:
Require all granted
this finally allowed apache to allow users access to the folders, the second thing was to add an alias:
Alias /<app_name> "/home/<app_user>/<app_location>/staging/current"
and it started working as expected so now my site config looks like:
Alias /<app_name>"/home/<app_user>/<app_name>/<app_environment>/current"
<Directory "/<app_name>"/home/<app_user>/<app_name>/<app_environment>/current">
AllowOverride All
Options -Indexes
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot "/<app_name>"/home/<app_user>/<app_name>/<app_environment>/current"
ErrorLog "/var/log/apache2/<app_name>.error_log"
CustomLog "/var/log/apache2/<app_name>.access_log" combined
</VirtualHost>

Apache configuration for a simple test page

I'm trying to get a handle on Apache configuration files. One of the things I want to do is move some webpages away from the /var/www directory. I have a working configuration in my own pc (name is pc01). There is a webpage I use for tests in /var/www/compile/ and inside there is a single page file: compile.php.
I moved the compile folder to /home/web/compile. And I created this configuration file:
<VirtualHost *:80>
ServerName ctest
DocumentRoot /home/web/compile/
<Directory />
Order Allow,Deny
Options FollowSymLinks
AllowOverride None
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/compile.log combined
</VirtualHost>
As I understand the tutorials I have used if I point my browser to pc01/ctest/compile.php I should see the webpage. However All I get is:
Forbidden
You don't have permission to access /ctest/compile.php on this server.
What am I doing wrong?
You need to set file owner to apache user (user running web server).
Default user for web server is www-data. Try this command in terminal:
chown www-data:www-data /home/web/compile -R
Found it!! The key was to modify the apache2.conf file (/etc/apache2/apache2.conf in Debian) to add another document root other than /var/www. Then it started working. Thanks for the help!

Apache virtual host configuration: How to make my Zend projects and phpMyAdmin play together?

In order for me to be able to run a Zend Framework project on my local development machine, I made changes to Apache's \etc\apache2\httpd.conf and the openSUSE system's \etc\hosts files. I set up a test3.local alias for an individual Zend project, and things seem to "work".
Before I started fiddling with things, I could access phpMyAdmin simply by entering http://localhost/phpMyAdmin/ in my browser. And if I take away my changes, that once again works.
Using this answer as a basis, I tried to set up an additional virtual host specifically for phpMyAdmin, hoping to "solve" this problem. But right now if key in the virtual host name, admin.local, that I intend to take me to phpMyAdmin, I get a 403 error like this:
(source: willmatheson.com)
Here is my present httpd.conf:
### Virtual server configuration ############################################
IncludeOptional /etc/apache2/vhosts.d/*.conf
<VirtualHost *:80>
ServerName test3.local
DocumentRoot /home/william/public_html/ZendTest3/public
SetEnv APPLICATION_ENV "development"
<Directory /home/william/public_html/ZendTest3/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName admin.local
DocumentRoot /var/lib/mysql/phpMyAdmin
# This gives permission to serve the directory
<Directory /var/lib/mysql/phpMyAdmin>
DirectoryIndex index.php
Options None
AllowOverride All
# This allows eveyone to access phpmyadmin, which you may not want
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
and hosts:
127.0.0.1 localhost
127.0.0.1 local
127.0.0.1 test3.local
127.0.0.1 admin.local
Ideally I'd like to not have to specify a virtual host for phpMyAdmin at all, because I'm sure to muck it up, and just somehow have the settings to make the Zend project work but to also have phpMyAdmin work like it did before.
I know this question was already answered, but I thought I'd share what I did to overcome a similar problem, in case it helps anyone else.
My problem was:
I started to get localhost/phpmyadmin 404 error after changing the DocumentRoot folder in httpd.conf. The change I made was to change the DocumentRoot
from:
DocumentRoot "C:\Program Files (x86)\Zend\Apache2/htdocs"
to:
DocumentRoot "C:\Program Files (x86)\Zend\Apache2/htdocs/a/deeper/folder"
I fixed it by changing a line in zend.conf
from:
Alias /phpMyAdmin "C:\Program Files (x86)\Zend\phpMyAdmin"
to:
Alias /phpMyAdmin "C:\Program Files (x86)\Zend\ZendServer\data\apps\http\__default__\0\phpMyAdmin\4.0.5.4_41"
Hope this helps somebody else!
Well, there's a good reason I was getting a 403 - I was digging in the wrong place. My installation of phpMyAdmin was actually in /srv/www/htdocs/phpMyAdmin. Changed that, restarted Apache (sudo systemctl restart apache2.service) and things seem to work.
If you're interested in how the heck to find files and folders on openSUSE, the following steps worked for me:
sudo zypper install findutils-locate
su
updatedb (go check your e-mail)
locate phpMyAdmin (like that, not 'phpmyadmin')

apache php Virtualhost

I am trying to implement virtual host in my system.
I have used the below code for that.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Users/shanker/Documents/content_booking/"
ServerName content.boking
Alias /booking "/Users/shanker/Documents/content_booking/public/"
</VirtualHost>
also i have updated the hosts file as
127.0.0.1 content.booking
But I am getting the following errors:
---------------------------------------------------------
Access forbidden!
You don't have permission to access the requested directory. There is either no index document or the directory is read-protected.
If you think this is a server error, please contact the webmaster.
Error 403
---------------------------------------------------------
Can anybody please fix this issue.
First check your httpd.conf file in Apache and make sure Virtual hosts is on. (remove the '#' to turn on/uncomment).
#Virtual hosts
Include conf/extra/httpd-vhosts.conf
next update your vhosts-file with some Directory options.
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Users/shanker/Documents/content_booking/"
ServerName content.boking
<Directory "C:/Users/shanker/Documents/content_booking/">
Options Indexes FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Alias /booking "/Users/shanker/Documents/content_booking/public/"
Hope this helps.
Your ServerName is with one "o", and your hosts file entry is with "oo" (two).
Does it work once you fix that typo?
If you do not include an index.[html|htm|php] in a directory, the default action may be to list all the files in that directory or more commonly for security, to throw this error.
http://127.0.0.1/ causes Apache to look for index.[html|htm|php] files and if there isn't one there and your security doesn't allow listing the directory (which it shouldn't), this is the error.
Create an index.html file in the proper directory and see if that helps.
OS X's permissions, etc. are set up (out of box) to serve web documents from /Users/shanker/Sites so, if it's a permissions related issue, you might just try moving your code there to the /Users/shanker/Sites/content_booking/ directory and updating the DocumentRoot directive to side-step any permissions issues.

Categories