I am trying to host node app on vps using nginx server. But I am unable to install nginx in ubuntu 18.04 version after apt install nginx shows:
The following packages have unmet dependencies:
nginx : Depends: libc6 (>= 2.28) but 2.27-3ubuntu1.6 is to be installed
Depends: libcrypt1 (>= 1:4.1.0) but it is not installable
E: Unable to correct problems, you have held broken packages.
Anyone face such problem & how do you solve it? I also got problem in nginx.cnf, after nginx -t to check it shows brotil error.
ubuntu nginx installation
node app
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
I want to host node app to serve backend API's. So, I bought VPS from Hostinger website & have installed Ubuntu v18.04 in it. Then I successfully installed nodejs to run nodeapp & pm2 to manage processes. It was successfully running on port 8000. But problem is when I called the API from frontend, shows SSL security error.
So, for that I tried to install nginx but shows error & I guess for SSL nginx server is required. Now I am stuck in nginx installation.
I need some solution from someone who also face same problem.
You should consider upgrading to more recent versions of ubuntu as version 18.04 will reach EOL on April 30, 2023
Otherwise, you may try upgrading packages on your system, as it currently has libc6 version 2.27 while version 2.28 or higher is required.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx
if you continue seeing an issue, you may try
sudo apt-get install --no-install-recommends nginx
Actually, I had installed Ubuntu v18.04 with plex control panel. I guess that is giving me the issue.
Now I installed Ubuntu v18.04 only. It is working fine now. I successfully installed nginx and it is running properly.
I'm building an application which uses Node, redis and mongo. I finished the development, and I want to containerize it with docker.
Here's my Dockerfile:
FROM node:13.8.0-alpine3.11
RUN npm install -g pm2
WORKDIR /user/src/app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
And here my docker-compose.yml
version: '3'
services:
redis-server:
container_name: scrapr-redis
image: 'redis:6.0-rc1-alpine'
ports:
- '6379:6379'
mongo-db:
container_name: scrapr-mongo
image: mongo
ports:
- '27017:27017'
command: --auth
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
- MONGO_INITDB_DATABASE=db
app:
container_name: scrapr-node
restart: always
build: .
ports:
- '3000:3000'
- '3001:3001'
links:
- mongo-db
depends_on:
- redis-server
environment:
- DB_USER=user
- DB_PWD=pass
- DB_NAME=db
- REDIS_HOST=redis-server
command: 'node index.mjs'
I can start the service successfully, but when node starts, it generates the following error:
Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
When I do docker ps -a, I can see that all containers are running:
Why can't it connect with redis? What did I miss?
127.0.0.1 does not look right to me at all. Let me get the quick checks out of the way first, are you sure you are using the REDIS_HOST env variable in the node app correctly? I would add some console logging to your node app to echo out the env variables to check what they are.
Secondly try attach to the running scrapr-node with docker container exec -it scrapr-node sh or /bin/bash if sh does not work.
Then run nslookup scrapr-redis from the shell, this will give you the ip address of the redis container. if ping scraper-redis returns then you know its an issue with your node app not the docker network.
you can also exec into the redis node and run hostname -I which should show the same ip address as you saw from the other container.
This should help you to debug the issue.
EDIT:
Ensure that you are correctly getting the value from your environment into your node app using process.env.REDIS_HOST and then correctly using that value when connecting to redis something like:
const redisClient = redis.createClient({
host: process.env.REDIS_HOST,
port: 6379
});
I would not try and force 127.0.0.1 on the docker network (if that is even possible) it is reserved as the loopback address.
I recently updated my MongoDB server from 3.2 to 3.6 to fix some errors with naming. When I had my Mongo server as 3.2 it did work fine.
I downloaded the MongoDB 3.6 from https://www.mongodb.com/download-center/community and installed it with the package manager (sudo dpkg -i mongodb-org-server_3.6.12_amd64.deb) and I have verified that the server did update with mongod --version.
As far as I know, starting the system works, but when I run mongo, or sudo mongo, it gives me the following output
MongoDB shell version: 3.2.22
connecting to: test
2019-05-14T13:28:09.049-0400 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2019-05-14T13:28:09.049-0400 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
exception: connect failed
I'm not sure how I could fix the problem either, I have tried to restart the service and restarting the server it's running on (Ubuntu 16.04) and neither worked. Nothing I found online could help either so if you know a possible solution, please let me know, thanks!
Before that, try with
sudo service mongod stop
sudo rm /var/lib/mongodb/mongod.lock
mongod –repair
sudo service mongod start
I've installed MongoDB v4.0 for the most amazing feature of it Transaction in Nodejs with mongodb 3.1 as a driver.
When I try to use a transaction session I've faced this error:
MongoError: Transaction numbers are only allowed on a replica set member or mongos.
What's that and how can I get rid of it?
Transactions are undoubtedly the most exciting new feature in MongoDB 4.0. But unfortunately, most tools for installing and running MongoDB start a standalone server as opposed to a replica set. If you try to start a session on a standalone server, you'll get this error.
In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process. The new run-rs npm module makes starting replica sets easy. Running run-rs is all you need to start a replica set, run-rs will even install the correct version of MongoDB for you.
Run-rs has no outside dependencies except Node.js and npm. You do not need to have Docker, homebrew, APT, Python, or even MongoDB installed.
Install run-rs globally with npm's -g flag. You can also list run-rs in your package.json file's devDependencies.
npm install run-rs -g
Next, run run-rs with the --version flag. Run-rs will download MongoDB v4.0.0 for you. Don't worry, it won't overwrite your existing MongoDB install.
run-rs -v 4.0.0 --shell
Then use replicaSet=rs in your connection string.
You find more details about it here.
I got the solution, and it's just three lines configuration inside the MongoDB config file.
After switching from MongoDB atlas and installing MongoDB v 4.4.0 on my CentOS 7 VPS with WHM, I faced that issue also.
the run-rs solution does not work for me, but I managed to solve this issue without any third-party tool, following these steps:
1. turn off mongod.
the most efficient way is by entering the MongoDB shell with the command mongo
checkout the method
db.shutdownServer()
You will be no ability to use the MongoDB server.
For me, the shutdown process took too long, and then I killed the process with the command:
systemctl stop -f mongod
if you killed the mongod process,s probably you will need to run
mongod --dbpath /var/db --repair
The var/db should point to your database directory.
2. setting replicaSet configuration.
for the replicaSet settings step, check out the /etc/mongod.conf file,
look for the replication value line, and you should add the following lines as below:
replication:
oplogSizeMB: <int>
replSetName: <string>
enableMajorityReadConcern: <boolean>
use the replSetName value on the next step.
an example of those settings:
oplogSizeMB: 2000
replSetName: rs0
enableMajorityReadConcern: false
3. add your connection string URL.
add the value of replSetName to your connection URL &replicaSet=--YourReplicationSetName--
if you used the name rs0 from our example, then you should add to your DB connection URL query replicaSet=rs0
4. turn on mongod again
enter the command: systemctl start mongod
5. Access your replicaSet database
enter MongoDB shell with the command mongo, enter the command rs.initiate()
now you should be in your replicaSet database.
Possible solution for local development using docker
Create Dockerfile
FROM mongo:4.4.7
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "rs" ]
Build this Dockerfile
docker build ./ -t mongodb:4.7-replset
Run this created image
docker run --name mongodb-replset -p 27017:27017 -d mongodb:4.7-replset
Connect to database using this URI
mongodb://localhost:27017/myDB
For those who wants to develop against of the dockerized MongoDB instance, here is the single-file docker-compose.yaml solution based on the official MongoDB docker image:
version: '3.9'
services:
mongodb:
image: mongo:5
command: --replSet rs0
ports:
- "28017:27017"
environment:
MONGO_INITDB_DATABASE: attachment-api-local-dev
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/admin --quiet
interval: 2s
timeout: 3s
retries: 5
mongo-init:
image: mongo:5
restart: "no"
depends_on:
mongodb:
condition: service_healthy
command: >
mongo --host mongodb:27017 --eval
'
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "localhost:27017" }
]
})
'
A much easier solution is to just use Bitnami MongoDB image:
services:
mongodb:
image: bitnami/mongodb:5.0
ports:
- "27017:27017"
environment:
MONGODB_REPLICA_SET_MODE: primary
ALLOW_EMPTY_PASSWORD: 'yes'
I faced the same issue recently. In my case it's because I'm connecting to a remote Mongo server with a different version than my local development environment.
To quickly solve the issue, I added the following param to my connection string:
?retryWrites=false
In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process.
You can use the run-rs npm module. Zero-config MongoDB runner. Starts a replica set with no non-Node dependencies, not even MongoDB.
Or you can simply create an account in MongoDB Atlas which gives you a limited resource MongoDB cluster and so you can run/test your application.
MongoDB Atlas
When running MongoDB on a Linux Machine, you can simply use replication by updating connection string via editing service file
/usr/lib/mongod.service or /lib/systemd/system/mongod.service
and update it with following
ExecStart=/usr/bin/mongod --config "/etc/mongod.conf" --replSet rs0
where --config "/etc/mongod.conf" is pointing to your MongoDB Configuration file and --replSet rs0 is telling it to use replication with the name of rs0
and then restart
sudo systemctl daemon-reload //<--To reload service units
sudo systemctl restart mongod //<--To Restart MongoDB Server
and then initiate replication through your mongod instance in terminal
$ mongosh
$ rs.initiate()
I've been fighting against this issue for weeks. I let you my conclusion.
In order to be able to use transactions on a sharded cluster, you need to run at least MongoDB 4.2 on your cluster. If the cluster is not sharded, from 4.0.
I was using a library that has as a sub-dependency mongodb NodeJS driver. This driver from version 3.3.x fails against the sharded MongoDB cluster with version 4.0.4.
The solution for me was to update my cluster to 4.2 version.
src: https://www.bmc.com/blogs/mongodb-transactions/
Works for mongo:5.0.5-focal image.
Dockerfile:
FROM mongo:5.0.5-focal AS rs-mongo
# Make MongoDB a replica set to support transactions. Based on https://stackoverflow.com/a/68621185/1952977
RUN apt-get update && apt-get install patch
# How to create scripts/docker-entrypoint.sh.patch
# 1. Download the original file:
# wget https://github.com/docker-library/mongo/raw/master/5.0/docker-entrypoint.sh
# ( wget https://github.com/docker-library/mongo/raw/b5c0cd58cb5626fee4d963ce05ba4d9026deb265/5.0/docker-entrypoint.sh )
# 2. Make a copy of it:
# cp docker-entrypoint.sh docker-entrypoint-patched.sh
# 3. Add required modifications to docker-entrypoint-patched.sh
# 4. Create patch:
# diff -u docker-entrypoint.sh docker-entrypoint-patched.sh > scripts/docker-entrypoint.sh.patch
# 5. Clean up:
# rm docker-entrypoint.sh docker-entrypoint-patched.sh
COPY scripts/docker-entrypoint.sh.patch .
RUN patch /usr/local/bin/docker-entrypoint.sh docker-entrypoint.sh.patch
RUN mkdir -p /etc/mongo-key && chown mongodb:mongodb /etc/mongo-key
CMD ["--replSet", "rs", "--keyFile", "/etc/mongo-key/mongodb.key"]
scripts/docker-entrypoint.sh.patch:
--- docker-entrypoint.sh 2022-01-04 15:35:19.594435819 +0300
+++ docker-entrypoint-patched.sh 2022-01-06 10:16:26.285394681 +0300
## -288,6 +288,10 ##
fi
if [ -n "$shouldPerformInitdb" ]; then
+
+ openssl rand -base64 756 > /etc/mongo-key/mongodb.key
+ chmod 400 /etc/mongo-key/mongodb.key
+
mongodHackedArgs=( "$#" )
if _parse_config "$#"; then
_mongod_hack_ensure_arg_val --config "$tempConfigFile" "${mongodHackedArgs[#]}"
## -408,7 +412,14 ##
set -- "$#" --bind_ip_all
fi
- unset "${!MONGO_INITDB_#}"
+ echo 'Initiating replica set'
+ "$#" --logpath "/proc/$$/fd/1" --fork
+ echo 'rs.initiate({"_id":"rs","members":[{"_id":0,"host":"127.0.0.1:27017"}]});' | mongosh -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD"
+ "$#" --logpath "/proc/$$/fd/1" --shutdown
+ echo 'Done initiating replica set'
+
+ unset "${!MONGO_INITDB_#}"
+
fi
rm -f "$jsonConfigFile" "$tempConfigFile"
docker-compose.yml:
version: '3.9'
services:
mongo:
image: rs-mongo:current
restart: always
env_file:
- .env
ports:
- 127.0.0.1:27017:27017
volumes:
- mongo-db:/data/db
- mongo-configdb:/data/configdb
- mongo-key:/etc/mongo-key
volumes:
mongo-db:
driver: local
mongo-configdb:
driver: local
mongo-key:
driver: local
UPDATED: 6th of Jan, 2022
The error is because you are using MongoDB sessions and it is not configured on your system.
run this to install run-rs :-
npm install run-rs -g
run:-
run-rs -v 4.0.0 --shell
You should see the below output. Please be patient since MongoDB 4.0.0 is about 70MB.
$ run-rs -v 4.0.0 --shell
Downloading MongoDB 4.0.0
Copied MongoDB 4.0.0 to '/home/node/lib/node_modules/run-rs/4.0.0'
Purging database...
Running '/home/node/lib/node_modules/run-rs/4.0.0/mongod'
Starting replica set...
Started replica set on "mongodb://localhost:27017,localhost:27018,localhost:27019"
Running mongo shell: /home/node/lib/node_modules/run-rs/4.0.0/mongo
rs:PRIMARY>
You now have a replica set running MongoDB 4.0.0 locally. Run rs.status() to verify the replica set is running.
NOTE:- Your nodejs version should be $gte v3.1.0
I have been trying to install NODEJS on remote AWS instance of Red Hat Enterprise Linux 7.1
I had read some of the posts here and been on the node js website
I have tried t
curl --silent --location https://rpm.nodesource.com/setup | bash -
but then get the error
error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)
i even get this error if i run
sudo curl --silent --location https://rpm.nodesource.com/setup | bash -
I have also tried to change the permissions on that file but then it still doesnt install
Can someone suggest a better way / correct way of doing this?
Thanks
You just don't have permissions to install things. It doesn't matter you are running curl with sudo, because what really needs superuser permissions is the bash session inside which you run the script.
So, this would work.
curl --silent --location https://rpm.nodesource.com/setup | sudo bash -
At least on one of our ESXi RedHat virtual machines the easiest way I know to install nodejs is:
yum install epel-release
yum install nodejs
Let yum do all the heavy lifting :)
You may be running as a non root user.
Before running your actual curl command, run
sudo su
Then run (without sudo, since you are now running as root user)
curl --silent --location https://rpm.nodesource.com/setup | bash -