How to deploy nginx without root access? - python

Recently I rented a web server at a certain company. I have ssh access but no root privileges. They don't excpect users to actually use ssh the main way of deploying is ftp or some cms/clickibunti. The OS if FreeBSD and they have a Python installation(2.7.8).
I downloaded virtualenv, Django and gunicorn (installed in the home directory and the virtualenv). I deployed the bootstrap Django app with the development server on 0.0.0.0:8000 which works fine. They deleted pkg and ports (packagemanagers) so I build nginx from source (installed into the /home/myuser/urs/local/bin/).
Now I'm stuck.
How do I deploy nginx without root access? Is this possible? I only have (write)access to /home/myuser/.
Disclaimer: They have a cash return policy so I'm not too concerned. But I have some free time and this seems like a nice problem to master.

Install it into your home directory, and change the config so it uses a port higher than 1024:
$ ./configure --prefix=/home/steve/nginx
$ make && make install
$ cd ~/nginx
$ vi conf/nginx.conf
Change:
server {
listen 80;
to:
server {
listen 8080;
Then start it:
$ ./sbin/nginx
$ netstat -na | grep 8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN

Related

Configuring python flask app in HTTPS mode in OpenShift [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to run a python flask app in https mode in openshift 4.7. I have my python flask app listening at port 8080 and this port is exposed through dockerFile configuration as shown below. I have redirected the https requests through the configuration in Openshift console by doing Service Port Mapping for https port 443 to pod port 8080. You can configure this by going to Openshift Console > Project > Services > Service Details and select your app. But still I can't access the service in https mode. When I try to access in https mode I get the error The application is currently not serving requests at this endpoint. It may not have been started or is still starting.
When I do normal deployment through oc cli and do not do any ssl configuration through openshift console, the app works fine in http mode. Please advise on how to run this in https mode
my app.py code is below
from flask import Flask, jsonify, request, redirect
app = Flask(__name__)
#app.route('/<string:name>/')
def helloName(name):
print(request)
return "Hello " + name
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug = True)
my Dockerfile content is as below and I am exposing port 8080 from it
# set base image
FROM registry.redhat.io/rhel8/python-38
# copy the dependencies file to the working directory
COPY requirements.txt .
# Configure PIP with local Nexus
ENV PIP_TRUSTED-HOST=nexus.company.com:8443
ENV PIP_INDEX=https://nexus.company.com:8443/repository/pypi-group/pypi
ENV PIP_INDEX-URL=https://nexus.company.com:8443/repository/pypi-group/simple
ENV PIP_NO-CACHE-DIR=false
ENV PIP_TIMEOUT=600
RUN touch ~/.netrc && \
echo "machine nexus.company.com" >> ~/.netrc && \
echo "login ${NEXUS_USER}" >> ~/.netrc && \
echo "password ${NEXUS_TOKEN}" >> ~/.netrc
# install dependencies
RUN pip install -U pip \
pip install -r requirements.txt
# copy the content of the local src directory to the working directory
COPY src/ .
EXPOSE 8080
# command to run on container start
CMD [ "python3", "./app.py" ]
Requirements.txt has only Flask
Two things here:
With host="0.0.0.0" Flask listens to the IP of the machine it is running on. I think changing this to 127.0.0.1 should be good.
app.run(host='127.0.0.1', port=8080, debug = True)
Probably even more important, the .run() method from Flask is only for development, for production you should use something like gunicorn or something.
link: https://gunicorn.org/
Cheers, T

Docker - connect to host Postgres raises: Error starting userland proxy: listen tcp4 127.0.0.1:5432: bind: address already in use [duplicate]

When I run docker-compose up in my Docker project it fails with the following message:
Error starting userland proxy: listen tcp 0.0.0.0:3000: bind: address already in use
netstat -pna | grep 3000
shows this:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN -
I've already tried docker-compose down, but it doesn't help.
In your case it was some other process that was using the port and as indicated in the comments, sudo netstat -pna | grep 3000 helped you in solving the problem.
While in other cases (I myself encountered it many times) it mostly is the same container running at some other instance. In that case docker ps was very helpful as often I left the same containers running in other directories and then tried running again at other places, where same container names were used.
How docker ps helped me:
docker rm -f $(docker ps -aq) is a short command which I use to remove all containers.
Edit: Added how docker ps helped me.
This helped me:
docker-compose down # Stop container on current dir if there is a docker-compose.yml
docker rm -fv $(docker ps -aq) # Remove all containers
sudo lsof -i -P -n | grep <port number> # List who's using the port
and then:
kill -9 <process id> (macOS) or sudo kill <process id> (Linux).
Source: comment by user Rub21.
I had the same problem. I fixed this by stopping the Apache2 service on my host.
You can kill the process listening on that port easily with one command below :
kill -9 $(lsof -t -i tcp:<port#>)
ex :
kill -9 $(lsof -t -i tcp:<port#>)
or for ubuntu:
sudo kill -9 `sudo lsof -t -i:8000`
Man page for lsof : https://man7.org/linux/man-pages/man8/lsof.8.html
-9 is for hard kill without checking any deps.
(Not related, but might be useful if its PORT 5000 mystery) - the culprit process is due to Mac OS monterery.
The port 5000 is commonly used to serve local development servers. When updating to the latest macOS operating system, I was unable the docker to bind to port 5000, because it was already in use. (You may find a message along the lines of Port 5000 already in use.)
By running lsof -i :5000, I found out the process using the port was named ControlCenter, which is a native macOS application. If this is happening to you, even if you use brute force (and kill) the application, it will restart itself. In my laptop, lsof -i :5000 returns that Control Center is being used by process id 433. I could do killall -p 433, but macOS keeps restarting the process.
The process running on this port turns out to be an AirPlay server. You can deactivate it in
System Preferences › Sharing, and unchecking AirPlay Receiver to release port 5000.
I had same problem,
docker-compose down --rmi all (in the same directory where you run docker-compose up)
helps
UPD: CAUTION - this will also delete the local docker images you've pulled (from comment)
For Linux/Unix:
Simple search for linux utility using following command
netstat -nlp | grep 8888
It'll show processing running at this port, then kill that process using PID (look for a PID in row) of that process.
kill PID
In some cases it is critical to perform a more in-depth debugging to the problem before stopping a container or killing a process.
Consider following the checklist below:
1) Check you current docker compose environment
Run docker-compose ps. If port is in use by another container, stop it with docker-compose stop <service-name-in-compose-file> or remove it by replacing stop with rm.
2) Check the containers running outside your current workspace
Run docker ps to see list of all containers running under your host.
If you find the port is in use by another container, you can stop it with docker stop <container-id>.
(*) Because you're not under the scope of the origin compose environment - it is a good practice first to use docker inspect to gather more information about the container that you're about to stop.
3) Check if port is used by other processes running on the host
For example if the port is 6379 run:
$ sudo netstat -ltnp | grep ':6379'
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 915/redis-server 12
tcp6 0 0 ::1:6379 :::* LISTEN 915/redis-server 12
(*) You can also use the lsof command which is mainly used to retrieve information about files that are opened by various processes (I suggest running netstat before that).
So, In case of the output above the PID is 915. Now you can run:
$ ps j 915
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
1 915 915 915 ? -1 Ssl 123 0:11 /usr/bin/redis-server 127.0.0.1:6379
And see the ID of the parent process (PPID) and the execution command.
You can also run: $ pstree -s <PID> to a visual display of the process and its related processes.
In our case we can see that the process probably is a daemon (PPID is 1) - In that case consider running: A) $ cat /proc/<PID>/status in order to get a more in-depth information about the process like the number of threads spawned by the process, its capabilities, etc'.
B) $ systemctl status <PID> in order to see the systemd unit that caused the creation of a specific process. If the service is not critical - you can stop and disable the service.
4) Restart Docker service
Run: sudo service docker restart.
5) You reached this point and..
Only if its not placing your system at risk - consider restarting the server.
In my case it was
Error starting userland proxy: listen tcp 0.0.0.0:9000: bind: address already in use
And all that I need is turn off debug listening in php storm
Most probably this is because you are already running a web server on your host OS, so it conflicts with the web server that Docker is attempting to start.
So try this one-liner before trying anything else:
sudo service apache2 stop; sudo service nginx stop; sudo nginx -s stop;
I had apache running on my ubuntu machine. I used this command to kill it!
sudo /etc/init.d/apache2 stop
I was getting the below error when i was trying to launch a new container -
listen tcp 0.0.0.0:8080: bind: address already in use.
To check which process is running on port 8080, run below command:
netstat -tulnp | grep 8080
i got the output below
[root#ip-112-x6x-2x-xxx.xxxxx.compute.internal (aws_main) ~]# netstat -tulnp | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN **12749**/java [root#ip-112-x6x-2x-xxx.xxxxx.compute.internal (aws_main) ~]#
run
kill -9 12749
Then try to relaunch the container it should work
If redis server is started as a service, it will restart itself when you using kill -9 <process_id> or sudo kill -9 `sudo lsof -t -i:<port_number>` . In that case you will need to stop the redis service using following command.
sudo service redis-server stop
I upgraded my docker this afternoon and ran into the same problem. I tried restarting docker but no luck.
Finally, I had to restart my computer and it worked. Definitely a bug.
Check docker-compose.yml, it might be the case that the port is specified twice.
version: '3'
services:
registry:
image: mysql:5.7
ports:
- "3306:3306" <--- remove either this line or next
- "127.0.0.1:3306:3306"
Changing network_mode: "bridge" to "host" did it for me.
This with
version: '2.2'
services:
bind:
image: sameersbn/bind:latest
dns: 127.0.0.1
ports:
- 172.17.42.1:53:53/udp
- 172.17.42.1:10000:10000
volumes:
- "/srv/docker/bind:/data"
environment:
- 'ROOT_PASSWORD=secret'
network_mode: "host"
I ran into the same issue several times. Restarting docker seems to do the trick
A variation of #DmitrySandalov's answer: I had tomcat/java running on 8080, which needed to keep going. Looked at the docker-compose.yml file and altered the entry for 8080 to another of my choosing.
nginx:
build: nginx
ports:
#- '8080:80' <-- original entry
- '8880:80'
- '8443:443'
Worked perfectly. (The only wrinkle is the change will be wiped if I ever update the project, since it's coming from an external repo.)
At first, make sure which service you are running in your specific port. In your case, you are already using port number 3000.
netstat -aof | findstr :3000
now stop that process which is running on specific port
lsof -i tcp:3000
I resolve the issue by restarting Docker.
It makes more sense to change the port of the docker update instead of shutting down other services that use port 80.
Just a side note if you have the same issue and is with Windows:
In my case the process in my way is just grafana-server.exe. Because I first downloaded the binary version and double click the executable, and it now starts as a service by user SYSTEM which I cannot taskkill (no permission)
I have to go to "Service manager" of Windows and search for service "Grafana", and stop it. After that port 3000 is no longer occupied.
Hope that helps.
The one that was using the port 8888 was Jupiter and I had to change the configuration file of Jupiter notebook to run on another port.
to list who is using that specific port.
sudo lsof -i -P -n | grep 9
You can specify the port you want Jupyter to run uncommenting/editing the following line in ~/.jupyter/jupyter_notebook_config.py:
c.NotebookApp.port = 9999
In case you don't have a jupyter_notebook_config.py try running jupyter notebook --generate-config. See this for further details on Jupyter configuration.
Before it was running on :docker run -d --name oracle -p 1521:1521 -p 5500:5500 qa/oracle
I just changed the port to docker run -d --name oracle -p 1522:1522 -p 5500:5500 qa/oracle
it worked fine for me !
On my machine a PID was not being shown from this command netstat -tulpn for the in-use port (8080), so i could not kill it, killing the containers and restarting the computer did not work. So service docker restart command restarted docker for me (ubuntu) and the port was no longer in use and i am a happy chap and off to lunch.
maybe it is too rude, but works for me. restart docker service itself
sudo service docker restart
hope it works for you also!
I have run the container with another port, like... 8082 :-)
I came across this problem. My simple solution is to remove the mongodb from the system
Commands to remove mongodb in Ubuntu:
sudo apt-get purge mongodb mongodb-clients mongodb-server mongodb-dev
sudo apt-get purge mongodb-10gen
sudo apt-get autoremove
Let me add one more case, because I had the same error and none of the solutions listed so far works:
serv1:
...
networks:
privnet:
ipv4_address: 10.10.100.2
...
serv2:
...
# no IP assignment, no dependencies
networks:
privnet:
ipam:
driver: default
config:
- subnet: 10.10.100.0/24
depending on the init order, serv2 may get assigned the IP 10.10.100.2 before serv1 is started, so I just assign IPs manually for all containers to avoid the error. Maybe there are other more elegant ways.
I have the same problem and by stopping docker container it was resolved.
sudo docker container stop <container-name>
i solved with this sudo service redis-server stop

Need to run django application in a domain name without specific port

I am newbie to Django recently I created a Django app and I uploaded to the server. I assigned a domain name to it. each time I run the server I need to type xyz.com:8000 to see my website. Is there any way to resolve this issue? Also, I have doubt. Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP.
You should set up a web server!
The web server is required for any site to work. Currently the most popular are Apache and NGINX. It is the web server that responds to user requests. We need to ensure the interaction of the web server and the python application. Most popular solutions:
uwsgi
gunicorn
Consider an example with Nginx and Gunicorn:
Let's start by installing the Gunicorn module in a virtual environment:
pip install gunicorn
Configure the gunicorn service settings for our project:
sudo nano /etc/systemd/system/gunicorn.service
/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi
[Install]
WantedBy=multi-user.target
We enable and run the gunicorn service, check its status:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
If all is well, install the nginx web server:
sudo apt install nginx
Configure the project site parameters:
sudo nano /etc/nginx/sites-available/project
/etc/nginx/sites-available/project:
server {
listen 80;
server_name <server IP or domain name>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/project_dir/project;
}
location /media/ {
root /home/project_dir/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/project_dir/project/project.sock;
}
}
We use Nginx as a proxy for the gunicorn Python server:
proxy_pass http://unix:/home/project_dir/project/project.sock;
Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:
sudo ln -s /etc/nginx/sites-available/project/etc/nginx/sites-enabled
We restart the Nginx service and add permissions to the firewall:
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
Done! You can check the operation of our site by typing the IP address of the server in the browser.
P.S. Sorry for my english! If you see an error in the text or code, please edit me.

Why i cannot start service web?

Help me with this error please.
ERROR: for web Cannot start service web: driver failed programming
external connectivity on endpoint semestral_dj01
(335d0ad4599512f3228b4ed0bd1bfed96f54af57cff4a553d88635f80ac2e26c):
Bind for 0.0.0.0:8000 failed: port is already allocated ERROR:
Encountered errors while bringing up the project.
Go to Terminal and run command:
lsof -i:8000
Where 8000 is the port number.
The result will be like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Python 123456 user ab type 123 000 TCP 0.0.0.0:8000
Now run command in terminal:
kill -9 <PID>
like
kill -9 123456
Then again run your server and the issue will be resolved.
The way i resolved this was by stopping the containers in execution and executing the one I wanted to start.
Use this command into your CMD for stop containers:
docker stop $(docker ps -a -q)
In the case you may want to delete them use this:
docker rm $(docker ps -a -q)
This happen to me time to time in my dev environment. Usually I have to restart docker service to get it working.
I encountered a very similar error. In my case, I had recently upgraded the native nginx version on the Linux box. After the upgrade, nginx automatically started (I had not noticed). When I deployed a docker image with nginx, the 2 nginx instances were competing for the same port (native and docker).
I saw it with:
> sudo netstat -nl -p tcp | grep 443
tcp 0a 0 0.0.0.0:443 0.0.0.0:* LISTEN #####/nginx: master
tcp6 0 0 :::443 :::* LISTEN #####/nginx: master
It was a bit confusing since I was trying to get nginx to run, and it said nginx was using the port. After I had typed docker-compose down, I realized nginx was still using the port, even though the nginx container was destroyed. That made me realize that the native nginx had started up again, even though I didn't manually start it.
My error message:
Cannot start service <webserver>: driver failed programming external connectivity on endpoint <server_instance>_webserver (...<guid>...): Error starting userland proxy: listen tcp 0.0.0.0:443: bind: address already in use

Pass commands from one docker container to another

I have a helper container and an app container.
The helper container handles mounting of code via git to a shared mount with the app container.
I need for the helper container to check for a package.json or requirements.txt in the cloned code and if one exists to run npm install or pip install -r requirements.txt, storing the dependencies in the shared mount.
Thing is the npm command and/or the pip command needs to be run from the app container to keep the helper container as generic and as agnostic as possible.
One solution would be to mount the docker socket to the helper container and run docker exec <command> <app container> but what if I have thousands of such apps on a single host.
Will there be issues having hundreds of containers all accessing the docker socket at the same time? And is there a better way to do this? Get commands run on another container?
Well there is no "container to container" internal communication layer like "ssh". In this regard, the containers are as standalone as 2 different VMs ( beside the network part in general ).
You might go the usual way, install opensshd-server on the "receiving" server, configure it key-based only. You do not need to export the port to the host, just connect to the port using the docker-internal network. Deploy the ssh private key on the 'caller server' and the public key into .ssh/authorized_keys on the 'receiving server' during container start time ( volume mount ) so you do not keep the secrets in the image (build time).
Probably also create a ssh-alias in .ssh/config and also set HostVerify to no, since the containers could be rebuild. Then do
ssh <alias> your-command
Found that better way I was looking for :-) .
Using supervisord and running the xml rpc server enables me to run something like:
supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi
supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi
In the helper container, this will connect to the rpc server running on port 9002 on the app container and execute a program block that may look something like;
[program:uwsgi]
directory=/app
command=/usr/sbin/uwsgi --ini /app/app.ini --uid nginx --gid nginx --plugins http,python --limit-as 512
autostart=false
autorestart=unexpected
stdout_logfile=/var/log/uwsgi/stdout.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/uwsgi/stderr.log
stderr_logfile_maxbytes=0
exitcodes=0
environment = HOME="/app", USER="nginx"]
This is exactly what I needed!
For anyone who finds this you'll probably need your supervisord.conf on your app container to look sth like:
[supervisord]
nodaemon=true
[supervisorctl]
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[inet_http_server]
port=127.0.0.1:9002
username=user
password=password
[program:uwsgi]
directory=/app
command=/usr/sbin/uwsgi --ini /app/app.ini --uid nginx --gid nginx --plugins http,python --limit-as 512
autostart=false
autorestart=unexpected
stdout_logfile=/var/log/uwsgi/stdout.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/uwsgi/stderr.log
stderr_logfile_maxbytes=0
exitcodes=0
environment = HOME="/app", USER="nginx"]
You can setup the inet_http_server to listen on a socket. You can link the containers to be able to access them at a hostname.

Categories