Tags
Create Virtual Host Files
By default, Apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents to our new virtual host files.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.local.conf
# Make the relevant changes that reflect to the site.
sudo vi /etc/apache2/sites-available/mysite.local.conf
# Add the following scripts below to mysite.local.conf # Assign the app to a specific port 8081 <VirtualHost *:8081> ServerAdmin webmaster@mysite.local ServerName mysite.local:8081 ServerAlias www.mysite.local DocumentRoot /var/www/html/mysite.local/public_html ErrorLog /var/www/html/mysite.local/logs/error.log CustomLog /var/www/html/mysite.local/logs/access.log combined </VirtualHost> <VirtualHost *:80> ServerName local.helloworldapp.com ServerAlias local.helloworldapp.com DocumentRoot /var/www/html/helloworldapp ErrorLog /var/www/html/helloworldapp/logs/error.log CustomLog /var/www/html/helloworldapp/logs/access.log <Directory /var/html/helloworldapp> Options Indexes FollowSymLinks MultiViews # AllowOverride All # Order allow,deny # Allow from all Require all granted </Directory> </VirtualHost> ## This web app is running using php 5.6 <VirtualHost *:80> ServerName local.ci-photogallery ServerAdmin webmaster@localhost DocumentRoot /home/armano/www/ci-photogallery DirectoryIndex index.php # 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 <FilesMatch \.php$> # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server # if youre using php 7.0 # SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost" # if this web app is using php5.6/php5.6-fpm SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost" </FilesMatch> <Directory /home/armano/www/ci-photogallery> Options Indexes FollowSymLinks MultiViews # AllowOverride All # Order allow,deny # allow from all # Require all granted Require local </Directory> </VirtualHost>
Edit ports.conf, add port 8081 for our site
$ sudo vi /etc/apache2/ports.conf Listen 80 Listen 8081
If you want to use a custom domain name(e.g. mysite.com) follow the format use in helloworld app(See above). And then update the local hosts file.
$ sudo vi /etc/hosts 127.0.0.1 localhost 127.0.0.1 local.helloworldapp.com 127.0.0.1 local.ci-photogallery
Thats it..it should work.
After modifying the virtual hosts files, you can enable it now:
##To enable the site: $ cd /etc/apache2/sites-available $ sudo a2ensite mysite.local.conf $ service apache2 reload ##To disable the site: $ sudo a2dissite mysite.local.conf
Finally, restart the apache service.
sudo service apache2 restart # for Ubuntu 14.10 sudo systemctl restart apache2 # for Ubuntu 15.04 sudo /etc/init.d/apache2 restart # for Ubuntu 15.04
Thats it, the site is ready to serve!! Type the url in your browser
mysite.local:8081. If it does not appear then there’s something wrong in the configuration. Check the error log for debugging..
$ tail -f /var/log/apache2/error.log
How to list all vHosts Currently enabled In Apache?
$ apache2ctl -S # for Ubuntu/Debian $ httpd -S # for CentOS
If you encountered an error that says “apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message”
Solution:
$ sudo vi /etc/apache2/apache2.conf # Add this line and then restart it ServerName localhost $ sudo /etc/init.d/apache2 restart artheman@ub3c:/etc/apache2/sites-available$ sudo /etc/init.d/apache2 restart [ ok ] Restarting apache2 (via systemctl): apache2.service. artheman@ub3c:/etc/apache2/sites-available$ apache2ctl -S VirtualHost configuration: *:80 localhost (/etc/apache2/sites-enabled/000-default.conf:1) *:8081 ciblog.local (/etc/apache2/sites-enabled/ciblog.local.conf:1) ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/var/log/apache2/error.log" Mutex watchdog-callback: using_defaults Mutex default: dir="/var/lock/apache2" mechanism=fcntl Mutex mpm-accept: using_defaults PidFile: "/var/run/apache2/apache2.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="www-data" id=33 not_used Group: name="www-data" id=33 not_used