Skip to content

Instantly share code, notes, and snippets.

@gunjanpatel
Last active October 10, 2023 15:31
Show Gist options
  • Save gunjanpatel/37d306cd1585ece1179b to your computer and use it in GitHub Desktop.
Save gunjanpatel/37d306cd1585ece1179b to your computer and use it in GitHub Desktop.
amazon ec2 LAMP and FTP installation and setup

Amazon ec2 LAMP and FTP installation and setup

Tutorial: Installing a LAMP Web Server on Amazon Linux

Go through this amazon link http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html

Associate and Elastic IP address to your EC2 instance.

This is so you have the same EIP if you restart your EC2 server.

Click the following link to Elastic IP in your management console. Click "Allocate new Address" button.

Click the Action button and assign the new created EIP to you instance ID i-50d663a6.

http://docs.aws.amazon.com/AmazonVPC/latest/GettingStartedGuide/getting-started-assign-eip.html

FTP Setup in Amazon EC2 instance

Step #1: Install vsftpd

SSH to your EC2 server. Type:

sudo yum install vsftpd

Step #2: Open up the FTP ports on your EC2 instance

Next, you'll need to open up the FTP ports on your EC2 server. Log in to the AWS EC2 Management Console and select Security Groups from the navigation tree on the left. Select the security group assigned to your EC2 instance. Select the Inbound tab and add port range 20-21

Also add port range 1024-1048

Type Protocol Port Range Source
SSH TCP 22 ip/32
HTTP TCP 80 0.0.0.0/0
Custom TCP Rule TCP 20 - 21 0.0.0.0/0
Custom TCP Rule TCP 1024 - 1048 0.0.0.0/0

Step #3: Make updates to the vsftpd.conf file

Edit your vsftpd conf file by typing:

sudo nano /etc/vsftpd/vsftpd.conf

Disable anonymous FTP by changing this line:

anonymous_enable=YES

to

anonymous_enable=NO

Then add the following lines to the bottom of the vsftpd.conf file:

pasv_enable=YES
pasv_min_port=1024
pasv_max_port=1048
pasv_address=<Public IP of your instance>

Your vsftpd.conf file should look something like the following - except make sure to replace the pasv_address with your public facing IP address:

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

# Additional configuration
pasv_enable=YES
pasv_min_port=1024
pasv_max_port=1048
pasv_address=xx-xxx-xxx-xx
local_root=/var/www/html

Step #4: Restart vsftpd

Restart vsftpd by typing:

sudo /etc/init.d/vsftpd restart

Step #5: Create an FTP user

If you take a peek at /etc/vsftpd/user_list, you'll see the following:

# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

This is basically saying, "Don't allow these users FTP access." vsftpd will allow FTP access to any user not on this list.

So, in order to create a new FTP account, you may need to create a new user on your server. (Or, if you already have a user account that's not listed in /etc/vsftpd/user_list, you can skip to the next step.)

Creating a new user on an EC2 instance is pretty simple. For example, to create the user gunjan, type:

sudo adduser gunjan-ftp
sudo passwd gunjan-ftp

Step #6: Restricting users to their home directories

At this point, your FTP users are not restricted to their home directories. That's not very secure, but we can fix it pretty easily.

Edit your vsftpd conf file again by typing:

sudo nano /etc/vsftpd/vsftpd.conf

Un-comment out the line:

chroot_local_user=YES

Restart the vsftpd server again like so:

sudo /etc/init.d/vsftpd restart

Surviving a reboot

vsftpd doesn't automatically start when your server boots. If you're like me, that means that after rebooting your EC2 instance, you'll feel a moment of terror when FTP seems to be broken - but in reality, it's just not running!. Here's a handy way to fix that:

sudo chkconfig --level 345 vsftpd on

Alternatively, if you are using redhat, another way to manage your services is by using this nifty graphic user interface to control which services should automatically start: sudo ntsysv

To change the default FTP upload folder

Edit

edit /etc/vsftpd/vsftpd.conf

Create a new entry at the bottom of the page:

local_root=/var/www/html

To apply read, write, delete permission to the files under folder so that you can manage using a FTP device

sudo find /var/www/html -type d -exec chmod 755 {} \;

If still doesn't working

It will not be ok until you add your user to the group www by the following commands:

sudo usermod -a -G www <USER>

Note that you will probably need to add the user you created to the "FTP" usergroup:

gpasswd -a <usr> ftp

This documented is generated based on http://stackoverflow.com/questions/7052875/setting-up-ftp-on-amazon-cloud-server?answertab=votes#answer-11404078

Thanks.

@nileshlets
Copy link

Nice one goga. It helped me,

@carcagi
Copy link

carcagi commented Dec 8, 2016

Awesome, thanks a lot... but I have a connection denied message when I try to create new files... :(

@soopernerdy
Copy link

To correct carcagi's problem with permissions, i used the following to correct:

sudo chown -R YOUR_FTP_USERNAME /var/www/html

@mdadnanali
Copy link

when i restart vsftpd service i recive following error
Starting vsftpd for vsftpd: 500 OOPS: bad bool value in config file for: anonymous_enable


vsftpd.conf

Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

Uncomment this to allow local users to log in.
local_enable=YES

@samphors
Copy link

That's really useful

@bobsawyer
Copy link

This just saved me hours. Thank you!

@nadyshalaby
Copy link

Awesome Man. Now it works thank you a lot.

@IAMNGP
Copy link

IAMNGP commented Aug 3, 2018

that's cool, i did this

sudo chown -R <YOUR_FTP_USERNAME>:apache /var/www

after that it's working

@cantacoop
Copy link

Fix: To change the default FTP upload folder
local_root=/var/www/html to local_root=/var/www

Change chmod www folder
sudo chmod 555 /var/www

Set chmod html folder
sudo chmod -R 755 /var/www/html

Change owner folder
sudo chown -R youruser-ftp /var/www

This work for us ;)

@khasim1227
Copy link

Nice tutorial It helped me a lot thank U bro.

@tunele
Copy link

tunele commented Feb 6, 2019

this one is not working anymore on the new aws linux ami 2
sudo /etc/init.d/vsftpd restart
You should use instead:
sudo systemctl enable vsftpd.service
sudo systemctl restart vsftpd.service

@abdulrauf618
Copy link

This command works for me
sudo usermod -d /var/www/html ftpuser1

Copy link

ghost commented Jul 3, 2019

I am using AWS AMI Linux 2 and this still works flawless! Good job GunJanPatel!

@sunjava11
Copy link

Fix: To change the default FTP upload folder
local_root=/var/www/html to local_root=/var/www

Change chmod www folder
sudo chmod 555 /var/www

Set chmod html folder
sudo chmod -R 755 /var/www/html

Change owner folder
sudo chown -R youruser-ftp /var/www

This work for us ;)

This worked for me too. Awesome

@hexrom
Copy link

hexrom commented Nov 12, 2020

I had the following error 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

Fixed it by adding, allow_writeable_chroot=YES , to vsftpd.conf

@think4u
Copy link

think4u commented Jan 17, 2021

This command works for me
sudo usermod -d /var/www/html ftpuser1

This works for making connection using Filezilla to the amazon linux 2 instance.

@think4u
Copy link

think4u commented Jan 17, 2021

sudo chown -R

After updating the premissions. This one helped to upload and download files using Filezilla for Amazon linux 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment