Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

How to Install And Configure Apache In Ubuntu

How to Install And Configure Apache In Ubuntu


Getting apache onto your Ubuntu machine is easy. Using either the Synaptic Package Manager, Ubuntu SoftwareCenter, search and install the “apache2” module. Alternatively, you can open a terminal and type the following command:
sudo apt-get update
sudo apt-get install apache2
Once the installation finished, open a browser and go to the URL “http://localhost“. If you see the word “It Works!“, then your installation of apache is successful.sudo apt-get update


Start, Stop and Restart Apache
After you have installed Apache, it will be added to the init.d list and will auto start whenever you boot up your computer. The following commands allow you to start, restart, stop Apache.
sudo /etc/init.d/apache2 start   #start apache
sudo /etc/init.d/apache2 stop   #stop apache
sudo /etc/init.d/apache2 restart   #restart apache
To prevent Apache from autostart when booting up:
sudo update-rc.d -f apache2 remove
To restore Apache back to the autostart list:
sudo update-rc.d apache2 defaults
Notethe above commands will work in debian-based distro (including Ubuntu) only.
Changing the default localhost folder
By default, apache will operate on the “/var/www” folder. This means that whatever files you place in this /var/www folder will be visible from the URL http://localhost. In some instances, you may want the “localhost” to point to another folder instead, say /home/user/public_html. Here is how you do it:
First, make sure the /home/damien/public_html folder exists. Create a simple html file, name it index.html and place it in the public_html folder.
Open a terminal and type:
gksu gedit /etc/apache2/sites-enabled/000-default
Change DocumentRoot /var/www to DocumentRoot /home/user/public_html.
Change <Directory /var/www/> to <Directory /home/user/public_html/>.
Save and exit the file.
Restart the apache
sudo /etc/init.d/apache2 restart
Now, in your browser, reload the URL http://localhost. You should see the html file that you have placed in the public_html folder.

Configuring different sites
The above trick allows you to change the default operating folder of apache, however, some of you might not want to override the default settings. An alternative is to create multiple sites and point apache to the active site.
Create a new settings file for your new site.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1
Next, edit this settings file.
gksu gedit /etc/apache2/sites-available/site1
Change DocumentRoot /var/www to DocumentRoot /home/user/public_html.
Change <Directory /var/www/> to <Directory /home/user/public_html/>.
Save and exit the file.
Disable the default setting and make active the site1 settings
sudo a2dissite default && sudo a2ensite site1
Lastly, restart the apache.
sudo /etc/init.d/apache2 restart
With this trick, you can create multiple site configuration file, each pointing to a different folder. You can then easily switch between the sites with the a2dissite and a2ensite command
Enabling .htaccess file
.htaccess file is a powerful file that can be used to control and customize a site server behavior without editing the core Apache module. By default, the .htaccess functionality is turned off and all instances of .htaccess files are completely ignored. The server will not even attempt to read .htaccess files in the filesystem.
To enable .htaccess file, open up the settings file that you have created earlier:
gksu gedit /etc/apache2/sites-available/site1
Scroll down the file until you see the part “<Directory /home/user/public_html/>“. Underneath that line of code, change AllowOverride None to AllowOverride All.



Save and exit the file.




Generating Your SSH Keys


SSH Keys are very important. Without them you won’t be able to connect to your server. To start making your keys, create the folder in which your newly-generated key will live.


mkdir ~/.ssh



After making the folder, change its permissions.


chmod 700 ~/.ssh


Finally, generate your key. Keep in mind that SSH is only as safe as the password you set up. If you enter a weak password, one that can be guessed easily, you’ll be open to attacks. Instead, try to generate a secure, memorable password.





ssh-keygen -t rsa


Connecting Over LAN


Want to connect over LAN? It’s easy! Just open a terminal window and figure out the IP address of the machine with the SSH server running. This can be done simply by running the ifconfig command.

Once you’ve determined the IP address of the machine, you’ll be able to log in. Just go back to the machine you’re trying to log in with and enter this command:

ssh username@ip.address.here


Note: change the “username” to the user name of the SSH server.



From there you’ll be prompted to enter your password. Do so. Soon after you’ll be logged in over LAN via SSH.

Along with connecting over LAN, connecting via SSH over the Internet is possible too. It just requires a bit of network savvy to set up. If you want to do this, you’ll need to set up port forwarding for your router.
Since every router out there is vastly different, you’ll need to do your own research on this topic. Just make sure you forward port 22 to the Internet from whatever machine is set up to use the SSH server, and you’ll be set.

After setting up port forwarding, you’ll need to determine the IP address of the machine you’re trying to SSH to. The easiest way to figure this out is to head over to ipchicken.com. They’ll tell you your Internet IP address.

Once you’ve gotten your Internet IP address, you’ll be able to log in. Just go to your terminal and enter the following:

ssh username@ip.address.here


From there you’ll be prompted to enter the SSH password. Enter it, and soon after you’ll be logged into your machine over the Internet via SSH.

Congratulations, you have completed setting up SSH in Ubuntu; you can now proceed to secure your SSH server.



How to Set Up and Enable SSH on Ubuntu

How to Set Up and Enable SSH on Ubuntu
sudo apt-get update

Setting Up SSH

To get started, we have to install the SSH server. You can find and install the openssh-server package in Software Center. Alternatively, open a terminal and type the following command:
sudo apt-get install openssh-server

Enable SSH in Ubuntu

Once OpenSSH server has been installed on your machine, you’ll need to make a copy of the default SSH configuration and rename it as factory default. This is so if you mess up your configuration tweaks, you’ll be able to restore the backup.
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

After the backup has been made, you’ll need to modify its permissions.
sudo chmod a-w /etc/ssh/sshd_config.factory-defaults
After the backup is taken care of, you’ll be able to configure your SSH file.
Note: only tweak this configuration file if you know what you are doing. Beginner users shouldn’t need to tweak anything for SSH to work.
sudo gedit /etc/ssh/sshd_config
When all of the changes have been made, you’ll need to restart the SSH service. If you’re using Ubuntu 14.04, you’ll need to rely on Upstart to reboot the SSH server.
sudo restart ssh
If you’re on Ubuntu 15.04+, use this systemd command to restart SSH instead.
sudo systemctl restart ssh