# FreeBSD 11+Nginx+MySQL+PHP+NextCloud=502 Bad Gateway



## Gdan (Jan 12, 2017)

Hello folks,

Here is the output of my `uname -a` command:


```
FreeBSD biffbsd 11.0-RELEASE-p2 FreeBSD 11.0-RELEASE-p2 #0: Mon Oct 24 06:55:27 UTC 2016     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
```

I followed the directions below to an absolute tee:

https://project.altservice.com/issues/847

In fact I went over and re-did them step by step 3 times just to see if I missed something.  Now I -know- that they are for FreeBSD 10, but looking at them, they should also in theory work with 11.  Or am I mistaken about that?

Here are the versions of my installed packages/ports:

Nginx 1.10.2_3,2 (installed w/ pkg)
mysql56-server-5.6.34 (installed via ports)
mysql56-client-5.6.34 (installed via ports)
PHP 5.6.29 (pkg)
Nextcloud 11.0.0 (pkg)

At first hand glance, should have installed different versions?  Is there anything wrong with the directions I followed above, or should I have done it differently with FreeBSD 11?  I am new to nginx/web and net services but not to Unix.

Thank in advance,
Gdan


----------



## drhowarddrfine (Jan 12, 2017)

I run everything you have there except nextcloud so they work on FreeBSD 11. `502 Bad Gateway` is a message from nginx so something is getting through. Are you sure mysql is running? `ps aux|grep mysql`


----------



## Gdan (Jan 12, 2017)

```
mysql      1636   0.0  0.0  13148   2712  -  Is   02:13     0:00.01 /bin/sh /us
mysql      1828   0.0  5.5 733676 458956  -  I    02:13     0:00.34 /usr/local/
root       1964   0.0  0.0  14884   2504  0  S+   02:33     0:00.00 grep mysql
```

Its running.

I tried using a different example for the my.cnf file.  That produces a blank page.  Now I can access robots.txt and whatever else I put in the /usr/local/www/nextcloud directory.   Something is definitely wrong with MySQL/PHP/maybe even the Nextcloud installation.  I tried deinstalling the pkg and reinstalling from ports.  Still nothing doing.


----------



## SirDice (Jan 12, 2017)

Check the various log files for nginx and mysql. You can find clues why it's not working there. Reinstalling things usually doesn't help if it's a configuration issue.


----------



## abishai (Jan 12, 2017)

Usually blank page is fatal error during script execution with display errors disabled. Try to add _display_errors = 1_ for php


----------



## Gdan (Jan 13, 2017)

Heres what I have ascertained so far:

#1 everything is running.


```
biff@biffbsd:~ % ps aux | egrep 'php-fpm'
root        972   0.0  0.2 207072  14120  -  Ss   19:10    0:00.01 php-fpm: mas
nextcloud   973   0.0  0.2 207064  14136  -  I    19:10    0:00.00 php-fpm: poo
nextcloud   974   0.0  0.2 207064  14136  -  I    19:10    0:00.00 php-fpm: poo
www         975   0.0  0.2 207064  14136  -  I    19:10    0:00.00 php-fpm: poo
www         976   0.0  0.2 207064  14136  -  I    19:10    0:00.00 php-fpm: poo
biff@biffbsd:~ % ps aux | egrep 'nginx'
root        987   0.0  0.1  25412   6512  -  Is   19:10    0:00.00 nginx: maste
www         988   0.0  0.1  25412   6980  -  I    19:10    0:00.00 nginx: worke
biff       1451   0.0  0.0  14884   2476  0  S+   19:16    0:00.00 egrep nginx
biff@biffbsd:~ % ps aux | egrep 'mysql'
mysql       995   0.0  0.0  13148   2512  -  Is   19:10    0:00.01 /bin/sh /usr
mysql      1197   0.0  5.5 733676 458824  -  I    19:10    0:00.26 /usr/local/l
biff       1453   0.0  0.0  14884   2476  0  S+   19:16    0:00.00 egrep mysql
```

#2
Here is what my log files are spitting out:


```
root@biffbsd:/home/biff # less /var/log/php-fpm.log
[12-Jan-2017 19:10:34] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

[12-Jan-2017 19:10:34] NOTICE: fpm is running, pid 972
[12-Jan-2017 19:10:34] NOTICE: ready to handle connections
```


```
root@biffbsd:/home/biff # less /var/log/nginx-error.log
2017/01/12 19:18:26 [error] 988#101290: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'Memcached' not found in /usr/local/www/nextcloud/config/config.php on line 973" while reading response header from upstream, client: 127.0.0.1, server: nextcloud.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/nextcloud.example.com-php-fpm.sock:", host: "localhost"
```


```
root@biffbsd:/var/log/nginx # less access.log
127.0.0.1 - - [12/Jan/2017:19:18:26 -0500] "GET / HTTP/1.1" 500 5 "-" "Mozilla/5.0 (X11; FreeBSD amd64; rv:49.0) Gecko/20100101 Firefox/49.0 SeaMonkey/2.46 Lightning/5.1"
```


```
root@biffbsd:/var/log/nginx # less error.log
(File Empty.)
```


```
root@biffbsd:/var/db/mysql # less biffbsd.err
2017-01-12 19:10:36 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Pl
ease use --explicit_defaults_for_timestamp server option (see documentation for more de
tails).
2017-01-12 19:10:36 0 [Note] --secure-file-priv is set to NULL. Operations related to i
mporting and exporting data are disabled
2017-01-12 19:10:36 0 [Note] /usr/local/libexec/mysqld (mysqld 5.6.34) starting as proc
ess 1197 ...
2017-01-12 19:10:36 1197 [Warning] One can only use the --user switch if running as roo
t

2017-01-12 19:10:36 1197 [Note] Plugin 'FEDERATED' is disabled.
2017-01-12 19:10:36 1197 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-01-12 19:10:36 1197 [Note] InnoDB: The InnoDB memory heap is disabled
2017-01-12 19:10:36 1197 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-01-12 19:10:36 1197 [Note] InnoDB: Memory barrier is not used
2017-01-12 19:10:36 1197 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-01-12 19:10:36 1197 [Note] InnoDB: Using CPU crc32 instructions
2017-01-12 19:10:36 1197 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-01-12 19:10:36 1197 [Note] InnoDB: Completed initialization of buffer pool
2017-01-12 19:10:36 1197 [Note] InnoDB: Highest supported file format is Barracuda.
2017-01-12 19:10:36 1197 [Note] InnoDB: 128 rollback segment(s) are active.
2017-01-12 19:10:36 1197 [Note] InnoDB: Waiting for purge to start
2017-01-12 19:10:36 1197 [Note] InnoDB: 5.6.34 started; log sequence number 1626143
2017-01-12 19:10:37 1197 [Note] Event Scheduler: Loaded 0 events
2017-01-12 19:10:37 1197 [Note] /usr/local/libexec/mysqld: ready for connections.
Version: '5.6.34'  socket: '/tmp/mysql.sock'  port: 0  Source distribution
```


----------



## Gdan (Jan 13, 2017)

Looks like this is the main problem.  I could be wrong though.


```
root@biffbsd:/home/biff # less /var/log/nginx-error.log

2017/01/12 19:52:37 [error] 996#101321: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'Memcached' not found in /usr/local/www/nextcloud/config/config.php on line 973" while reading response header from upstream, client: 127.0.0.1, server: nextcloud.example.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/nextcloud.example.com-php-fpm.sock:", host: "localhost"
```

So I did `pkg info memcached` - it wasn't installed.

I did `pkg install memcached` - restarted everything, still nothing doing.

Looks like a bug in the prepackaged /usr/local/www/nextcloud/config/config.php file. I wonder if I can change anything in here so that it will start working?  And again, forgive me for being new to hosting webservices...  I am a Linux convert and I use FreeBSD as a personal desktop -only-

It's a huge file, so I will only post the relevant parts:


```
902 /**
    903  * Allow the configuration of system wide trusted certificates
    904  */
    905 'enable_certificate_management' => false,
    906
    907 /**
    908  * Memory caching backend configuration
    909  *
    910  * Available cache backends:
    911  *
    912  * * ``\OC\Memcache\APC``        Alternative PHP Cache backend
    913  * * ``\OC\Memcache\APCu``       APC user backend
    914  * * ``\OC\Memcache\ArrayCache`` In-memory array-based backend (not recommended)
    915  * * ``\OC\Memcache\Memcached``  Memcached backend

    916  * * ``\OC\Memcache\Redis``      Redis backend
    917  * * ``\OC\Memcache\XCache``     XCache backend
    918  *
    919  * Advice on choosing between the various backends:
    920  *
    921  * * APCu should be easiest to install. Almost all distributions have packages.
    922  *   Use this for single user environment for all caches.
    923  * * Use Redis or Memcached for distributed environments.
    924  *   For the local cache (you can configure two) take APCu.
    925  */
    926
    927 /**
    928  * Memory caching backend for locally stored data
    929  *
    930  * * Used for host-specific data, e.g. file paths
    931  */
    932 'memcache.local' => '\OC\Memcache\APCu',
    933
    934 /**
    935  * Memory caching backend for distributed data
    936  *
    937  * * Used for installation-specific data, e.g. database caching
    938  * * If unset, defaults to the value of memcache.local
    939  */
    940 'memcache.distributed' => '\OC\Memcache\Memcached',
    941
    942 /**
    943  * Connection details for redis to use for memory caching.
    944  *
    945  * For enhanced security it is recommended to configure Redis
    946  * to require a password. See http://redis.io/topics/security
    947  * for more information.
    948  */
    949 'redis' => array(
    950         'host' => 'localhost', // can also be a unix domain socket: '/tmp/redis.sock'
951         'port' => 6379,
    952         'timeout' => 0.0,
    953         'password' => '', // Optional, if not defined no password will be used.
    954         'dbindex' => 0, // Optional, if undefined SELECT will not run and will use Redis Server's     954 default DB Index.
    955 ),
    956
    957 /**
    958  * Server details for one or more memcached servers to use for memory caching.
    959  */
    960 'memcached_servers' => array(
    961         // hostname, port and optional weight. Also see:
    962         // http://www.php.net/manual/en/memcached.addservers.php
    963         // http://www.php.net/manual/en/memcached.addserver.php
    964         array('localhost', 11211),
    965         //array('other.host.local', 11211),
    966 ),
    967
    968 /**
    969  * Connection options for memcached, see http://apprize.info/php/scaling/15.html
    970  */
    971 'memcached_options' => array(
    972         // Set timeouts to 50ms
    973         \Memcached::OPT_CONNECT_TIMEOUT => 50,
    974         \Memcached::OPT_RETRY_TIMEOUT =>   50,
    975         \Memcached::OPT_SEND_TIMEOUT =>    50,
    976         \Memcached::OPT_RECV_TIMEOUT =>    50,
    977         \Memcached::OPT_POLL_TIMEOUT =>    50,
    978
    979         // Enable compression
    980         \Memcached::OPT_COMPRESSION =>          true,
    981
    982         // Turn on consistent hashing
    983         \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
    984
    985         // Enable Binary Protocol
    986         \Memcached::OPT_BINARY_PROTOCOL =>      true,
    987
    988         // Binary serializer vill be enabled if the igbinary PECL module is available
    989         //\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_IGBINARY,
    990 ),
    991
    992
    993 /**
    994  * Location of the cache folder, defaults to ``data/$user/cache`` where
    995  * ``$user`` is the current user. When specified, the format will change to
    996  * ``$cache_path/$user`` where ``$cache_path`` is the configured cache directory
    997  * and ``$user`` is the user.
    998  */
    999 'cache_path' => '',
   1000
```


----------



## Gdan (Jan 13, 2017)

A small update here.  I have the memcached daemon running and listening to the correct port (it looks like).


```
biff@biffbsd:~ % service memcached status
memcached is running as pid 1207.
```


```
biff@biffbsd:~ % netstat -an
tcp4       0      0 *.11211                *.*                    LISTEN
tcp6       0      0 *.11211                *.*                    LISTEN
```

However, running

`/usr/local/bin/php -i | grep -i ‘memcache’`

Only returns this:

```
PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
```

php56-extensions is installed via ports with sockets enabled in the installation dialogue.

pecl-memcache is also installed via pkg.

pecl-APCu is also isntalled via pkg.

In the /usr/local/www/nextcloud/config/config.php config file that nginx complains about
( /var/log/nginx-error.log ), I already have the below line enabled in line 932:


```
biff@biffbsd:/usr/ports/lang % less /usr/local/www/nextcloud/config/config.php | egrep -n 'APCu'
913: * * ``\OC\Memcache\APCu``       APC user backend
921: * * APCu should be easiest to install. Almost all distributions have packages.
924: *   For the local cache (you can configure two) take APCu.
932:'memcache.local' => '\OC\Memcache\APCu',
```

So after all that, I am really stumped.  Why is it complaining about line 973 of config.php????


```
973         \Memcached::OPT_CONNECT_TIMEOUT => 50,
```


----------



## Gdan (Jan 14, 2017)

I got fed up with nginx and went back to FreeBSD's old trusted standby:  APACHE.

And.. she works.  Beautifully.  The end.  I recommend APACHE to anyone running Nextcloud.


----------



## puretone (Jan 25, 2017)

Gdan said:


> I got fed up with nginx and went back to FreeBSD's old trusted standby:  APACHE.
> 
> And.. she works.  Beautifully.  The end.  I recommend APACHE to anyone running Nextcloud.



It is sort of hindsight here, but good to hear you got it working albeit with Apache; nginx when running properly is a great piece of kit for the smaller servers.
You did notice the bit in the logs stating:
"[error] 988#101290: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'Memcached' not found in /usr/local/www/nextcloud/config/config.php on line 973" while reading response header from upstream, client: 127.0.0.1, server: nextcloud.example.com"

I'm pretty sure that is being interpreted as an actual domain, rather than a default/sample name of a config; unless of course your doing something novel and exciting with your /etc/hosts file, which I doubt. I can tell you from a momentary hiccup at 4:13AM once on a very boring Thursday morning, I had been struck by a large spotted trout and sent into a "duuuuuhhhh" tailspin, which lead to me staring at a very obvious config mistake that "I wasn't seeing" at the time.

I ran a physical test box a while back to see if FreeBSD 11 & nextcloud (9 or 10 or whatever, when the ownCloud/nextcloud fork just happened) would be friendly to one another. To my surprise I ran into 0 issues on a relatively vanilla nginx & MariaDB stack. Ran pretty much flawless for the 3 weeks in testing, as a home media server / cloud.

I'll go out on a limb and say that in all likelihood you probably overlooked a tiny mistake. Happens to all, even the most experienced. That <n>.example.com is probably it. Apache, like nginx, is not very lenient when it comes to declaring domain/server names. I'd suggest you give it another go, if you've the time to spare.


----------



## rigoletto@ (Jan 25, 2017)

This guide is Linux minded but can be easily be adapted to FreeBSD.


----------



## OlivierW (Jan 25, 2017)

Hello,



Gdan said:


> ```
> root@biffbsd:/home/biff # less /var/log/nginx-error.log
> 2017/01/12 19:18:26 [error] 988#101290: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'Memcached' not found in /usr/local/www/nextcloud/config/config.php on line 973" while reading response header from upstream, client: 127.0.0.1, server: nextcloud.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/nextcloud.example.com-php-fpm.sock:", host: "localhost"
> ```



Did you install databases/pecl-memcached (with a "d" at the end)? Seems you installed databases/pecl-memcache instead.
Yes, there are two differents PHP extensions to connect to a Memcache server, and it's confusing.

Best Regards.


----------



## metsuke (Jan 26, 2017)

Gdan, sorry I didn't see this thread earlier lest I could have provided some assistance!  I run FreeBSD 11 with nginx, mariadb, and nextcloud in a jail and it is working perfectly, though it took a little more time to get set up.  If you ever decide to try nginx again with NextCloud, send me a message!  When I get more time, maybe I'll create a HowTo.


----------

