'How to host multiple laravel projects on single AWS EC2 instance?
I have 3 laravel projects namely project1, project2, project3 i want to host these three of them on single AWS EC2 instance. I made seperate folders for each project in /var/www/html directory. When i try hitting IPAddress/project1/public it works fine as expected all the routes works fine. But i want to run without public at end. So i tried modifying 000-default.conf file in /etc/apache2/sites-available directory with:
<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
Alias /project1 /var/www/html/project1/public
Alias /project2 /var/www/html/project2/public
Alias /project3 /var/www/html/project3/public
<Directory /var/www/html/project1>
AllowOverride All
</Directory>
<Directory /var/www/html/project2>
AllowOverride All
</Directory>
<Directory /var/www/html/project3>
AllowOverride All
</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
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>
Then the websites opens fine as expected without /public at end but the routes didn't work. It gives Internal server error. The server encountered an internal error or misconfiguration and was unable to complete your request.
I just want to know how to host multiple laravel applications on single EC2 instance.
Solution 1:[1]
You will need to create virtual hosts file for each of your projects. You can do this by copying the 000-default.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/project1.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/project2.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/project3.com.conf
Then in each individual virtual host file (i.e. for each project) you need to set the proper values for ServerName, ServerAlias and DocumentRoot. As an example
# Project 1: /etc/apache2/sites-available/project1.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName project1.com
ServerAlias www.project1.com
DocumentRoot /var/www/html/project1/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# Project 2: /etc/apache2/sites-available/project2.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName project2.com
ServerAlias www.project2.com
DocumentRoot /var/www/html/project1/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# Project 3: /etc/apache2/sites-available/project3.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName project3.com
ServerAlias www.project3.com
DocumentRoot /var/www/html/project3/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then enable the 3 sites
sudo a2ensite project1.com.conf
sudo a2ensite project2.com.conf
sudo a2ensite project3.com.conf
#disable the default
sudo a2dissite 000-default.conf
Restart apache and probably you may need to setup local hosts file /etc/hosts
Assuming that you don't own the domains for the three projects and are just in development phase, you can specify in the hosts file of your local (development) machine to connect to the specified IP Address whenever you try to visit project1com or project2.com so on
# /etc/hosts file
127.0.0.1 localhost
YOUR_SERVER_IP project1.com
YOUR_SERVER_IP project2.com
YOUR_SERVER_IP project3.com
Solution 2:[2]
You can simply create the Alias in your default config, but outside the Viruatlhost and after Directory tag, Here are the steps,
Open 000-default.conf
sudo nano /etc/apache2/sites-available/000-default.conf
Outside Viruatlhost and after Directory tag add the below code and replace the Alias(PROJECT2) and your directory name(PROJECT2)
Alias /PROJECT2 "/var/www/html/PROJECT2"
<Directory /var/www/html/PROJECT2>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Restart apache
sudo service apache2 restart
Finally, It will look like,
...
</VirtualHost>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Alias /PROJECT2 "/var/www/html/PROJECT2"
<Directory /var/www/html/PROJECT2>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Solution 3:[3]
well the first step is to enable a subdomain in route 53:
subdomain.example.com -> Value/Route traffic to(your machine ip or alias to aplication load balancer)
next: copy the default conf for the another project:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/project2.conf
whitin project2.conf set server name and documentRoot
ServerName subdomain.example.com
DocumentRoot /var/www/html/project2/public
last: enable domain2.conf
sudo a2ensite domain2.conf
make sure that you have the project in the documentRoot path and restart apache.
sudo service apache2 restart
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mahak Choudhary |
| Solution 3 | lemon |
