Skip to content

Instantly share code, notes, and snippets.

@dragonjet
Last active July 31, 2021 21:01
Show Gist options
  • Save dragonjet/270cf0139df45d1b7690 to your computer and use it in GitHub Desktop.
Save dragonjet/270cf0139df45d1b7690 to your computer and use it in GitHub Desktop.
Setup Web Server on EC2 Amazon Linux AMI

Environment

This assumes you are now connected to the server via SSH.

  • sudo -s Enter root mode for admin access
  • groupadd devgroup Create new group to be later granted access to /var/www/html

Creating a new Root User

  • useradd -G root,devgroup masterdev Create new root user. Also add to the devgroup
  • passwd masterdev Change password for the new root user
  • At this point, you'll need to input your new root user's new password

Further User Configuration

Next, we'll need to set PasswordAuthentication to On. By default, passwords cannot be used on SSH (Off), so you initially need to use the pem or ppk files. However, since we now have created our new root user, we can change this and set it to "On".

  • sudo vi /etc/ssh/sshd_config Edit SSH config file
  • Make sure this is set: PasswordAuthentication yes
  • sudo service sshd restart

Add as sudoer

  • sudo vi /etc/sudoers Edit the sudoers file
  • Add masterdev user after the root's line
root ALL=(ALL) ALL
masterdev ALL=NOPASSWD: ALL

Setup WebServer (PHP)

Installation

  • sudo yum update -y Update linux packages
  • sudo yum -y install httpd24 Install webserver
  • sudo chkconfig httpd on Check for config errors
  • sudo service httpd start Start the webserver
  • sudo yum -y install php55 php55-devel php55-common php55-cli php55-pecl-apc php55-pdo php55-mysql php55-xml php55-gd php55-mbstring php-pear php55-mysqlnd php55-mcrypt Install php and the usual extensions.
  • sudo service httpd restart Restart the webserver

Configuration

  • Apache Configuration sudo vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
</Directory>
  • PHP Configuration sudo vi /etc/php.ini
error_log = /var/log/php-error.log
date.timezone = "UTC"
  • After configurations sudo service httpd restart

Other

  • sudo usermod -a -G devgroup apache Add apache to dev group
  • sudo service httpd restart

Setup MySQL

  • If you will be using Amazon RDS or any separate database server, skip the whole MySQL Section. This is only required when you want to use a "localhost" MySQL on your server.

Installation

  • sudo yum -y install mysql mysql-server Install
  • sudo mysqladmin -u root password 'NEW_ROOT_PASSWORD' Setup root password
  • sudo chkconfig mysqld on Check if there are config errors
  • sudo service mysqld start Start the mysql service
  • sudo mysql -u root -p Login to MySQL via console
  • At this point you will need to enter your password

MySQL Console

  • After entering your password, the console supposedly changed to mysql>.
  • At this point you can execute MySQL queries, not linux commands.
  • Execute the following code to test running queries:
DROP DATABASE test;
DELETE FROM mysql.user WHERE user = '';
FLUSH PRIVILEGES;
  • To exit the MySQL Console, you can type exit, or press Ctrl+C.
@rdibona
Copy link

rdibona commented Oct 28, 2015

Thanks for this. It seems like in step 2 (b) for nginx, the proper 3rd command for "setup service" should be: chkconfig nginx on instead of chkconfig httpd on

@vinaythoke
Copy link

This is brilliant. Thank you so much for this. This saved me so much of surfing and searching on google. 👍

@tetreault
Copy link

Just wanted to add that its really bad practice to use passwords. If your'e the admin on the server get the user's public keys and create their user account then add their pub key to new-user/.ssh/authorized_keys. Feel free to also reference some steps i've jotted down: https://github.com/MSCHF/aws-ec2-node-npm-setup

@WilldelaVega777
Copy link

Amazing Work!!!

Please consider changing in step 2b (Install Nginx Web Server) the following:

chkconfig httpd on

to:

chkconfig nginx on

@jrbattles
Copy link

I used SFTP with keys insted of dev.... Having problems now with "permission denied" when I attempt to upload a file.

@alfredtan
Copy link

Thank you!

@yevgnenll
Copy link

awesome!! thx!

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