1

In my Ubuntu 16.04 Nginx environment I tried to create an online test version of a local WordPress site. The original site works fine and is accessed in HTTPS (the lock favicon is green in all pages).

My current state and problem

When I browse into my_ip_addrres/test I land on a 404 page in the original site (the local site I desire to duplicate), even though the URL remains my_ip_addrres/test. Moreover, all main menu links in that 404 page with my_ip_addrres/test URL point to the original site.

My desired end state

When I'll navigate to my_ip_addrres/test in whatever browser, I'll land on the duplicated version of the original site, and will operate it just as I would operate the original site, so I could go to my_ip_addrres/test/wp-admin, log in, and change that test site.

What I did so far to duplicate the original site

I basically did everything with the following bash commands. Please use these if you wish to reproduce in your end:

cd /var/www/html/
read domain # Domain of the site for duplication.
read -s rps # Password for Mysql root user.
read -s sps # Password for Mysql DB user.

cp -r ./${domain} ./test/
sed -i "s/${domain}/test"/g ./test/wp-config.php
cp -r /etc/nginx/sites-available/${domain}.conf /etc/nginx/sites-available/test.conf
sed -i "s/${domain}/test"/g /etc/nginx/sites-available/test.conf
ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf

echo "CREATE USER 'test'@'localhost' IDENTIFIED BY \"${sps}\";" | mysql -u root -p"${rps}"
echo "CREATE database test;" | mysql -u root -p"${rps}"
echo "GRANT ALL PRIVILEGES ON test.* TO test@localhost;" | mysql -u root -p"${rps}"

mysqldump -u root -p"${rps}" "${domain}" > test.sql
mysql -u test -p"${sps}" test < ./test.sql

cd test 
wp search-replace "${domain}" "MY_IP_ADDRESS/test" --allow-root

Further details

1) I had about 1300 replacements in the database after running the above WP-CLI search and replace command.

2) /var/www/html/test/wp-config.php:

define('DB_NAME', 'test');
define('DB_USER', 'test');

Nginx confs

Logs:

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

tail /var/log/nginx/error.log
tail /var/log/nginx/access.log
  • Both tails show no errors regarding /var/www/html/test/.

My question

What remains a possible cause for the test site not to go up from my_ip_addrres/test? As an Nginx newbie, I really feel I tried everything in the arsenal...

Arcticooling
  • 1
  • 3
  • 7
  • 22
  • Patience. You only asked your question 90 minutes ago, and this is a quieter time for SF. Please edit your question to include the Nginx site configs, current and test. I personally use subdomains for testing, not folders. – Tim Dec 18 '17 at 18:17
  • Key problem seems to be that you have two configuration files for the same domain. If you want a second WP instance on the same domain in a folder you need to account for it in your main site config file, rather than a new config file, as I don't think two configs can listen on the same domain. I suggest that you put your test site onto a subdomain, that should work using the method you outlined. – Tim Dec 18 '17 at 19:11
  • @Tim I included them in the question already under "further details". I don't want to run my test site under a sub-directory (with or without a subdomain), rather, in a unique separate directory, but just without a domain. – Arcticooling Dec 19 '17 at 04:44
  • Log into the admin area, go to settings -> permalinks and click on "save changes". No need to change anything there. This should update all existing URLs. – Gerald Schneider Dec 19 '17 at 13:59
  • Please edit your question to clearly state your desired end state and current problem. If you want to address the test site by IP only why have you defined a server_name? I think this configuration will be fiddly to get going, suggest you try for something more standard. – Tim Dec 19 '17 at 18:13
  • I edited the question to include both of this as clearly as I could, with heading. I didn't notice that server_name or at least didn't think it's problematic, my bad but even when I totally removed that line, saved and restarted the server, I still encounter the exact same problem. – Arcticooling Dec 19 '17 at 18:36
  • Like I said, I think this is the wrong approach. Think about your requirements carefully and edit your question to say what they are. Do you really need to browse by IP? Why can't it be in a subdirectory or subdomain or on another domain? Someone may be able to answer your question, but despite working with Nginx and Wordpress for some time I can't easily. Maybe if I spent a couple of hours playing with it I could. – Tim Dec 19 '17 at 18:50
  • I don't have another free domain and doesn't want to buy one just for this purpose (unless I wouldn't have another choice). I don't mind putting the site in a subdirectory (without a subdomain, as I prefer not to work with by Principe because it seems to me non minimalistic). The most minimal approach, as I see it, is to access the test site directly from the IP, I really don't need anything beyond accessing it directly from the IP. – Arcticooling Dec 19 '17 at 19:10
  • I've spent a little trying trying to have Nginx listen on an IP when there are Nginx servers defined by name. I can't get it to work easily. Using server_name _ and default_server didn't work. I suggest you need to use a subdirectory (not too difficult) or a subdomain (easy) to do this. I don't mind helping with either of those, but I can't help further with binding to an IP only. – Tim Dec 20 '17 at 19:20
  • @Tim did you do that with a webserver with 1 site or 2 or more sites? Either, way, a didactic answer can be good and will surly help Nginx newcomers that tried the same action. – Arcticooling Dec 21 '17 at 11:00
  • I think the advice not to do it is pretty good, given I've given a suggestion which is well documented in many places, including my own [tutorial](https://www.photographerstechsupport.com/tutorials/hosting-wordpress-on-aws-tutorial-pt1-introduction-configuration-downloads/). I tried it on my AWS Linux production web server, which runs 6 websites. – Tim Dec 21 '17 at 18:21
  • @Tim, Daniel's answer mentions `/etc/hosts` for creating a subdomain, as well as editing the duplicated site's site conf. Please add a short answer describing how to create a subdomain for the duplicated site if you wish. It would be a great contribution. – Arcticooling Dec 25 '17 at 09:35
  • I don't see how a hosts file is relevant for serving public traffic, that would only be useful for local access say for a development environment. hosts file replaces DNS, and is trivial to do. Having Nginx serve a subdomain is trivially easy - Google found [this example](https://gist.github.com/ianjuma/9009490) in 5 seconds. – Tim Dec 25 '17 at 22:26
  • Sadly, this seems vastly different than my original example. I don't know to recognize what I should utilize from it in my case. – Arcticooling Dec 25 '17 at 23:26

1 Answers1

1

I really don't need anything beyond accessing it directly from the IP

Options:

  1. Just save yourself a headache and a half and use a test.example.com domain name. You don't even have to publish valid DNS records just edit your local HOSTS file. And add a server name directive to the test.conf file.
  2. You need to ensure that the test config is the FIRST config that is loaded. Change the file name in sites-enabled to 000-test.confThis means anyone doing anything silly will see your test site.
  3. Add a server name directive to the test.conf with the server IP
  4. Manually include the individual config files in order rather than the entire sites-enabled

Explanation

Place a HelloWorld.html file in /var/www/html/example.com/test/. Browse to IP_ADDRESS/test/HelloWorld.html and you will see the contents of your file. Since your configs have no server { ... } with a servername that corresponds to http://YOUR_IP/ it uses the first server { ... } block as default. Statistically speaking example.com.conf is loaded before test.conf

Nginx Is looking int he wrong root for your file because the main website is the default.

Recommendation

Use option 1 or 3. Your live site should probably be the default website visitors see. I would opt for option 1 because it lets your further "hide" your test site. Option 3 will make "http://ip/test" but for reasons above I still prefer option 1.

Daniel Widrick
  • 3,488
  • 2
  • 13
  • 27
  • In the end I created a subdomain [this way](https://unix.stackexchange.com/questions/413148/nginx-creating-a-subdomain-linking-to-another-local-website). – Arcticooling Jan 01 '18 at 04:03
  • Update: `server_name 192.0.2.1 phpmyadmin;`, for example, worked. Shouldn't be different with a WordPress webape --- the principal is that the domain+tld should come right after the IP address. Might be worth to update to include such code example. https://serverfault.com/questions/890236/accessing-phpmyadmin-in-nginx-directly-from-ip-address-without-a-subdomain – Arcticooling Jan 02 '18 at 03:50