I try to setup Multiple Domains in nginx under ubuntu 18
I created new directory with index file:
mkdir /var/www/test.com
cd /var/www/test.com
sudo nano /var/www/test.com.index.html
with some text text
/var/www/test.com.index.html page sample
I create config file:
sudo nano /etc/nginx/modules-available/test.com
with text:
server {
listen 80;
root /var/www/test.com;
index index.html index.htm index.nginx-debian.html;
server_name test.com;
location / {
try_files $uri $uri/ = 404;
}
}
and in hosts:
sudo nano /etc/hosts
add 1 line :
127.0.0.1 localhost test.com
I create symbol link :
sudo ln -s /etc/nginx/modules-available/test.com /etc/nginx/sites-enabled/
and Check it I see test.com:
cd /etc/nginx/sites-enabled/
ls
sudo service nginx restart
check :
curl test.com
I got :
$ curl test.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
but not sample text I enreed above.
File /etc/nginx/nginx.conf is default and has lines :
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
1) Why error and how to fix it ?
2) I suppose I need to disable /etc/nginx/sites-available/default file. How can I do it ?
Modified :
I run
cd /var/www/ # (it root of all aps)
sudo chown -R www-data: .
and restarting with
sudo systemctl restart php7.2-fpm
sudo service nginx restart
It did not help , I have the same error ...
Can it be some option or misspelling in modules-available/test.com ?
Related
I'm using 'Oracle Cloud'.
I created a VM(Computer instance) on Oracle Cloud with CentOS 8. And I installed NginX, and it works well when I test it with 'http://mydeal.servername.com'.
To make NginX service with HTTPS, I also installed certbot(Let's Encrypt) and created certificate, using the following command.
sudo certbot --standalone -d mydeal.servername.com certonly
Result files were like below.
Cert : /etc/letsencrypt/live/mydeal.servername.com/fullchain.pem;
Key : /etc/letsencrypt/live/mydeal.servername.com/privkey.pem;
I added http and https to firewall service list like below.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
And I created test index.html like below.
sudo -i
mkdir /var/www
mkdir /var/www/mydeal
echo "MyDeal at Oracle Cloud" > /var/www/mydeal/index.html
And I created https settings, including http redirection, in /etc/nginx/conf.d/my.conf file.
server {
listen 80;
server_name my.servername.com;
location / {
root /var/www/mydeal;
index index.html;
try_files $uri /index.html;
}
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mydeal.servername.com;
ssl_certificate /etc/letsencrypt/live/mydeal.servername.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydeal.servername.com/privkey.pem;
location / {
root /var/www/mydeal;
index index.html;
try_files $uri /index.html;
}
}
Finally, when I start nginx server with the following command, it works well.
sudo -i
sudo nginx
But, when I start nginx server with the following command, it gives error "500 Internal Server Error" on the browser screen.
sudo systemctl enable nginx
sudo systemctl start nginx
I can not find any differences b/w 2 start procedures.
How I can debug this problem?
EDIT:
Ok, very strange still. It seems that it does not work on my main browser. In incognito browsers or just a completely new chrome window the sites now do work. I guess it has something to do with browser caching?
So I am hosting my website on Digital Ocean and I want to host multiple 'websites' on 1 droplet/server. By multiple websites, I mean different subdomains of my main domain. So I want to host admin.domain.com, dev.domain.com and test.domain.com on the same server. I followed this tutorial, but it is not working like expected. Currently, only 1 subdomain of the 3 is working...
What have I tried so far?
First of, I created 3 A records in my DNS all pointing to the same server_ip droplet on Digital Ocean.
I've created a different a different folder for each subdomain in the /var/www folder, each containing a html folder with a simple index.html file and some html:
The image above shows my /var/www folder.
I then used the following command sudo chmod -R 755 /var/www.
Next, I copied the default server block file and used this as the default for a new server block with the following command:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/admin.domain.nl
I changed the contents of the file in all 3 config files for the 3 subdomains to the following (obviously changing the root to the specific subdomain aswell as the server_name):
server {
listen 80;
listen [::]:80;
root /var/www/admin.domain.nl/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name admin.domain.nl;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
I then used the following command: sudo ln -s /etc/nginx/sites-available/dev.domain.nl /etc/nginx/sites-enabled/ 3 times for the 3 different subdomains to 'enable' the server blocks.
This is my sites-enabled folder:
I've had no syntax errors and thus restarted nginx with: sudo systemctl restart nginx.
The problem
Now, for some very odd reason I do not understand, only the admin.domain.nl site is working. The other 2 subdomains simply display: This site can’t be reached.
What am I missing here?
IN /etc/nginx/nginx.conf
http {
# ...........................others contents.....................................
# in bottom
server {
listen 80;
root /var/www/html/cmp/api;
server_name "cmpapi.localhost";
index index.html index.php;
location / {
try_files $uri $uri/ $uri.html $uri.php =404;
}
}
server {
listen 80;
root /var/www/html/cmp/frontend;
server_name "cmp.localhost";
index index.html index.php;
location / {
try_files $uri $uri/ $uri.html $uri.php =404;
}
}
}
Here my project name is cmp. here 2 project
one is react frontend
another is laravel api project
Here 2 folder created in
/var/www/html/cmp/api
This assigned to http://cmpapi.localhost (server_name)
/var/www/html/cmp/frontend
This assigned to http://cmp.localhost (server_name)
cmd
sudo find /var/www/html/cmp/frontend/ -type f -exec chmod 644 {} \;
sudo find /var/www/html/cmp/frontend/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/cmp/api/ -type f -exec chmod 644 {} \;
sudo find /var/www/html/cmp/api/ -type d -exec chmod 755 {} \;
systemctl reload nginx
Browser
http://cmpapi.localhost
http://cmp.localhost
i try to setup an nginx server with reverse proxy via a bash script. But somehow i end up in an endless redirect loop but i dont know why. Does someone has an idea?
sudo apt install nginx -y
sudo apt-get remove apache2* -y
sudo apt install ufw -y
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
sudo systemctl start nginx
sudo systemctl restart nginx
#sudo systemctl status nginx
sudo mkdir -p /var/www/servername.com/html
sudo chmod -R 755 /var/www/servername.com
sudo chown -R $USER:$USER /var/www/servername.com/html
sudo bash -c 'echo "<html> <head> <title>Welcome to servername.com!</title> </head> <body> <h1>Success! The servername.com server block is working!</h1> <b>Meow Meow!</b> </body> </html>" > /var/www/servername.com/html/index.html'
sudo bash -c 'echo "server {
listen 80;
listen [::]:80;
root /var/www/servername.com/html;
index index.html index.htm index.nginx-debian.html;
#You can put your domain name here for ex: www.servername.com
server_name MYIP;
location / {
try_files $uri $uri/ =404;
}
}" > /etc/nginx/sites-available/servername.com'
sudo ln -s /etc/nginx/sites-available/servername.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Sorry for noob question, I suck at Ubuntu.
I have just installed nginx in a Ubuntu server with:
sudo apt-get update
sudo apt-get -y install nginx
It successfully built. I'm trying to change the index page, so I have modified my /usr/share/nginx/html/index.html, and then tried all of these:
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
But when I refresh the root page on my browser it still shows the old page.
This is what the index.html looks like:
I have checked my /etc/nginx/nginx.conf, but don't find anything particular there.
What could I be missing?
If you had checked vhost, you knowned, root directory is /var/www/html...
vhost is in /etc/nginx/sites-available and /etc/nginx/sites-enabled (sites-enabled is symlink).
The correct configuration file for NGINX on Debian is :
/var/www/html/index.nginx-debian.html
If you update this file, the changes will be reflected immediately, without a start/stop or restart.
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
I have same problem before, then after updating nginx conf by moving 'root' from 'server/location' to 'server', it works well. Nginx config file :
server {
listen 443 ssl;
server_name localhost;
root /usr/share/nginx/html/rdist;
location /user/ {
proxy_pass http://localhost:9191;
}
location /api/ {
proxy_pass http://localhost:9191;
}
location /auth/ {
proxy_pass http://localhost:9191;
}
location / {
index index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
I have taken over the development of a few websites and am currently trying to get one hosted within a Vagrant box. I am very familiar with Vagrant but am having a strange issue that I have been unable to fix since last Friday.
I have created the Vagrant file and MYSQL database for the Wordpress installation has been moved to my local (host) machine and I point to this from the Wordpress installation on the guest machine. All the Wordpress files exist and the folder is being shared with the guest machine.
My Vagrant file looks as follows:
Vagrant.configure("2") do |config|
# Set Vagrant box to use
config.vm.box = "ubuntu/trusty64"
# Configure port forwarding
config.vm.network :forwarded_port, guest: 80, host: 8930, auto_correct: true
# Set synched folder
config.vm.synced_folder "./", "/var/www", create: true, group: "www-data", owner: "www-data"
# Configure the VM
config.vm.provider "virtualbox" do |v|
v.name = "St. David's Lab"
v.customize ["modifyvm", :id, "--memory", "1024"]
end
# Set up shell provisioning
config.vm.provision :shell, :path => "bootstrap.sh"
end
The boostrap.sh file is used to setup the required software and similar on the guest machine and looks as follows:
#!/bin/bash
echo "Provisioning virtual machine..."
apt-get update
echo "Installing Git"
apt-get install git -y > /dev/null
echo "Installing Nginx"
apt-get install nginx -y >/dev/null
echo "Configuring Nginx"
cp /var/www/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null
ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/nginx_vhost
rm -rf /etc/nginx/sites-available/default
service nginx restart > /dev/null
echo "Updating PHP repository"
apt-get install python-software-properties build-essential -y > /dev/null
add-apt-repository ppa:ondrej/php5 -y > /dev/null
apt-get update > /dev/null
echo "Installing PHP"
apt-get install php5-common php5-dev php5-cli php5-fpm -y > /dev/null
echo "Installing PHP extensions"
apt-get install curl php5-curl php5-gd php5-mcrypt php5-mysql -y > /dev/null
apt-get install libapache2-mod-php5
And here is the server config that gets created on the guest machine:
server {
listen 80;
server_name localhost;
root /var/www/;
index index.php index.html;
# Important for VirtualBox
sendfile off;
location / {
try_files $uri $uri/ =404;
}
location ~* \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache off;
fastcgi_index index.php;
}
}
I have changed the siteurl in the Wordpress database to localhost:8930 as well.
The issue I am having is that when I try and access the address localhost:8930 (as defined in the port forwarding in my Vagrant file) I get redirected back to localhost default index page (http://localhost). It is not a cache issue as I have cleared this, used an incognito window and replaced the index file with a simple "hello world" and it shows.
Can anyone see why this may be happening?
Thanks
The $uri/ clause of the try_files directive is causing an external redirect to the default port. You probably want to avoid external redirects from nginx itself, because it over complicates your 8930 port mapping rule.
One solution is to remove the index directive and the $uri/ clause.
You really need to add a default controller for WordPress anyway, something like:
location / {
try_files $uri /index.php?$args;
}
EDIT:
Detailed analysis of the problem:
You present the URL http://localhost:8930 which is presented to nginx as http://localhost:80. The location / processes the request. The try_files directive tests for the existence of a file and a directory. The presence of a directory causes an external redirect to http://localhost:80/. The external redirect is an undocumented side-effect of the $uri/ clause.
The try_files directive is documented here.