Nginx virtual host not working - php

I'm hosting a PHP system on my localhost, I created index.php in the /var/www/html like this:
// /var/www/html/index.php
<?php
phpinfo();
And when I access localhost:80 it's showing up my PHP configurations info.
But when I cloned /etc/nginx/sites-available/default to /etc/nginx/sites-available/superstore and changed server_name it's not working after sudo service nginx restart, even sudo service nginx status is returning success.
My nginx config (/etc/nginx/sites-available/superstore) is like this:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name superstore.dev;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
My PHP version:
Nginx status after restart:
I added this virtual host to my /etc/hosts like this:
127.0.0.1 superstore.dev
But when I try to access superstore.dev it's returning this error to me:
I installed PHP and Nginx by following command on Ubuntu 16:
sudo apt install nginx php7.0-fpm -y
And I'm sure that I already symbolic linked superstore config file to sites-enabled folder.
Why my virtual host is not working?

Similar to Apache config scheme, folder sites-available is supposed to contain all your config files, even the ones you don't want to run.
There is a second folder, sites-enabled, that contains symlinks to the config files you want to run.
To create the symlink run ln -s /etc/nginx/sites-available/superstore /etc/nginx/sites-enabled/ and then service nginx reload
For more info see answer https://serverfault.com/a/424456/240702

Related

How to setup up Laravel with Nginx on AWS linux AMI

I am trying to user laravel with nginx on AWS linux ami, however when I try to access my instance public ip i get file not found. Here is what i do
sudo amazon-linux-extras install nginx1
sudo service nginx start
After this step I am sure nginx is working because I can see nginx webpage. Now I am trying to install laravel
sudo amazon-linux-extras install php7.3
sudo yum install php-xml php-mbstring
sudo service php-fpm start
sudo service nginx restart
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer global require laravel/installer
export PATH=$PATH:/home/ec2-user/.config/composer/vendor/bin/
laravel new test
sudo vi /etc/nginx/nginx.conf
server {
listen 80;
server_name _;
root /home/ec2-user/test/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
sudo chown -R apache:apache storage/
sudo chown -R apache:apache bootstrap/cache
When I try to load my page i recieve file not found.
You can fix this issue in 2 way:
Providing a valid server name/ Domain
Fixing Nginx Default IP address
Provide valid server name
You need to fix the server name inside your Nginx configuration you have putten the server name as _. You need to put there a valid domain name. For example:
server_name example.com;
Then create an A record of the domain pointing to the IP adress. For example:
example.com A 123.123.123.123
Fix Nginx Default configuration
By default, if you install Nginx the server IP takes the files from /var/www/html so you can find this issue in 2 way:
Upload the files in the default path
Change the Nginx default path
Upload the files in the default path:
1st delete the html folder under /var/www/
2nd uload your laravel files inside /var/www/
3rd rename your laravel project public folder to html
Now restart nginx and visit your ip
Change Nginx Default Path
Open the default file
sudo nano /etc/nginx/sites-enabled/default
Find root /var/www/html and replace the /var/www/html with your path
Restart nginx and then visit your IP

Configure multi site on nginx server through IP using Digital Ocean

Problem: Unable to hit two websites handled under one nginx server i.e. <<ip-address>> & <<ip-address>>/web2
Configuration on Digital Ocean:
1 Droplet / Ubuntu 18 / LEMP
I have two test PHP website in the CodeIgniter framework
Folder config for 1st Website: /var/www/html/web1/
Folder config for 2nd Website: /var/www/html/web2/
Nginx Server Block configuration for two sites
web1.com
server {
listen 80;
root /var/www/html/web1;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
web2.com
server {
listen 80;
root /var/www/html/web2;
index index.php index.html index.htm index.nginx-debian.html;
server_name <<ip-address>>/web2;
location /web2/ {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
I am totally new to nginx server, I do according to the documentation provided by a community under a digital ocean.
Please help!
Thanks.
What you're trying to do is not how nginx works out of the box. It could, with a lot of fiddling, end up working that way, but I don't think it's worth the effort.
See, nginx configuration expects server_name to be either a FQDN (fully qualified domain name) or an IP address, but not a full URL with path.
In your case, the request for ip-address/web2 is probably actually matching web1's config (so pointing you to /var/www/html/web1/web2/ which doesn't exist)
Best way to work this out (assuming you want to keep both sites on the same droplet): get a FQDN for each site. It could be a subdomain for a domain you already have (i.e. web1.sharad.com and web2.sharad.com)... Then on each of nginx's config files use the appropriate server name (web1.sharad.com and web2.sharad.com), check for typos and errors with sudo nginx -t and if all is OK restart nginx with sudo systemctl restart nginx

Creating your first laravel project on web server

ive recently just follwed this guide and "installed" laravel on my lamp server: https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04
Now when im finished it says at the bottom of their guide
"You now have Laravel completely installed and ready to go. You can
see the default landing page by visiting your server's domain or IP
address in your web browser:
http://server_domain_or_IP"
When i visit my website there is no new landing page and there is no new files in the var/www/html that would act as a landing page except for the standard apache one.
For me this is fine but i still dont know how to create my first project, i want to do this to check my install is finished and i am ready for bed.
Here is how my www directory looks like at the moment:
And here is my nginx config which i believe plays a part in this whole thing:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name www.mysite.me;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The files are in the /var/www/laravel folder rather than the /var/www/html folder which is visible in the screenshot you provided.
From my own experience when this happens, it is due to having multiple 'default server' attributes in multiple nginx config files for different websites so when looking at the server via the IP it is not selecting your new laravel build.
The server needs to restart for some of the settings to take place.
You can try accessing its console and type
To reboot a server from the command line, run:
sudo shutdown -r now
To restart Apache, run:
sudo service apache2 restart

Vagrant & Nginx - Name Based Virtual Hosts

I believe I have set up an nginx virtual host file correctly, however, I'm not able to navigate to that URL on the host machine. I have a slight feeling that I'm misunderstanding something here.
Here's the situation.
I have a Vagrantfile that does the following:
Installation of Required Packages
Composer Update
Copies an Nginx Virtual Host file over to Nginx
Restarts all relevant services
Heres the vhost file:
server {
listen 80;
server_name vagrant;
root /usr/share/nginx/www;
index index.html index.htm index.php;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
I can verify that when the virtual host file was called simply vagrant and the server name was set to the following server_name _; that a page was rendered when I visited localhost.
As I'm trying to set up Magento, which doesn't work well with localhost domains, I'm trying to set up a name based virtual host for it under the domain dev.magento.co.uk
If I ssh into the vagrant installation, I can verify that the dev.magento.co.uk file is in the /etc/nginx/site-enabled directory and that it's contents were copied over correctly.
What am I missing?

Trouble Installing Nginx and php5-fpm on Debian 7.3

I have a Debian 7.3 installation in a VM that I am practising installing Nginx and php5-fpm on. I got the Nginx working, by assigning it a manual port of :8080 and that points to /var/www/ for data and in that directory is an index.html and info.php file.
The config file for my Nginx is located at /etc/nginx/conf.d/default.conf and looks like this:
server {
listen 8080;
root /var/www;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I have tried changing fastcgi_pass both ways:
fastcgi_pass 127.0.0.1:9000;
and also as:
fastcgi_pass unix:/var/run/php5-fpm.sock;
In my /etc/php5/fpm/pool.d/www.conf file I have the following configuration:
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php5-fpm.sock
Here too, I have uncommented the line to match in the Nginx default.conf file.
In my php.ini file I have edited it so that it shows cgi.fix_pathinfo = 0 as required by most of the guides I have seen.
When I try to load nginx, it runs OK. When I try to run php5-fpm this is what happens:
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/# /etc/init.d/php5-fpm reload
[ ok ] Reloading PHP5 FastCGI Process Manager: php5-fpm.
root#debianx86:/# /etc/init.d/php5-fpm restart
[FAIL] Restarting PHP5 FastCGI Process Manager: php5-fpm failed!
root#debianx86:/# /etc/init.d/php5-fpm start
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/#
I then open up any of the browsers on my VM and point them to either 127.0.0.1:8080 or localhost:8080 and I get the custom index.html loading that I made and it works! So I then try to load theinfo.php file and I get presented with a 404 Not Found - nginx/1.4.4.
I don't understand what I'm doing wrong. Is there something I'm missing from all this?
I installed nginx from sudo apt-get -y install nginx and sudo apt-get -y install php5-fpm too. Any dependencies they required would have been installed along with that.
Is there a script that I can run on a fresh install of Debian 7.3 that someone has got that will install it properly for me, and make all the modifications so that nginx and php5-fpm are up and running? I've looked over many of the websites with the instructions and I seem to be doing pretty much everything they do, except for the default-sites and enabled-sites, as neither of those folders exist for me, and I don't want to run my virtual hosts like that. I will run them with their own servers listed in the default.conf file.
EDIT: I have even tried following this article at DigitalOcean and it still doesn't work.
EDIT #2: I also did chown -R www-data:www-data /var/www to ensure that the user and group match the information in the www.conf file. I also tried by changing it back to the original root:root specs too. Still nothing.
I think maybe port 9000 is already being used, so php5-fpm can't bind with that port and fails to start.
in the fpm pool settings swap the line of port 9000 with the line with the sock file, then try to start php5-fpm like you were doing, if it works then all you need is to update the nginx configuratuin to proxy pass to the sock file instead of the port.

Categories