Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting document root for Laravel project on Apache virtual host

I inherited a php/Laravel app that was running on an Apache server that I don't have access to. My task is to get it running on another Apache server. I'm pretty good with php but relatively new to Laravel and very new to Apache configuration.

I have figured out how to get the Laravel app running on Apache that is running on an Ubuntu VM (VirtualBox.) I can access the Laravel app in a browser on the Ubuntu VM via http://localhost. I can also access the Laravel app in a browser from the Internet via http://appname.com/public. However, if I just use http://appname.com, then I just get a folder listing of /var/www/appname.

I have tried several modifications to the /etc/apache2/available-sites/appname.conf file but haven't quite got it right yet, apparently. I have also read a number of posts around the nets about making modifications to various other config files including php config files and Apache config files. It seems like these other mods (while they may be workable) shouldn't be necessary.

Here is my current /etc/apache2/available-sites/appname.conf

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName appname.com
        ServiceAlias www.appname.com
        DocumentRoot /var/www/appname/public

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

Any advise is appreciated.

  • Bob
like image 645
bcrimmins Avatar asked Dec 10 '22 07:12

bcrimmins


2 Answers

You need to allow the mod_rewrite in the apache server and allowSymLinks. Source

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName appname.com
    ServiceAlias www.appname.com
    DocumentRoot /var/www/appname/public

    <Directory "/var/www/appname/public">
            Options FollowSymLinks
            ReWriteEngine On
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

in the DocumentRoot Directory i would also allow MultiViews

<Directory "/var/www/appname/public">
        Options FollowSymLinks MultiViews
        ReWriteEngine On
</Directory>

You may need to also do

sudo a2enmod rewrite

to enable module rewrite.

Edit 1:

In my .conf files i got them with the quotes and they are working. Did you enable the modudle rewrite?

Besides some options i also have the "/" folder with the next config.

<Directory "/">
    Options FollowSymLinks
    AllowOverride All
    ReWriteEngine On
</Directory>

and here i'll write my full code of public directory

<Directory "/var/www/appname/public">
        Options FollowSymLinks MultiViews
        Order Allow,Deny
        Allow from all
        ReWriteEngine On
</Directory>

Try it and see if it works, after delete the options that you don't like to use.

like image 105
Ellesar Avatar answered Apr 07 '23 19:04

Ellesar


Follow the steps and all will be good and easy,

1). Type following command in terminal

cd /etc/apache2/sites-available

2). Make a new config file

sudo cp 000-default.conf appname.dev.conf

3. Open the new config file and paste the following code

<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 [email protected]
        ServerAlias appname.dev
        DocumentRoot /var/www/html/appname/public

        <Directory /var/www/html/appname/public>
           Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
            <FilesMatch \.php$>
               #Change this "proxy:unix:/path/to/fpm.socket"
               #if using a Unix socket
               #SetHandler "proxy:fcgi://127.0.0.1:9000"
            </FilesMatch>
        </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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

4). CTRL+x, then press y then press enter and run following command in terminal

sudo a2ensite appname.dev.conf

5). Type following command and edit the /etc/hosts file

sudo nano /etc/hosts

127.0.0.1 appname.dev

press CTRL x then press Enter and type following command

sudo service apache2 restart

6). Now your app will execute on appname.dev successfully.

like image 27
Shahrukh Anwar Avatar answered Apr 07 '23 19:04

Shahrukh Anwar