简体   繁体   English

Codeigniter 4部署到apache2

[英]Codeigniter 4 deployment to apache2

I've been trying to deploy my codeigniter 4 app into a local apache server.我一直在尝试将我的 codeigniter 4 应用程序部署到本地 apache 服务器中。

I want to be able to access my app through http://localhost/my_app/ .我希望能够通过http://localhost/my_app/访问我的应用程序。

I've set up the url in the.env file:我在 .env 文件中设置了 url:

app.baseURL = 'http://localhost/my_app'

Edited the .htaccess inside the public/ folder在 public/ 文件夹中编辑了 .htaccess

RewriteBase /public/

And created the config for apache并为 apache 创建了配置

<Directory "/var/www/html/my_app/public">
    AllowOverride all
    LogLevel debug
</Directory>

Alias /my_app /var/www/html/my_app/public

I can access the home page http://localhost/my_app , but when I try to go to another page http://localhost/my_app/bootstrapview , apache can't find the url.我可以访问主页http://localhost/my_app ,但是当我尝试 go 到另一个页面http://localhost/my_app/bootstrapview时,apache 找不到 url.

"The requested URL was not found on this server."

Have I missed something to make it work?我是否错过了让它发挥作用的东西? I don't want to change the position of the index.php file and it is most of the things I've found in my searching.我不想更改 index.php 文件的 position,这是我在搜索中发现的大部分内容。

When I try it with the spark server, it works well.当我在 spark 服务器上尝试时,它运行良好。

Edit: I'm using Ubuntu 18.04 and mod_rewrite is enabled.编辑:我正在使用 Ubuntu 18.04 并启用了 mod_rewrite。

If I don't use RewriteBase, it keeps getting redirected:如果我不使用 RewriteBase,它会不断被重定向:

redirected from r->uri = /my_app/public/index.php/public/index.php/public/index.php/public/index.php/public/index.php/bootstrapview 

I have found some answers while checking out other options.我在查看其他选项时找到了一些答案。

With this configuration, it will work if you change the RewriteBase to the URL of the app:使用此配置,如果您将 RewriteBase 更改为应用程序的 URL,它将起作用:

RewriteBase /my_app/

There is still a problem with redirections to the base URL " http://localhost/my_app/ " in which apache will not find the base route (in Routes.php -> "/") but you can create another route to point to it ("/home") for example.重定向到基本 URL“ http://localhost/my_app/ ”仍然存在问题,其中 apache 找不到基本路由(在 Routes.php ->“/”中),但您可以创建另一个路由指向例如它(“/home”)。

You should always create virtual hosts for your app.您应该始终为您的应用程序创建虚拟主机。 This way instead of working on localhost/foo your would be working on something like http://myapp.local and this way you can mimic a production environment way better and not have problems with uri segments and stuff like that.这种方式不是在 localhost/foo 上工作,而是在http://myapp.local 之类的东西上工作,这样你就可以更好地模拟生产环境,并且不会遇到 uri 段和类似问题。

To do this you must do the following.为此,您必须执行以下操作。

First Register the local domin myapp.local in your local environment首先在本地环境中注册本地domin myapp.local

$ sudo nano /etc/hosts 

add the line添加行

127.0.0.1 myapp.local

Then you should update your virtual host to the following:然后你应该将你的虚拟主机更新为以下内容:

<VirtualHost *:80>
    DocumentRoot /var/www/html/my_app/public
    ServerName myapp.local
    <Directory "/var/www/html/my_app/public">
        allow from all
        Options None
        Require all granted
    </Directory>
    RewriteEngine on
</VirtualHost>

After all this you MUST reboot your apache service在这一切之后你必须重新启动你的 apache 服务

$ sudo service apache2 restart

Now you can change your app to work with this local domain and you should do this for every app your need to work on your local apache environment.现在您可以更改您的应用程序以使用此本地域,您应该为您需要在本地 apache 环境中工作的每个应用程序执行此操作。

ps.附言。 You local domain can be whaterver you want it to be.您的本地域可以随心所欲。 myapp.local is just an example, it could be myapp.dev, myapp.loc anything you want. myapp.local 只是一个例子,它可以是 myapp.dev,myapp.loc 任何你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM