JupyterLab is a powerful web-based development environment that allows users to work with interactive computing resources across multiple programming languages. In this tutorial, we will guide you through the process of installing and configuring JupyterLab on a Rocky Linux 9 server. By following these steps, you’ll be able to set up a JupyterLab environment and access it using a domain name.
Prerequisites
Before we begin, make sure you have the following:
- A server running Rocky Linux 9.
- A non-root user with sudo privileges.
- A fully qualified domain name (FQDN) pointing to your server. For the purpose of this tutorial, we will use
jupyterlab.example.com
as the domain name. - SELinux disabled.
Step 1: Configure the Firewall
The first step is to configure the firewall on your Rocky Linux server. By default, Rocky Linux uses Firewalld Firewall. To check the firewall’s status, run the following command:
sudo firewall-cmd --state
If the firewall is running, proceed to list all the services and ports active on the firewall:
sudo firewall-cmd --permanent --list-services
Ensure that the following services are active: cockpit
,dhcpv6-client
, and ssh
. If any of these services are not active, you can enable them using the following commands:
sudo firewall-cmd --add-service=http --permanent sudo firewall-cmd --add-service=https --permanent
Finally, reload the firewall to apply the changes:
sudo firewall-cmd --reload
Step 2: Install Nginx
Rocky Linux 9 ships with an older version of Nginx. To install the latest version, you’ll need to download the official Nginx repository. Follow these steps to do so:
- Create and open the
nginx.repo
file for creating the official Nginx repository:
sudo nano /etc/yum.repos.d/nginx.repo
- Paste the following code into the file:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- Save the file by pressing
Ctrl + X
and enteringY
when prompted. - Install the Nginx server:
sudo dnf install nginx -y
- Verify the installation:
nginx -v
You should see the version of Nginx installed on your system.
- Enable and start the Nginx server:
sudo systemctl enable nginx --now
- Check the status of the server to ensure it’s running:
sudo systemctl status nginx
Step 3: Install JupyterLab
Before installing JupyterLab, let’s check the version of Python installed on your system:
python -V
Make sure you have Python 3.9.14 or a compatible version installed. If not, you can install it using the following command:
sudo dnf install python3 -y
Next, install the PIP package manager:
sudo dnf install python3-pip -y
Create a directory for JupyterLab:
mkdir jupyterlab
Switch to the newly created directory:
cd ~/jupyterlab
Set up a virtual environment called jupyterlab_env
:
python3 -m venv --system-site-packages jupyterlab_env
Activate the virtual environment:
source jupyterlab_env/bin/activate
Upgrade the Pip package manager:
pip install --upgrade pip
Install JupyterLab:
pip install jupyterlab
Step 4: Configure JupyterLab
By default, JupyterLab generates a new token each time it starts to grant access to the interface. In this step, we’ll replace the token with password authentication.
Generate a password hash by running the following command:
python3 -c "from jupyter_server.auth import passwd; print(passwd('YOUR_PASSWORD'))"
Replace 'YOUR_PASSWORD'
with a strong password of your choice. Make a note of the generated password hash.
Create a JupyterLab configuration file:
jupyter lab --generate-config
Open the configuration file for editing:
nano ~/.jupyter/jupyter_lab_config.py
Find the following variables, uncomment them, and update their values as shown below:
c.ServerApp.allow_remote_access = True c.ServerApp.password = 'PASSWORD_HASH'
Replace 'PASSWORD_HASH'
with the password hash generated earlier.
Save the file by pressing Ctrl + X
and entering Y
when prompted.
Open port 8888 in the firewall to allow access to JupyterLab:
sudo firewall-cmd --add-port=8888/tcp --permanent sudo firewall-cmd --reload
Now, run the JupyterLab server with the following command:
jupyter lab --ip0.0.0.0
This command starts the JupyterLab server on port 8888. To access the JupyterLab interface, open your web browser and enter the URL http://<your_server_IP>:8888/
. You should see the JupyterLab login screen.
Enter your password and click the “Login” button to access the JupyterLab interface.
To stop the server, press Ctrl + C
and enter y
when prompted.
Step 5: Create a JupyterLab Service File
To run JupyterLab as a service, we need to create a service file. Follow these steps to create the service file:
- Exit the virtual environment:
deactivate
- Create a new service file for JupyterLab:
sudo nano /lib/systemd/system/jupyterlab.service
- Paste the following code into the file:
[Unit]
Description=JupyterLab Server
[Service]
User=shapehost
Group=shapehost
Type=simple
WorkingDirectory=/home/shapehost/jupyterlab
ExecStart=/home/shapehost/jupyterlab/jupyterlab_env/bin/jupyter-lab --config=/home/shapehost/.jupyter/jupyter_lab_config.py
StandardOutput=null
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
- Save the file by pressing
Ctrl + X
and enteringY
when prompted. - Replace
'shapehost'
with your system’s current logged-in user. - Initialize the JupyterLab service:
sudo systemctl daemon-reload
- Enable and start the JupyterLab service:
sudo systemctl enable jupyterlab --now
- Check the status of the service to ensure it’s running:
sudo systemctl status jupyterlab
Step 6: Install SSL
Before we can access JupyterLab over HTTPS, we need to install an SSL certificate. In this step, we’ll use Certbot to obtain a free SSL certificate from Let’s Encrypt.
To install Certbot, we’ll use the Snapd package installer. Follow these steps to install Certbot:
- Install the Epel repository:
sudo dnf install epel-release -y
- Install Snapd:
sudo dnf install snapd -y
- Enable and start the Snap service:
sudo systemctl enable snapd.socket --now
- Create the necessary links for Snapd to work:
sudo ln -s /var/lib/snapd/snap/snap echo'export PATH=$PATH: /var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snapd.sh
- Ensure that Snapd is up to date:
sudo snap install core sudo snap refresh core
- Install Certbot:
sudo snap install --classic certbot
- Create a symbolic link to make the Certbot command accessible:
sudo ln -s /snap/bin/certbot/usr/bin/certbot
- Generate an SSL certificate:
sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m test@example.com -d jupyterlab.example.com
Make sure to replace 'jupyterlab.example.com'
with your domain name.
- Generate a Diffie-Hellman group certificate:
sudo openssl dhparam-dsaparam -out /etc/ssl/certs/dhparam.pem4096
- Perform a dry run to test the SSL certificate renewal process:
sudo certbot renew --dry-run
If you see no errors, your certificate will renew automatically when needed.
Step 7: Configure Nginx
In this step, we’ll configure Nginx as a reverse proxy to serve JupyterLab over HTTPS.
- Create and open the
jupyterlab.conf
file for editing:
sudo nano /etc/nginx/conf.d/jupyterlab.conf
- Paste the following code into the file, replacing all instances of
jupyterlab.example.com
with your domain:
## enforce HTTPS server { listen 80; listen [::]:80; server_name jupyterlab.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name jupyterlab.example.com; access_log /var/log/nginx/jupyterlab.access.log; error_log /var/log/nginx/jupyterlab.error.log; client_max_body_size 20m; http2_push_preload on; # Enable HTTP/2 Server Push ssl_certificate /etc/letsencrypt/live/jupyterlab.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/jupyterlab.example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/jupyterlab.example.com/chain.pem; ssl_session_timeout 1d; # Enable TLS versions (TLSv1.3 is required for upcoming HTTP/3 QUIC). ssl_protocols TLSv1.2 TLSv1.3; # Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to # prevent replay attacks. # @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data ssl_early_data on; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384'; ssl_prefer_server_ciphers on; ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; # OCSP Stapling # Fetch OCSP records from URL in ssl_certificate and cache them ssl_stapling on; ssl_stapling_verify on; ssl_dhparam /etc/ssl/certs/dhparam.pem; add_header X-Early-Data $tls1_3_early_data; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header X-Scheme $scheme; proxy_buffering off; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8888; } }
- Save the file by pressing
Ctrl + X
and enteringY
when prompted. - Open the
nginx.conf
file for editing:
sudo nano /etc/nginx/nginx.conf
- Add the following line before the line
include /etc/nginx/conf.d/*.conf;
:
server_names_hash_bucket_size 64;
- Save the file by pressing
Ctrl + X
and enteringY
when prompted. - Verify the Nginx configuration:
sudo nginx -t
If there are no errors, reload the Nginx server:
sudo systemctl reload nginx
Congratulations! You can now access JupyterLab using the URL https://jupyterlab.example.com
in your web browser.
Conclusion
In this tutorial, we have walked you through the process of installing and configuring JupyterLab on a Rocky Linux 9 server. By following the steps outlined in this tutorial, you have successfully set up a JupyterLab environment and accessed it using a domain name. JupyterLab provides a powerful and flexible development environment for interactive computing, allowing you to work with various programming languages seamlessly.
If you have any questions or encounter any issues during the installation and configuration process, feel free to reach out to our support team at Shape.host. We provide reliable and scalable Cloud VPS solutions, ensuring your JupyterLab environment runs smoothly and securely. Empower your business with efficient cloud hosting solutions from Shape.host.