<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tushar Phalke</title>
    <description>The latest articles on DEV Community by Tushar Phalke (@tushar_phalke_ca389f96acc).</description>
    <link>https://dev.to/tushar_phalke_ca389f96acc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2098142%2F764f0267-c2f0-4bcd-b98a-c282d0ff18fe.jpeg</url>
      <title>DEV Community: Tushar Phalke</title>
      <link>https://dev.to/tushar_phalke_ca389f96acc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tushar_phalke_ca389f96acc"/>
    <language>en</language>
    <item>
      <title>The Ultimate Guide to Hosting Angular Applications on NGINX</title>
      <dc:creator>Tushar Phalke</dc:creator>
      <pubDate>Thu, 19 Sep 2024 17:01:54 +0000</pubDate>
      <link>https://dev.to/tushar_phalke_ca389f96acc/the-ultimate-guide-to-hosting-angular-applications-on-nginx-1kam</link>
      <guid>https://dev.to/tushar_phalke_ca389f96acc/the-ultimate-guide-to-hosting-angular-applications-on-nginx-1kam</guid>
      <description>&lt;p&gt;Hosting your Angular application on an NGINX server can enhance performance, provide better security, and enable easier configuration for production environments. Below is a step-by-step guide to deploying your Angular application on NGINX.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;p&gt;NGINX installed: Ensure you have NGINX installed on your server. You can install it on a Linux-based system using:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;sudo apt update&lt;br&gt;
sudo apt install nginx&lt;br&gt;
Angular Application: Ensure your Angular application is ready for production and built using the following command:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;ng build --prod&lt;br&gt;
Access to Server: You should have SSH access or direct access to the server where you want to deploy your application.&lt;br&gt;
Step 1: Build Your Angular Application&lt;/p&gt;

&lt;p&gt;Run the following command in your Angular project directory to build the application for production:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;ng build --prod&lt;br&gt;
This command will generate the production-ready files in the dist folder of your project.&lt;/p&gt;

&lt;p&gt;Step 2: Copy Files to the Server&lt;/p&gt;

&lt;p&gt;You will need to copy the contents of your Angular dist folder to your server. You can use scp or rsync. Assuming your dist folder is named dist/my-angular-app, run:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;scp -r dist/my-angular-app/* username@your_server_ip:/var/www/html/&lt;br&gt;
Replace username with your server username and your_server_ip with your server’s IP address.&lt;/p&gt;

&lt;p&gt;Step 3: Configure NGINX&lt;/p&gt;

&lt;p&gt;Once the files are on the server, you need to configure NGINX to serve your Angular application. Create a new configuration file for your application:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;sudo nano /etc/nginx/sites-available/my-angular-app&lt;br&gt;
Insert the following configuration into the file:&lt;/p&gt;

&lt;p&gt;nginx&lt;/p&gt;

&lt;p&gt;server {&lt;br&gt;
    listen 80;&lt;br&gt;
    server_name your_domain.com; # Replace with your domain or server IP&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location / {
    root /var/www/html; # Path to the folder where your files are located
    try_files $uri $uri/ /index.html; # Fallback to index.html for Angular routing
}

error_page 404 /index.html; # Handle 404 errors by redirecting to index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
Save and exit the editor.&lt;/p&gt;

&lt;p&gt;Step 4: Enable the Configuration&lt;/p&gt;

&lt;p&gt;Create a symbolic link to enable the site configuration:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;sudo ln -s /etc/nginx/sites-available/my-angular-app /etc/nginx/sites-enabled/&lt;br&gt;
Step 5: Test Your NGINX Configuration&lt;/p&gt;

&lt;p&gt;Before restarting NGINX, you should test the configuration to ensure there are no syntax errors:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;sudo nginx -t&lt;br&gt;
If everything is correct, you’ll see a message indicating the test was successful.&lt;/p&gt;

&lt;p&gt;Step 6: Restart NGINX&lt;/p&gt;

&lt;p&gt;Restart NGINX to apply the changes:&lt;/p&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;p&gt;sudo systemctl restart nginx&lt;br&gt;
Step 7: Access Your Application&lt;/p&gt;

&lt;p&gt;Now, your Angular application should be accessible through your domain or public IP address. Open a web browser and visit your domain (e.g., &lt;a href="http://your_domain.com" rel="noopener noreferrer"&gt;http://your_domain.com&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Additional Tips&lt;/p&gt;

&lt;p&gt;SSL Configuration: It’s advisable to set up SSL for secure access. You can use Let's Encrypt to obtain free SSL certificates.&lt;br&gt;
Error Handling: Implement proper error handling to manage unexpected cases.&lt;br&gt;
Caching: Consider configuring caching in NGINX for better performance.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>nginx</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
