CodeIgniter 4: Setup Virtual Host

So.. yeah here we are again, with a new post. It is not actually about CodeIgniter but I decided to move further with our previous project code setup, we discussed from previous post.

The Virtual Host

Let us get this straight, instead of using http:localhost/ci4/public we will use something like http://dev.ci4.local . That looks pretty neat. I am on Ubuntu so will share linux commands to get the desired url.

Assuming that you have apache setup in default directory you would need to create a file /etc/apache2/sites-available/ci4.conf with the following command

sudo gedit /etc/apache2/sites-available/ci4.conf

Enter your password and add the following code to your file

<VirtualHost *:8080>
	ServerName dev.ci4.local
	ServerAlias dev.ci4.local
	DocumentRoot /var/www/html/ci4/public
	<Directory /var/www/html/ci4/public>
	    Options Indexes FollowSymLinks MultiViews
	    AllowOverride All
	    Require all granted
	</Directory>
</VirtualHost>

The path /var/www/html/ci4/public” is the path to my local project directory. After this you would need to enable this site file on apache, for that run the following code:

sudo a2ensite ci4.conf

Then simply restart apache server with the following command

sudo service apache2 restart

Please do check the status of apache server with

sudo service apache2 status

If apache service is dead, there is probably something wrong with your config file you created. If all went well you should now be able to access your project via http://dev.ci4.local. If that does not work for you, there is a last trick to get done. Open a file:

sudo gedit /etc/hosts

Carefully add the following line to the file:

127.0.0.1	dev.ci4.local

Save and close the file. Restart the browser to check the url again.

Get back to project

Now since url is all setup let us get back to our project. Please do read previous blog before moving further. Update the value for baseurl in file .env

app.baseURL = 'http://dev.ci4.local/'

After this our previous links should be accessible at:

http://dev.ci4.local/user/add

and

http://dev.ci4.local/user/list

That is all for now. New post coming soon.

Cover Photo by Agnieszka Boeske on Unsplash

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.