VPS पर Swift ऐप डिप्लॉय: A से Z तक पूरी गाइड
\n\nSwift अब सिर्फ iOS डेवलपमेंट की भाषा नहीं रही। Vapor फ्रेमवर्क और Linux के आधिकारिक सपोर्ट की बदौलत, आप Swift में हाई-परफॉर्मेंस सर्वर एप्लिकेशन बना सकते हैं और उन्हें VPS पर डिप्लॉय कर सकते हैं। इस गाइड में हम सर्वर सेटअप से लेकर आपके Swift ऐप के प्रोडक्शन वर्जन को चलाने तक का पूरा सफर तय करेंगे।
\n\n1. VPS तैयार करना और Swift इंस्टॉल करना
\n\nशुरुआत के लिए आपको Ubuntu 22.04 LTS (या 20.04) वाला एक VPS चाहिए। SSH के जरिए कनेक्ट करें और सिस्टम अपडेट करें:
\n\nssh root@आपका-सर्वर-ip\napt update && apt upgrade -y\n\nSwift के लिए जरूरी डिपेंडेंसी इंस्टॉल करें:
\n\napt install -y curl git build-essential libssl-dev libcurl4-openssl-dev\napt install -y libxml2-dev libicu-dev libsqlite3-dev libz-dev\n\nअब Ubuntu के लिए Swift डाउनलोड और इंस्टॉल करें। नवीनतम वर्जन swift.org पर चेक करें। इस लेख के समय Swift 5.9 उपलब्ध है:
\n\ncd /opt\nwget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz\ntar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gz\necho 'export PATH=/opt/swift-5.9-RELEASE-ubuntu22.04/usr/bin:$PATH' >> ~/.bashrc\nsource ~/.bashrc\n\nइंस्टॉलेशन वेरिफाई करें:
\n\nswift --version\n\nआपको Swift 5.9 का वर्जन दिखना चाहिए।
\n\n2. Swift/Vapor एप्लिकेशन बनाना और बिल्ड करना
\n\nVapor Toolbox इंस्टॉल करें (वैकल्पिक लेकिन सुविधाजनक):
\n\ngit clone https://github.com/vapor/toolbox.git\ncd toolbox\nswift build -c release\ncp .build/release/vapor /usr/local/bin/\n\nएक टेस्ट Vapor एप्लिकेशन बनाएं। अगर आपके पास पहले से प्रोजेक्ट है, तो उसे SCP या Git के जरिए सर्वर पर कॉपी करें।
\n\ncd ~\nvapor new HelloServer\ncd HelloServer\n\nएक सिंपल endpoint जोड़ने के लिए Sources/App/routes.swift को एडिट करें:
import Vapor\n\nfunc routes(_ app: Application) throws {\n app.get { req async in\n "VPS पर Swift से नमस्ते!"\n }\n \n app.get("status") { req async -> HTTPStatus in\n return .ok\n }\n}\n\nप्रोजेक्ट को प्रोडक्शन मोड में बिल्ड करें:
\n\nswift build -c release\n\nबाइनरी फाइल .build/release/HelloServer में बनेगी।
3. systemd सर्विस सेटअप और ऑटो-स्टार्ट
\n\nसर्वर रीबूट होने पर ऐप अपने आप शुरू हो, इसके लिए systemd सर्विस बनाएं। पहले ऐप के लिए एक यूजर बनाएं (सुरक्षा के लिए):
\n\nuseradd -m -s /bin/bash swiftuser\nchown -R swiftuser:swiftuser /root/HelloServer\n\nसर्विस फाइल बनाएं:
\n\nnano /etc/systemd/system/helloserver.service\n\nइसमें निम्नलिखित कोड डालें:
\n\n[Unit]\nDescription=HelloServer Swift App\nAfter=network.target\n\n[Service]\nType=simple\nUser=swiftuser\nWorkingDirectory=/root/HelloServer\nExecStart=/root/HelloServer/.build/release/HelloServer --host 127.0.0.1 --port 8080\nRestart=on-failure\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target\n\nसर्विस शुरू करें:
\n\nsystemctl daemon-reload\nsystemctl enable helloserver\nsystemctl start helloserver\nsystemctl status helloserver\n\nजांचें कि ऐप लोकल रूप से रिस्पॉन्स कर रहा है:
\n\ncurl http://127.0.0.1:8080\n\n4. Nginx को रिवर्स प्रॉक्सी के रूप में सेटअप करना
\n\nप्रोडक्शन एनवायरनमेंट में पोर्ट 8080 को सीधे खोलने की अनुशंसा नहीं की जाती। Nginx इंस्टॉल करें और SSL (Let's Encrypt) के साथ प्रॉक्सी सेटअप करें।
\n\napt install -y nginx certbot python3-certbot-nginx\n\nNginx कॉन्फ़िगरेशन बनाएं:
\n\nnano /etc/nginx/sites-available/helloserver\n\nइसमें डालें:
\n\nserver {\n listen 80;\