how to create a virtual host in ubuntu 14.04 - php

I'm using ubuntu 14.04 operating system and I want to create a virtual host before starting my php project. If any one can give me the terminal commands I would be grateful.
I have already installed necessary installations and I'm using Symfony php framework.

Open a terminal or a console and write the following commands
Go to your sites-available apache2 folder with:
cd /etc/apache2/sites-available/
Copy your 000-default.conf to another file (with the name of your site or vhost Preferably)
sudo cp 000-default.conf example.conf
Open that new file (example.conf) with your best text editor or use:
sudo nano example.conf
Change the example.conf's content with (Ignore the comments):
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerAdmin webmaster#mail.com
ServerName example.com
ServerAlias www.example.com
# example.com is the folder where you have to put your site's code.
# Obviously, its name can be whatever you want
DocumentRoot /var/www/html/example.com
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
# Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Enable the site as follows:
sudo a2ensite example.com
Restart your apache2 web server
sudo /etc/init.d/apache2 restart
Edit your /etc/host file with:
sudo nano /etc/hosts
Add at the end of the file, the domain example.com and www.example.com. It must look like:
127.0.0.1 localhost
...
# you can replace with your IP
127.0.0.1 example.com
127.0.0.1 www.example.com
...
Open your best web browser and write in its address bar: www.example.com or example.com

Create virtual host
sudo nano /etc/hosts
Enter Password=>
create ip and domain Name=>
127.0.0.1 testsite.lk
create project folder=>
Go to that directory comand prompt=>
sudo chmod -R 777 testsite.lk
to asign project to group=>
:~/mayura$
> sudo chown -R www-data:www-data testsite.lk/
give permission=>
:~/mayura$
sudo chmod -R 777 testsite.lk/
give document root file for vertual host
:~/mayura$
cd /etc/apache2/sites-available/
sudo nano testsite.lk.conf
<VirtualHost *:80>
DocumentRoot /home/mayura/project/testsite/web
DirectoryIndex index.php
ServerName testsite
<Directory "/home/mayura/project/testsite/web">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>
Create New project on Netbeans in that folder=>
index.php
Enable the site=>
cd /etc/apache2/sites-available
sudo a2ensite testsite.lk.conf
Apache reload=>
sudo /etc/init.d/apache2 reload
Start Browser=>
Run=>
xdg-open http://testsite.lk

Related

Laravel project on ubuntu server :HTTP ERROR 500

My laravel project works fine in development machine, but when i uploaded to ubuntu server i will get HTTP ERROR 500.
Here is things i've done.
1.Uploaded the entire contents of <laravel_project> folder into the var\www\html folder.
when i try to access domain.com i will get the list of directories instead of home page,when entering on public folder i will get HTTP ERROR 500 error.
Also how can get the index page without accessing the public folder (ie i need to enter to my site while entering domain.com ) .
There can be multiple cases
Please check you can storage write permission to storage folder
You need to define path to public in your apache configuration to be able to open without public
Server does not meet the minimum requirements of Laravel
Please try above and let me know
You have to change Document root directory in apache configure file.
File Location in Ubuntu Server is :
1) $ cd /etc/apache2/sites-available 2) $ gedit 000-default.conf
Find the default Directory Root and modify it with your project_directory / public_directory.
i.e. DocumentRoot /var/www/html/project_dir/public/
See this code for referance :
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/project_dir/public_dir
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
After edit this file. Reload apache server once.
sudo service apache2 reload

apache virtual host with laravel 5 "Internal error 500"

I want to learn laravel, and recently installed a fresh copy laravel on an amazon ec2 instance through composer composer create-project laravel/laravel laravel-app with apache2, and php installed. I configure the conf.d file to change the document root to laravel-folder/public, when i try to go to the public ip address it show a server 500 error.
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#elastic IP address
ServerName 42.66.33.52
ServerAdmin webmaster#localhost
DocumentRoot laravel-app/public
<Directory laravel-app/public>
DirectoryIndex index.php
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
I've tried chmod 777 to the entire laravel folder and changed the url in app.php to 42.66.33.52, and it still showing the same error. I'm new to laravel, and I hope someone can point me to the right direction. I've host other PHP website from this server without using laravel framework and it is function normally.
You should chmod -R 775 on /storage folder and all files in it.
as #Alexey Mezenin said, you should change the chmod of storage and all files inside by -R, and also don't forget to do the same with the bootstrap/cache folder. Check that on "Directory Permissions" in the official laravel documentation installation :
https://laravel.com/docs/5.3/installation
hope that help ;) Regards.

How to point default virtual host in Ubuntu 14.04 LTS

I recently purchased linode hosting where i did successfully hosted a website. And its opening by domain-name.com and also when i enter server IP it opens default /var/www/domain-name.com/public_html
Now i want to host another site in it and created another virtual host with domain-name-2.com and enabled that and did reload my apache server.
Now when i enter again the server IP in browser like http://11.22.33.44/ it shows the first website (/var/www/domain-name.com/public_html) i hosted by default.
But i want to point some other folder so that when i hit http://11.22.33.44 it will show that folder's index.html not /var/www/domain-name.com/public_html
How to do that?
Not sure if I understand correctly, you want
http://11.22.33.44/ -> /var/www/domain-name.com/some_other_folder
http://domain-name.com/ -> /var/www/domain-name.com/public_html
Am I correct?
If that is the case, go to /etc/apach2/site-available
in 000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain-name.com/some_other_folder
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Create a new file called "domain-name.com.conf" in the same folder
Write the following into the file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain-name.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
To enable virtual host, type into terminal:
$ sudo a2ensite domain-name.com
Basically 000-default.conf wasn't working as it was disabled.
enable it by following command -
sudo a2ensite 000-default.conf
I was wondering why it opening my 000-domain-name.com.conf it's because may be as default conf has been disabled so apache search for other conf file alphabetically as act that as default conf.
Also if you want to disable default conf
sudo a2dissite 000-default.conf
Both above command require apache to be restarted by following command -
sudo service apache2 restart

apache2 multiple virtualhost but only first one works

I am running Apache/2.4.12 on Ubuntu and I added 2 .conf files in the /etc/apache2/sites-available/ folder, and enabled them. Both sites are using the same Symfony PHP files.
aaa.conf
Listen 8080
<VirtualHost *:8080>
ServerName local.aaa.com
ServerAlias www.local.aaa.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/aaa/web
<Directory "/var/www/html/aaa/web">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/aaa_error.log
CustomLog ${APACHE_LOG_DIR}/aaa_access.log combined
</VirtualHost>
bbb.conf
Listen 8081
<VirtualHost *:8081>
ServerName local.bbb.com
ServerAlias www.local.bbb.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/bbb/web
<Directory "/var/www/html/bbb/web">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/bbb_error.log
CustomLog ${APACHE_LOG_DIR}/bbb_access.log combined
</VirtualHost>
The problem I am having is that after a service apache2 reload only the first site that I open works. So for example:
1st scenario
- I do the reload
- I open local.aaa.com:8080 it works
- I open local.bbb.com:8081 it doesn't work, I get a PHP Fatal error: Class 'AppBundle\\AppBundle' not found
2nd scenario
- I do the reload
- I open local.bbb.com:8081 it works
- I open local.aaa.com:8080 it doesn't work, I get a PHP Fatal error: Class 'AppBundle\\AppBundle' not found
EDIT
#Ryan Vincent:
netstat -a shows both sites runnnig
All your Listen directives should be in the main apache configuration file provided by Ubuntu(/etc/apache2/apache2.conf).
The sites-enabled folder must only contain the proper symbolic links generated with a2ensite to sites-available, which is where you have to place your virtualhost configurations.
Once you moved them there, enable every virtualhost and reload apache.
sudo a2ensite aaa.conf
sudo a2ensite bbb.conf
sudo service apache2 reload
I had the same problem.
It has been solved by adding a rule on my firewall (UFW).
The ports i use are 18180 and 18181.
Connexion on port 18180 was OK, but not with 18181.
I tried with UFW disabled, but it didn't work, so i decided to add the rules "allow 18181/tcp" and "allow 18181/udp" and the problem is solved.
It's weird it didn't work with UFW disabled, but it's like that lol
I hope this will help

VirtualHost is not working under Apache 2.4.6 on CentOS 7

I'm trying to setup some VH in Apache 2.4.6 on CentOS 7 but without success since it's not working. This is what I've tried til now:
Since in /etc/httpd/conf/httpd.conf is this line Include conf.modules.d/*.conf then I create a file under /etc/httpd/conf.d/vhost.conf and place this inside it:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName webserver
ServerAlias localhost devserver development
DocumentRoot /var/www/html
</VirtualHost>
Reload/Restart Apache service (tried both):
service httpd reload|restart
At Windows side edit the file C:\Windows\system32\drivers\etc\hosts and add this line:
192.168.3.131 webserver localhost devserver development # this is the IP of Apache Server
Open the browser and tried: http://webserver, http://devserver and both goes to default Apache page so VH is not working.
Place a file under /var/www/html/index.php with this lines <?php phpinfo(); ?> just to know which modules is Apache loading, this is the result:
core mod_so http_core mod_access_compat mod_actions mod_alias mod_allowmethods mod_auth_basic mod_auth_digest
mod_authn_anon mod_authn_core mod_authn_dbd mod_authn_dbm mod_authn_file mod_authn_socache mod_authz_core
mod_authz_dbd mod_authz_dbm mod_authz_groupfile mod_authz_host mod_authz_owner mod_authz_user mod_autoindex
mod_cache mod_cache_disk mod_data mod_dbd mod_deflate mod_dir mod_dumpio mod_echo mod_env mod_expires mod_ext_filter
mod_filter mod_headers mod_include mod_info mod_log_config mod_logio mod_mime_magic mod_mime mod_negotiation
mod_remoteip mod_reqtimeout mod_rewrite mod_setenvif mod_slotmem_plain mod_slotmem_shm mod_socache_dbm
mod_socache_memcache mod_socache_shmcb mod_status mod_substitute mod_suexec mod_unique_id mod_unixd mod_userdir
mod_version mod_vhost_alias mod_dav mod_dav_fs mod_dav_lock mod_lua prefork mod_proxy mod_lbmethod_bybusyness
mod_lbmethod_byrequests mod_lbmethod_bytraffic mod_lbmethod_heartbeat mod_proxy_ajp mod_proxy_balancer mod_proxy_connect
mod_proxy_express mod_proxy_fcgi mod_proxy_fdpass mod_proxy_ftp mod_proxy_http mod_proxy_scgi mod_systemd mod_cgi mod_php5
And apparently mod_vhost is loaded but is not working, did I miss something? Any help or advice around this? Maybe I forgot something but I read Apache docs and doesn't found something helpful
Update: test1
I made some changes to VH definition and now this is what I have:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName webserver
#ServerAlias localhost devserver development
<Directory "/var/www/html">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
Allow from all
#Require local
#Require 192.168.3.0/16
#Require 192.168.1.0/16
</Directory>
</VirtualHost>
But I'm getting a 403 Forbidden
Forbidden
You don't have permission to access /index.php on this server.
What is failing here?
To elaborate on jap1968's post, CentOS 7 comes with SELinux's pain in the butt level set to enforcing. This causes all kinds of confusion when perfectly normal service configuration silently fail (Apache).
To disable SELinux you'll need to:
0) [optional] Crack open a shell and become root... or enjoy a shiny new, super fun, configuring sudo to let you do "root stuffs" project. Probably.
su -l
1) Get the current status of SELinux. Run sestatus:
sestatus
2) If SELinux is causing hair loss and premature aging you'll get something like this:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
3) Edit the /etc/selinux/config file. Change SELINUX=enforcing to SELINUX=permissive. Doing this will set you up for endless joy next time you reboot. You'll end up with something like this:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# SELINUX=enforcing
# ===> VOODOO HERE <===
SELINUX=permissive
# ===> END VOODOO <===
#
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
4) Disable SELinux. You can reboot at this point but it's easier to tell SELinux to take time off from tormenting you. Run setenforce to reset SELinux's enforcement level to match the /etc/selinux/config file:
setenforce 0
5) Check sestatus again:
sestatus
If everything went as expected sestatus will return something like this:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: permissive
Mode from config file: permissive
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
6) Restart Apache. If your vhost's domain name is resolving to the server you're working on you'll see your shiny new virtual host:
# Restart apache:
systemctl restart httpd.service
# Be lazy by checking your virtual host from the command line:
curl www.example.com/new-file-that-only-exists-in-your-new-vhost.txt
6.5) Stop reading here. Or don't. I'm a message board post, not your Mom.
Everything below is beyond the scope of the original question and only included because you really should be running with SELinux enabled.
7) Work towards re-enabling selinux. Start by watching the selinux logs to see some awesome alphabet soup:
tail -f /var/log/audit/audit.log
8) Be amazed at the depth of features, crazy number of poorly named utilities, and ugly UX mess that constitutes SELinux. You should probably put on your big boy pants and drink a whole pot of coffee before you dive in. Here's some Info:
CentoOS HowTo: https://wiki.centos.org/HowTos/SELinux
Project: http://selinuxproject.org/page/Main_Page
A couple of thing that may be causing you problems :-
NameVirtualHost *:80
Is no longer a valid syntax for Apache 2.4.x you should remove it completely.
On the Windows side once you have changed the HOSTS file, you need to reload the DNS Client service, so either reboot or better still, launch a command window using "Run as Administrator" and do this :-
net stop dnscache
net start dnscache
Lastly, within your virtual hosts definition, it will help to tell apache from where it is allowed to accept connections to this Virtual Host like so :-
<VirtualHost *:80>
ServerName webserver
ServerAlias localhost devserver development
DocumentRoot /var/www/html
<Directory "/var/www/html">
AllowOverride All
Require local
Require ip 192.168.3
</Directory>
</VirtualHost>
This will allow access from the machine running apache Require local and from any ip address on the local network Require ip 192.168.3
Also I am not sure where Apache on unix puts its default document root but it might be an idea to differentiate your 3 domain names to different directories like so
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot /var/www/html
<Directory "/var/www/html">
AllowOverride All
Require local
Require ip 192.168.3
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName webserver
ServerAlias webserver
DocumentRoot /var/www/html/webserver
<Directory "/var/www/html/webserver">
AllowOverride All
Require local
Require ip 192.168.3
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName development
ServerAlias development
DocumentRoot /var/www/html/development
<Directory "/var/www/html/development">
AllowOverride All
Require local
Require ip 192.168.3
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName devserver
ServerAlias devserver
DocumentRoot /var/www/html/devserver
<Directory "/var/www/html/devserver">
AllowOverride All
Require local
Require ip 192.168.3
</Directory>
</VirtualHost>
Then put a simple html file in each directory saying 'Hello from Servername' and change servername in each file so you know you have got to the correct server.
RE: Update test1.php
Allow from all
Is not valid Apache 2.4 syntax either, unless you have loaded LoadModule access_compat_module modules/mod_access_compat.so
Even then it should be
Order Allow,Deny
Allow from all
So USE Apache 2.4 syntax
Require all granted
If you want to take the lazy route and allow access from the universe.
Be careful also with SELinux. The default configuration will prevent your virtual hosts directories from being accessed by httpd. You will need to set the appropriate context:
# chcon -R -u system_u -r object_r -t httpd_sys_content_t <DocumentRoot>
Another option is just to disable SELinux.

Categories