Virtual hosts not working properly in ubuntu LAMP stack - php

I have created virtual hosts in the following ways:
Created laravel.test.conf as
- cd /etc/apache2/sites-available
- sudo nano laravel.test.conf
And then updated laravel.test.conf as
<VirtualHost *:80>
ServerName laravel.test
ServerAlias www.laravel.test
ServerAdmin admin#laravel.test
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run command: sudo a2ensite laravel.test
Permission provided:
sudo chown -R www-data: /var/www/laravel
sudo chown -R $USER:$USER /var/www/laravel
sudo chmod -R 755 /var/www
Added sitename on etc/hosts
127.0.0.1 laravel.test www.laravel.test
Reloaded apache sudo systemctl reload apache2
Run configtest sudo apachectl configtest and received Syntax Ok
Restarted apache server sudo systemctl restart apache2
Now, when I hit larave.test it loads the site successfully, but when ever I tried to hit any other route then it returns 404 error as shown in image.
This app is the freshly installed laravel one with laravel breeze so I am pretty much confident that there is a valide route. On top of that, if it was laravel, error, it would have been log it on app level and there would be different UI for 404 pages.
I have checked error log on laravel/storage/logs as well as apache2 logs on var/log/apache2/error.log but both of these logs file are empty.

Make sure your rewrite mod is enabled.
or simply run this in terminal
$ sudo a2enmod rewrite
$ sudo systemctl reload apache2

Related

Apache + PHP (multi version) + multi sites (with different PHP versions) + user access setup

I was searching the internet looking for solution, unfortunately none of those was helpful.
I am trying to achive the following:
On Linux Ubuntu 20.04 setup several sites. Each site may have different PHP version. Also users of each site must not be able to read/write files on other sites. Each site should have own group/user.
I see the solution this way (and was trying this way):
to create different folders with different group/users (site1/site1, site2/site2)
install Apache, PHP as FCGI
enable MPM module to be able to run script on behalf of different user on each site
What I could do so far. I could install Apache + PHP (+FCGI) with different versions. So far it was ok, no problem.
Then I need to separate users I enabled MPM modules of Apache. The best result I could get is to run PHP script as if on behalf of different users. But when PHP script is trying to create a file in the same folder I can see the owner/group as www-data. So security issues are not resolved.
I not not sure that enabling MPM is working as it should.
Since there is a lot of work done, I didn't want to show all long list of commands here. I hope someone could advise the correct guide I could look at.
If not helpful I'll show all here
UPD 1: (I coudn't find a proper way to show code in reply comment, so I do this way)
sudo apt-get install apache2
sudo apt-get install php7.4
sudo apt-get install libapache2-mpm-itk
sudo useradd -m --shell /bin/bash --home /home/u-site1 u-site1
sudo passwd u-site1
sudo vi /etc/apache2/sites-available/site1.local.conf
-- start of content
<VirtualHost *:80>
ServerName site1.local
ServerAdmin info#site1.local
DocumentRoot /home/u-site1/site1.local/html
CustomLog ${APACHE_LOG_DIR}/site1.local.log combined
ErrorLog ${APACHE_LOG_DIR}/site1.local-error.log
<IfModule itk.c>
AssignUserID u-site1 u-site1
</IfModule>
<Directory "/home/u-site1/site1.local/html">
Options All -Indexes
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
</VirtualHost>
-- end of content
sudo mkdir -p /home/u-site1/site1.local/html
sudo a2ensite site1.local.conf
sudo vi /home/u-site1/site1.local/html/index.php
-- start of content
site1.local
<?php
$sFilePath = __DIR__ . "/new.file";
$bRes = file_put_contents($sFilePath, "TEST");
if($bRes === false)
echo "<pre>", var_export(error_get_last(), 1), "</pre>";
phpinfo();
-- end of content
sudo chown -R u-site1:u-site1 /home/u-site1/site1.local
sudo chmod 755 /home/u-site1/site1.local
sudo systemctl restart apache2

Can't route to CakePHP Controllers with Apache

I have my own digitalocean droplet setup and followed this tutorial to a T to get Cakephp working
Setup CakePHP with LAMP Stack
Everything works exactly as written in that tutorial besides for 'Step 6' - Creating the Article user interface. (Books in my case)
After using the bake all command and navigating to `/books' I get a 404 not found
As you can see CakePHP is setup and working at the top level URL, however when trying to move past that to a Controller that definitely exists it can't find it.
After some more digging, I found the issue. Apparently mod_rewrite is turned off by default on Ubuntu
`sudo a2enmod rewrite
systemctl reload apache`
is the solution here
Make sure your rewrite mod is On
If you are using Linux goto /etc/apache2/sites-enabled -> open 000-default.conf file
<VirtualHost *:80>
....
// add these lines
<Directory /var/www/html>
#Options FollowSymLinks
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order deny,allow
Allow from all
</Directory>
....
<</VirtualHost>
Save it.
restart your server: sudo service apache2 restart
give your project permissions by giving this command:
sudo chown -R user_name:www-data project_name/
sudo chmod -R 755 project_name/
cd project_name
chown -R www-data tmp
chown -R www-data logs
chmod -R 777 tmp
chmod -R 777 logs

How to get Nightwatch tests running in CircleCI?

I am trying to get Nightwatch tests running in CircleCI and it has been a bit of a ...nightmare
It seems that CricleCI is not set up to run a webserver for a PHP app.
CircleCI version of the Chrome browser ~54 is not compatible with Nightwatch, which is asking for >= ~55
CircleCI's Chrome cannot find my local.webapp.dev domain, and gives the error ERR_ICANN_NAME_COLLISION
I have setup the web server, using the following apache config, modified from the recommended version in the CircleCI docs:
<VirtualHost *:80>
LoadModule php5_module /opt/circleci/php/5.6.17/libexec/apache2/libphp5.so
DocumentRoot /home/ubuntu/phpwebapp
ServerName local.webapp.dev
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
</VirtualHost>
After much trial and error, I finally have this working:
File examples:
circleApache.conf
circle.yaml
Nightwatch.json
package.json
The tests are run automatically by Circle using the package.json:
"test": "./node_modules/.bin/nightwatch --env circleci"
This picks up and runs the tests from your Nightwatch.json:
"circleci" : {
"output_folder" : "${CIRCLE_TEST_REPORTS}",
"launch_url" : "http://local.phpwebapp.dev",
"selenium_host" : "localhost",
"selenium_port" : 4444,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "chrome",
"marionette": true
}
}
CircleCI machines are using an older version of Chrome which is not compatible with the current version of Selenium/Nightwatch. Chrome needs to be updated in the pre dependencies of circle.yaml:
dependencies:
pre:
# Update Google Chrome.
- google-chrome --version
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb stable main" >> /etc/apt/sources.list.d/google.list'
- sudo apt-get update
- sudo apt-get --only-upgrade install google-chrome-stable
- google-chrome --version
The Circle docs forget an important piece in the apache conf file, you must set the allow/deny rules for your webroot directory, the port is also changed to use the default port 80:
<VirtualHost *:80>
LoadModule php5_module /opt/circleci/php/5.6.17/libexec/apache2/libphp5.so
DocumentRoot /home/ubuntu/phpwebapp
ServerName local.phpwebapp.dev
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
<Directory /home/ubuntu/phpwebapp>
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
You must then activate all the required Apache modules and load your conf into Apache, using circle.yaml:
dependencies:
...
post:
# circle seems to expect this but doesnt install it
- sudo apt-get install libapache2-mod-php5
# copy apache config file
- sudo cp ~/phpwebapp/circleApache.conf /etc/apache2/sites-available
# give phpwebapp to apache
- sudo chown -R www-data:www-data ~/phpwebapp
- sudo a2enmod rewrite
- sudo a2enmod headers
- sudo a2ensite circleApache
# DocumentRoot doesnt work, so symlinking instead
- sudo rm -r /var/www/html
- sudo ln -s /home/ubuntu/phpwebapp /var/www/html
- ls /var/www/html
- sudo service apache2 restart
# add local.phpwebapp.dev to /etc/hosts
- sudo sh -c "echo 127.0.0.1 local.phpwebapp.dev >> /etc/hosts"
The a2enmod lines enable the necesary apache modules rewrite and header for the PHP app.
a2ensite enables the configuration file and your domain. Certain domains, like *.dev will also require adding a line to /etc/hosts:
- sudo sh -c "echo 127.0.0.1 local.phpwebapp.dev >> /etc/hosts"
This was realized when the Circle Chrome browser was giving the error ERR_ICANN_NAME_COLLISION. The error was uncovered by printing the source of the test page via Nightwatch:
browser
.url("http://www.local.phpwebapp.dev")
.source(function (result){
// Source will be stored in result.value
console.log(result.value);
})

How can I enable moodle?

I've installed postgres for moodle and I got the following instructions:
Apache/mod_php setup
Give full access to the project directory to the process running
apache. You might also have to set the x-flag on all .php files.
$ chmod -R a+rw * Create a 'moodle.vhost.conf' from moodle.vhost.conf.example in project directory. * Create a symlink
from /usr/local/var/moodle to the project directory.
$ cd /usr/local/var; ln -s moodle * Also symlink the new virtual host into apache config dir.
$ cd /private/etc/apache2/other
$ sudo ln -s /moodle.vhost.conf moodle.conf
$ sudo apachectl restart * Create your own custom /etc/vcmoodle/config_local.php file for things local to your
environment by copying, and modifying if required, the example file.
$ sudo mkdir -p /etc/vcmoodle
$ sudo cp /config_local.php.example /etc/vcmoodle/config_local.php
Still it won't work. When I look at my apache root then it comes just the standard apache page and no moodle. I've this sites-available
:/etc/apache2/sites-available$ ls
000-default.conf default-ssl.conf moodle.conf
and the file moodle.conf contents
<VirtualHost *:80>
DocumentRoot "/usr/local/var/moodle"
ServerName moodle.localdev.kth.se
ErrorLog /usr/local/var/moodle/error_log
LogLevel debug
<Directory "/usr/local/var/moodle">
AllowOverride all
Order allow,deny
Allow from 127.0.0.1
</Directory>
</VirtualHost>
But I don't get a moodle page at my web server. What am I doing wrong?
if youre running Ubuntu (judging by the tag), you need to put your project directory inside /var/www. Apache cant access project folders inside your specified directory unless you change the users/groups etc.
what youve tried is the normal apache default location for web folders, but Ubuntu like to change things.

Setup laravel app on digitalocen

I an trying to get my Laravel site running on Digital Ocean. My laravel app is static so no SQL is used and I developed it locally with Homestead.
I can get the site showing the home page on IPaddress /public. It won't link to any pages though. I am guessing it is some rewrite problem I am having. I want to get rid of the /public and get it linking to other pages. Then worry about making it work on a domain
I followed PHPAcademys Cloud Server Set-up tutorial https://www.youtube.com/watch?v=1-ok9d_6xrc
The only difference is I used UBUNTU 14.04 x64. While following the tutorial I found I had a html folder inside /var/www. I needed to put my app within the html folder.
Code setup
sudo apt-get update
sudo apt-get install apache 2
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
I didnt use mysql because i dont need it for this app. Its a static site with laravel.
It said mcrypt was needed so I used
sudo php5enmod mcrypt
sudo service apache2 restart
Then I was getting an exception handler error, so i fixed that with
chgrp -R www-data /var/www/html
chmod -R 775 /var/www/html/app/storage
You need to setup an apache2 virtual host
cd /etc/apache2/sites-available
sudo nano myapp.conf
<VirtualHost *:80>
ServerName myapp.com
ServerAlias www.myapp.com
DocumentRoot "/var/www/html/myapp/public"
<Directory "/var/www/html/myapp/public/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
exit nano
sudo a2ensite myapp.conf
sudo service apache2 restart
First go to your virtual host file which can be found on
/etc/apache2/sites-available/default.conf
I am not very sure about the path because I have been using nginx for so long. Anyways locate the conf file and change your root directory to something like
/var/www/html/public
rest all looks fine to me if that doesn't work chmod 777 app/storage

Categories