Install yii2 on raspbian


(flixbox) #1

Hi everyone,
first-time post here :slightly_smiling_face:

I’ve developed a small application on my windows host, and it’s working fine. Login, webcrawler, navigation, everything is working as expected (including pretty URLs).

Therefore, I’ve bought a raspberry pi3 to run my application within my local network. This is the setup:

  • mysql: Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
  • apache: Server version: Apache/2.4.25 (Raspbian)
  • php: PHP 7.0.30-0+deb9u1 (cli)

So I’ve copied my yii-basic (called yii) folder to /var/www/html, so I would expect to access it with http://[ip of my raspb]/yii .
That didn’t work, so I tried http://[ip of my raspb]/yii/web . That worked, but only for the index-page. As soon as I clicked a link, I got an 404 response.

So I googled and tried different stuff, until I changed
/etc/apache2/sites-enabled/000-default.conf

to the following settings:

<VirtualHost *:80>                                                                
    ServerAdmin webmaster@localhost                                           
    ServerName www.virtualdomain.local                                        
    DocumentRoot "/var/www/html/yii/web"                                      
    <Directory "/var/www/html/yii/web">                                       
       # use mod_rewrite for pretty URL support                               
       RewriteEngine on                                                       
       # If a directory or a file exists, use the request directly            
       RewriteCond %{REQUEST_FILENAME} !-f                                    
       RewriteCond %{REQUEST_FILENAME} !-d                                    
       # Otherwise forward the request to index.php                           
       RewriteRule . index.php                                                
                                                                              
       # use index.php as index file                                          
       DirectoryIndex index.php                                               
                                                                              
       # ...other settings...                                                 
   </Directory>                                                               
    ErrorLog ${APACHE_LOG_DIR}/error.log                                      
    CustomLog ${APACHE_LOG_DIR}/access.log combined                           
 </VirtualHost>

Then I’ve restarted the apache server and called http://[ip of my raspb]/ and yii was fully available, including links, actions etc.

Problem:
Within /var/www/html/ I’d like to have other web applications run as well. And with my current settings, I cant reach them.

Final Question therefore:
How do I have to configure the apache, so that I can reach my yii-mb application with http://[ip of my raspb]/yii/ ?

Thanks for your help in advance,
flixbox

Edit: I don’t think that’s a right problem. Im updating my application via “git checkout” and automatically run
sudo chmod -R 770 /var/www/html/yii/
sudo chown -R pi:www-data /var/www/html/yii/
after it.


#2

You need to configure rewrite rules for rewriting yii/web to yii if you want that.

The easier way to deal with it would be to have DocumentRoot point to /var/www/html to be able to access the other applications. Keep the <Directory "/var/www/html/yii/web"> config as they is now to allow accessing yii with pretty urls. Your yii application is in /yii/web/ now.


(flixbox) #3

Hi CeBe, thanks for your answer! I’ve tried this config:

<VirtualHost *:80>                                                                
ServerAdmin webmaster@localhost                                           
ServerName www.virtualdomain.local                                        
DocumentRoot "/var/www/html"                                      
<Directory "/var/www/html/yii/web">                                       
   # use mod_rewrite for pretty URL support                               
   RewriteEngine on                                                       
   # If a directory or a file exists, use the request directly            
   RewriteCond %{REQUEST_FILENAME} !-f                                    
   RewriteCond %{REQUEST_FILENAME} !-d                                    
   # Otherwise forward the request to index.php                           
   RewriteRule . index.php                                                
                                                                          
   # use index.php as index file                                          
   DirectoryIndex index.php                                               
                                                                          
   # ...other settings... [...]                                                            

after restarting the apache, I can reach (for example) http://[ip]/adminer/

yii is, as you said, avalaible with http://[ip]/yii/web, which is fine. BUT:

If I click on a link in my navbar, I get mixed results:

  • first nav button links to http://[ip]/yii/web/item -> works!
  • second nav button links to http://[ip]/crawlerdata -> does not work!
    (item and crawlerdata are two of my models)

this is how my navbar looks like in main.php:

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => [
        ['label' => 'Home', 'url' => ['/site/index']],    // working
        ['label' => 'Filme', 'url' => ['/item']],   // working

        !Yii::$app->user->isGuest ? (
        [
            'label' => 'Watchlist',
            'items' => [
                ['label' => 'labelA', 'url' => '/watchlist/create'], // doenst work
                ['label' => 'labelB', 'url' => '/watchlist'], // doenst work
                ['label' => 'labelC', 'url' => '/item/otherAction'], // doenst work
                ['label' => 'labelD', 'url' => '/item/otherAction2'], // doenst work
            ],
        ]
        ) : (['label'=>""]),
        [
            'label' => 'Statistik',
            'items' => [
                ['label' => 'labelE', 'url' => '/crawlerdata/st'], // doenst work
                ['label' => 'labelF', 'url' => '/crawlerdata'], // doenst work
            ],
        ],
        Yii::$app->user->isGuest ? (
            ['label' => 'Login', 'url' => ['/site/login']] //works
        ) : (
            '<li>'
            . Html::beginForm(['/site/logout'], 'post')
            . Html::submitButton(
                'Logout (' . Yii::$app->user->identity->username . ')',
                ['class' => 'btn btn-link logout']
            )
            . Html::endForm()
            . '</li>'
        )
    ],
]);
NavBar::end();

So basically, the links within a dropdown of the menu bar don’t work. The links that are directly (first level) within the navbar without dropdowns are working fine.

Edit: I just tried moving one of the links within the dropdown to first level of the navbar. And suddenly this one is working, while the same link within the dropdown still leads to [ip]/module/action.

Any ideas?


(flixbox) #4

Ok. Problem between chair and keyboard.

I changed

'items' => [
            ['label' => 'labelA', 'url' => '/watchlist/create'], // doenst work
            ['label' => 'labelB', 'url' => '/watchlist'], // doenst work
            ['label' => 'labelC', 'url' => '/item/otherAction'], // doenst work
            ['label' => 'labelD', 'url' => '/item/otherAction2'], // doenst work
        ],

to

'items' => [
            ['label' => 'labelA', 'url' => ['/watchlist/create']], 
],

and everything seems fine. Damn…
I’ve tried many apache configurations and the mistake was within my navbar. I’m just wondering why this wasn’t an issue on dev environment.

Anyways, thanks && problem solved!


#5

Yeah, you need to wrap the URLs in arrays to make them Yii routes instead of absolute URLs.

You probably worked on a host without subdir, where absoulte URLs work the same as default URL created for Yii routes.