<?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: Ramon Zuniga</title>
    <description>The latest articles on DEV Community by Ramon Zuniga (@monchoz).</description>
    <link>https://dev.to/monchoz</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%2F179752%2Ffe170a34-757b-4762-8ebf-6b151f0b523c.jpg</url>
      <title>DEV Community: Ramon Zuniga</title>
      <link>https://dev.to/monchoz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/monchoz"/>
    <language>en</language>
    <item>
      <title>Python + Flask + Selenium + Chromedriver + Lightsail + SSL</title>
      <dc:creator>Ramon Zuniga</dc:creator>
      <pubDate>Thu, 17 Sep 2020 21:20:19 +0000</pubDate>
      <link>https://dev.to/monchoz/python-flask-selenium-chromedriver-lightsail-ssl-568c</link>
      <guid>https://dev.to/monchoz/python-flask-selenium-chromedriver-lightsail-ssl-568c</guid>
      <description>&lt;h3&gt;
  
  
  1. Secure the server
&lt;/h3&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# edit the configuration file
sudo nano /etc/ssh/sshd_config

# set these lines, if not already set
PermitRootLogin no
PasswordAuthentication no
ChallengeResponseAuthentication no
PubkeyAuthentication yes

# restart the service
sudo service ssh restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Install dependencies
&lt;/h3&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get -y update
sudo apt-get -y install python3 python3-venv python3-dev
sudo apt-get -y install supervisor nginx git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Install the application
&lt;/h3&gt;




&lt;p&gt;Follow repo instructions. Remember to install everything inside the virtual environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Setup Gunicorn and Supervisor
&lt;/h3&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/supervisor/conf.d/transit.conf

# example file
[program:transit-cu]
command=/home/ubuntu/transit-cu/venv/bin/gunicorn -b localhost:8000 -w 3 transit:app
directory=/home/ubuntu/transit-cu
user=ubuntu
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Enable Lightsail HTTPS rule and create a private address
&lt;/h3&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpgymrwskfdgb3uz6idyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpgymrwskfdgb3uz6idyp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Update A record on DNS
&lt;/h3&gt;




&lt;p&gt;My domain is registered with Godaddy and also created a sub-domain to setup as my server name. Go to your DNS manager page and update or create an A record with your host pointing to the Static IP address provided by Lightsail.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Setup NGINX
&lt;/h3&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/transit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace "transit" with your app name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5fps3sao7zchg72edro0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5fps3sao7zchg72edro0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Setup Let's Encrypt
&lt;/h3&gt;




&lt;p&gt;I decided to install the certificate manually since &lt;code&gt;certbot --nginx&lt;/code&gt; failed to reach my domain. However it worked later after I was able to solve the certbot challenge.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo certbot -d mydomain.com --manual --preferred-challenges dns certonly&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Confirm any required step and then create a TXT record on your domain DNS manager. In my case, It went like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5hz8rxgmx4ko2ssyw7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5hz8rxgmx4ko2ssyw7m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The name was &lt;em&gt;_acme-challenge.rsscraper&lt;/em&gt; which was different from what cerbot displayed. Actually just the first part of the entire string.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Install Chrome (headless) and Chromedriver on Ubuntu Server 18.04
&lt;/h3&gt;




&lt;p&gt;Let's go back into the project dependencies since this is a tricky part, use the following commands to install the latest Chrome version available:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;curl&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//intoli.com/install-google-chrome.sh | bash&lt;/span&gt;

&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;mv&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;stable&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt;

&lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;which&lt;/span&gt; &lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you'll have to install the respective chrome driver, go to this &lt;a href="http://chromedriver.chromium.org/downloads" rel="noopener noreferrer"&gt;site&lt;/a&gt; and download it using &lt;code&gt;wget&lt;/code&gt; then unzip the package and move to the binaries folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="nx"&gt;wget&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip&lt;/span&gt;
&lt;span class="nx"&gt;unzip&lt;/span&gt; &lt;span class="nx"&gt;chromedriver_linux64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;zip&lt;/span&gt;
&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;mv&lt;/span&gt; &lt;span class="nx"&gt;chromedriver&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;chromedriver&lt;/span&gt;
&lt;span class="nx"&gt;chromedriver&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The above code is just an example.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Make sure the way you use Selenium with the Chrome driver goes as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;selenium&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Options&lt;/span&gt;
&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headless&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;True&lt;/span&gt;
&lt;span class="nx"&gt;browser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./chromedriver&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_window_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1920&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1080&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not sure if the &lt;code&gt;set_window_size&lt;/code&gt; is required with this driver but that fixes issues when the browser screen is not able to see other elements because of the responsiveness of the site and your scrapping code might fail when trying to find elements.&lt;/p&gt;

&lt;p&gt;The Important part here is that you have to either specify the binary location of the chrome driver or leave it on the root location of the project (like in my case).&lt;/p&gt;

&lt;p&gt;You may experience issues due to driver incompatibility with the Chrome version so that's why it was better for me to have it inside the project.&lt;/p&gt;

&lt;p&gt;Another possible issue that you could have is related to some &lt;code&gt;lib&lt;/code&gt; libraries because we are using a server version that is not connected to a monitor. The issues are related with display and can be easily fixed with a google search.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Initializing services
&lt;/h3&gt;




&lt;p&gt;If everything was done correctly with all your project dependencies, you should be able to access to  your endpoints using your custom domain name after running the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Reload supervisor&lt;/span&gt;

&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;supervisorctl&lt;/span&gt; &lt;span class="nx"&gt;reload&lt;/span&gt;
&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;supervisorctl&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;

&lt;span class="c1"&gt;// Check Nginx syntax and reload&lt;/span&gt;

&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;nginx&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;
&lt;span class="nx"&gt;sudo&lt;/span&gt; &lt;span class="nx"&gt;service&lt;/span&gt; &lt;span class="nx"&gt;nginx&lt;/span&gt; &lt;span class="nx"&gt;reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/dandalpiaz/lightsail-flask" rel="noopener noreferrer"&gt;https://github.com/dandalpiaz/lightsail-flask&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux" rel="noopener noreferrer"&gt;https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://chromedriver.chromium.org/downloads" rel="noopener noreferrer"&gt;http://chromedriver.chromium.org/downloads&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@praneeth.jm/running-chromedriver-and-selenium-in-python-on-an-aws-ec2-instance-2fb4ad633bb5" rel="noopener noreferrer"&gt;https://medium.com/@praneeth.jm/running-chromedriver-and-selenium-in-python-on-an-aws-ec2-instance-2fb4ad633bb5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/43382548/can-i-still-specify-a-path-to-chromedriver-using-chromeoptions-in-python" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/43382548/can-i-still-specify-a-path-to-chromedriver-using-chromeoptions-in-python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-using-lets-encrypt-certificates-with-nginx#request-a-lets-encrypt-certificate-nginx" rel="noopener noreferrer"&gt;https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-using-lets-encrypt-certificates-with-nginx#request-a-lets-encrypt-certificate-nginx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.godaddy.com/community/Managing-Domains/Problem-adding-txt-record-for-letsencrypt/td-p/115376#" rel="noopener noreferrer"&gt;https://www.godaddy.com/community/Managing-Domains/Problem-adding-txt-record-for-letsencrypt/td-p/115376#&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>selenium</category>
      <category>lightsail</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
