<?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: Mitch Dennett</title>
    <description>The latest articles on DEV Community by Mitch Dennett (@mitchdennett).</description>
    <link>https://dev.to/mitchdennett</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%2F98325%2F64c313b4-6e68-4fa1-a47c-24d3807df63a.jpg</url>
      <title>DEV Community: Mitch Dennett</title>
      <link>https://dev.to/mitchdennett</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mitchdennett"/>
    <language>en</language>
    <item>
      <title>Deploying A Masonite Project To AWS</title>
      <dc:creator>Mitch Dennett</dc:creator>
      <pubDate>Mon, 06 Apr 2020 18:53:14 +0000</pubDate>
      <link>https://dev.to/mitchdennett/deploying-a-masonite-project-to-aws-2hkm</link>
      <guid>https://dev.to/mitchdennett/deploying-a-masonite-project-to-aws-2hkm</guid>
      <description>&lt;h2&gt;
  
  
  Deploying Masonite To AWS
&lt;/h2&gt;

&lt;p&gt;In this article we are going to walk through the steps of deploying your Masonite python project to an EC2 instance on AWS. We are going to be using Ubuntu 18.04 LTS, Nginx as a front-end reverse proxy and uWSGI as the application server. &lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before moving on there are a few prerequisites.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An AWS account&lt;/li&gt;
&lt;li&gt;A Masonite project ready to be deployed&lt;/li&gt;
&lt;li&gt;All your dependencies should be inside your requirements.txt so we can easily install them.&lt;/li&gt;
&lt;li&gt;Some knowledge of AWS and EC2&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1 - Spin Up An EC2 Instance
&lt;/h3&gt;

&lt;p&gt;Our first step is to get an EC2 instance up and running with Ubuntu 18.04 LTS. Let's go ahead and open up the AWS console and head over to the EC2 dashboard. Launch an instance and make sure to select the Ubuntu 18.04 image. You can choose any instance type but I chose a T2 Nano for this purpose. Be sure to edit the security group to allow HTTP traffic through port 80. You can leave everything else as is for now. Just go ahead and click Review &amp;amp; Launch. Don't forget to download the Key Pair so we can SSH into the instance. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2 - Install Nginx
&lt;/h3&gt;

&lt;p&gt;Once the EC2 instance is up and running, SSH into it so we can install Nginx.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will install Nginx and all the required dependencies. To confirm lets check the status of Nginx.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see something like this below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-04-06 18:32:37 UTC; 10s ago
     Docs: man:nginx(8)
 Main PID: 2417 (nginx)
    Tasks: 2 (limit: 547)
   CGroup: /system.slice/nginx.service
           ├─2417 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2419 nginx: worker process
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see it looks as though Nginx is up and running. The best way to actually test this is to see if we can get Nginx to serve anything. &lt;/p&gt;

&lt;p&gt;Open up a browser and visit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://your_ec2_ip_address
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If all is working  you should see the default Nginx screen&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HvRBjDtO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9pnv2ei954w4vdwjkmo5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HvRBjDtO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9pnv2ei954w4vdwjkmo5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have Nginx installed and running we can move onto installing some necessary dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3 - Installing System Dependencies
&lt;/h3&gt;

&lt;p&gt;We need to install all the dependencies we need to get our Masonite project and uWSGI server working. We are going to install &lt;code&gt;pip&lt;/code&gt; and everything needed for uWSGI.&lt;/p&gt;

&lt;p&gt;Run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools python3-venv git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once this is done we can start setting up our python virtual environment. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 - Deploying Masonite Project
&lt;/h3&gt;

&lt;p&gt;Now we need to get our Masonite project into the project folder we just created. There are a few ways to do this. We are going to use git to clone our repo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; ~/masoniteproject
&lt;span class="nb"&gt;cd&lt;/span&gt; ~/masoniteproject
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/repoOwner/yourRepo &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will install your project into our current directory. Once your project is on the EC2 instance we can move onto installing your project dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 - Virtual Environment
&lt;/h3&gt;

&lt;p&gt;In order to isolate our Masonite application we need to create a Virtual Environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;python3.6 &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Those commands have created and activated our virtual environment allowing us to now install our python dependencies and our application server uWSGI&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6 - Installing Project Dependencies
&lt;/h3&gt;

&lt;p&gt;Before we install any project dependencies we are going to install a few things we need. Make sure your virtual environment is still activated.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;HINT: Please install using pip and not pip3.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now lets install our application server uWSGI and wheels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;wheel uwsgi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we can install all our project dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will install our masonite dependencies along with any other dependencies you have placed in &lt;code&gt;requirements.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can now deactivate our virtual environment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;deactivate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 7 - Creating a uWSGI configuration file
&lt;/h3&gt;

&lt;p&gt;The next step is to configure uWSGI to serve our application. First we need to create a uWSGI configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;nano ~/masoniteproject/project.ini
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following information into your &lt;code&gt;project.ini&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[uwsgi]&lt;/span&gt;
&lt;span class="py"&gt;module&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;wsgi&lt;/span&gt;
&lt;span class="py"&gt;callable&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;application&lt;/span&gt;

&lt;span class="py"&gt;master&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="py"&gt;processes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;5&lt;/span&gt;

&lt;span class="py"&gt;socket&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;project.sock&lt;/span&gt;
&lt;span class="py"&gt;chmod-socket&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;660&lt;/span&gt;
&lt;span class="py"&gt;vacuum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;

&lt;span class="py"&gt;die-on-term&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save your ini and let's move onto creating a service.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8 - Creating Systemd service file
&lt;/h3&gt;

&lt;p&gt;Creating a systemd unit file will allow Ubuntu to automatically start your uWSGI application server whenever it boots.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/systemd/system/project.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Put the following information into your service file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[Unit]&lt;/span&gt;
&lt;span class="py"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;uWSGI instance to serve your masonite project&lt;/span&gt;
&lt;span class="py"&gt;After&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;network.target&lt;/span&gt;

&lt;span class="nn"&gt;[Service]&lt;/span&gt;
&lt;span class="py"&gt;User&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;ubuntu&lt;/span&gt;
&lt;span class="py"&gt;Group&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;www-data&lt;/span&gt;
&lt;span class="py"&gt;WorkingDirectory&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/home/ubuntu/masoniteproject&lt;/span&gt;
&lt;span class="py"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"PATH=/home/ubuntu/masoniteproject/venv/bin"&lt;/span&gt;
&lt;span class="py"&gt;ExecStart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini&lt;/span&gt;

&lt;span class="nn"&gt;[Install]&lt;/span&gt;
&lt;span class="py"&gt;WantedBy&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;multi-user.target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save that file and now we have a service. We can now start our uWSGI server and enable it so it boots on startup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start project
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can check the status of your project using this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
● project.service - uWSGI instance to serve your masonite project
   Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-04-06 18:45:27 UTC; 8s ago
 Main PID: 8946 (uwsgi)
    Tasks: 6 (limit: 547)
   CGroup: /system.slice/project.service
           ├─8946 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
           ├─8975 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
           ├─8976 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
           ├─8977 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
           ├─8978 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
           └─8979 /home/ubuntu/masoniteproject/venv/bin/uwsgi --ini project.ini
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you see any errors, be sure to resolve them before continuing on to configure Nginx&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 9 - Configuring Nginx
&lt;/h3&gt;

&lt;p&gt;Let's start by creating an Nginx configuration file for our project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/sites-available/masoniteproject.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Put the following information into your conf file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;aws_ec2_ip_address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;include&lt;/span&gt; &lt;span class="s"&gt;uwsgi_params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;uwsgi_pass&lt;/span&gt; &lt;span class="s"&gt;unix:/home/ubuntu/masoniteproject/project.sock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To enable the Nginx server block configuration you’ve just created, link the file to the &lt;code&gt;sites-enabled&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /etc/nginx/sites-available/masoniteproject.conf /etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now lets restart Nginx to make our changes take effect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should now be able to navigate to your EC2's IP address in your web browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://aws_ec2_ip_address
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see your masonite project in your browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Ta da! Your masonite project is now hosted on an AWS EC2 instance. Make sure you set up your &lt;code&gt;.env&lt;/code&gt; file. We are missing an important piece to all this though. And that is that database. I'm going to be writing another article soon about connecting your Masonite EC2 instance to an AWS RDS database. Please leave any comments or questions below and I will try to answer them the best I can. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>ec2</category>
      <category>masonite</category>
      <category>python</category>
    </item>
  </channel>
</rss>
