404 Page Not Found: Codeigniter Project On Apache2 Ubuntu
Some of you might have encountered this problem while setting up CodeIgnitor Project on your local machine or server. Let me explain the problem scenario.
The Problem
I had to make some changes to the existing codeigniter project. I downloaded the project, extracted the archive and moved the project to /var/www/html/
directory. Normally, any PHP project inside /var/www/html/
would work perfectly ( putting the database and other configurations aside) and this project was also supposed to work. But, I didn’t have any experience on CodeIgniter specifically. Anyway, I, then tried to launch a browser and entered a url I usually would: http://localhost/project_folder
. But immediately it threw the 404 Not Found Error. I tried other routes as well but none worked.
I, then wondered if it work on xampp server and moved folder to /opt/lampp/htdocs/
. I stopped apache2 using sudo service apache2 stop
and started xampp using sudo service xampp start
. I don’t know why but it worked like a charm.
I could just stick to xampp and proceed to work but I didn’t get satisfied about that. I was curious about not working on apache2 and working on xampp. So, I started reading some dev articles and found a solution. Let’s talk about the solution.
404 Page Not Found: Codeigniter Project On Apache2 Ubuntu | Solution
The solution is to create a virtual host environment and run the project on that environment. Let me break down the solution into steps so that its easier to proceed.
Part 1
- Create A Virtual Host Environment
Open your Terminal and go to this directory:/etc/apache2/sites-available
. You might have to usesudo
if do not haveroot
privilege. - Then create a new virtual host configuration, For now, we will name this configuration testproject. Use the following command:
sudo vim testproject.conf
Paste the following into the file:
<VirtualHost *:80>
ServerName testproject.local
DocumentRoot /var/www/html/testproject
<Directory /var/www/html/testproject/>
AllowOverride All
Require all granted
Allow from all
</Directory>
</VirtualHost>
Now, save the file and exit to the terminial.
Part 2
- Edit the
/etc/hosts
(hosts file) usingsudo vim /etc/hosts
) and add the following line at the end of file:127.0.0.1 localhost testproject.local
Now save the file and exit vim. - Execute the following commands:
sudo a2ensite testproject.conf
sudo a2enmod rewrite
- Restart the apache2:
sudo service apache2 reload
orsudo service apache2 restart
We are all done now. Try to access your application using http://testproject.local
. You should be able to see the CodeIgniter pages you were supposed to.