Skip to content

Instantly share code, notes, and snippets.

@githubgobi
Last active January 25, 2019 10:54
Show Gist options
  • Save githubgobi/4dfe10ff703894b07ca3bcc05f2ffa09 to your computer and use it in GitHub Desktop.
Save githubgobi/4dfe10ff703894b07ca3bcc05f2ffa09 to your computer and use it in GitHub Desktop.
Ubuntu Webserver Setup

Ubuntu Webserver Setup

This tutorial shows you how to install a LAMP stack under Ubuntu 18.04 LTS (Bionic Beaver).

Contents

Requirements

Install Apache, PHP, MySQL

All required components can be installed from the official package sources. Here you can install other server components besides Apache, MySQL and PHP.

Expert info: During the installation, a password for the database administrator root is always requested. Please choose this password carefully and keep it safe. And please do not confuse the database administrator with the system administrator account root. They are two completely different users, even if the name is identical.

sudo apt-get update
sudo apt-get install vim -y
sudo apt-get install apache2 -y
sudo apt-get install mysql-server mysql-client libmysqlclient-dev -y
sudo apt-get install libapache2-mod-php7.2 php7.2 php7.2-mysql php7.2-sqlite -y
sudo apt-get install php7.2-mbstring php7.2-curl php7.2-intl php7.2-gd php7.2-zip php7.2-bz2 -y
sudo apt-get install php7.2-dom php7.2-xml php7.2-soap -y

Test Apache

For a first test with a web browser, visit http://localhost or http://127.0.0.1

The following should be displayed:

It works!

This is the default web page for this server.

The web server software is running but no content has been added, yet.

If this page is not displayed, Apache probably hasn't started yet. In this case, start the web server with the following command:

sudo service apache2 start 

Create a PHP info page

cd /var/www/html/
sudo echo "<?php phpinfo();" > phpinfo.php

Open: http://localhost/phpinfo.php

Enable apache mod_rewrite

To enable the rewrite modul, run:

sudo a2enmod rewrite
sudo a2enmod actions

If you plan on using mod_rewrite in .htaccess files, you also need to enable the use of .htaccess files by changing AllowOverride None to AllowOverride All.

sudo vim /etc/apache2/apache2.conf 
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Finally, the configuration of Apache has to be reloaded.

sudo service apache2 restart

Optional: To check if mod_rewrite is installed correctly, download an run this script:

Webserver and PHP configuration tester

cd /var/www/html/
sudo php -r "copy('https://gist.githubusercontent.com/odan/64606e3668eac0e13afc/raw/d0fcc8efadaab6c197f3c63f09e4232cb599c2b2/webserver-test.php', 'webserver-test.php');"
sudo php webserver-test.php

Set permissions

Make /var/www/ writable without sudo privileges.

Set owner for /var/www/ to www-data (recursive)

sudo chown -R www-data /var/www/

The next command adds a attribute (recursive) which will keep new files and directories within /var/www/ having the same group permissions.

sudo chmod -R g+s /var/www/

Test the result

ls -l

Note: On a production server it's recommended to chown www-data to /var/www/another_folder/, not to /var/www/.

Change mysql root password

Start mysql

sudo service mysql start

Open debian.cnf and copy the password of the user debian-sys-maint to the clipboard.

sudo vim /etc/mysql/debian.cnf

Login with this command and paste the password from the clipboard.

sudo mysql -u debian-sys-maint -p 

Change the password:

mysql> update mysql.user set authentication_string=password('MyNewPass'), plugin='mysql_native_password' where user='root';
mysql> flush privileges;
mysql> quit; 

Note On a production server you should now run sudo mysql_secure_installation to improve the security of your MySQL installation.

Read more:

Install composer

cd /var/www/
sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo php -r "unlink('composer-setup.php');"
sudo composer

Install adminer

cd /var/www/html/
sudo php -r "copy('https://github.com/vrana/adminer/releases/download/v4.6.2/adminer-4.6.2.php', 'adminer.php');"

Open: http://localhost/adminer.php

Setup a SFTP server

sudo apt-get install openssh-server

Read more: How to setup a restricted SFTP server on Ubuntu?

Server Configuration - Modification

$ cd /etc/apache2
$ cd /sites-available
$ ls
$ sudo nano 000-default.conf

editor opend

ServerName testserver.net
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/foldername/public - for laravel (public)
ServerAlias www.domain.com

<Directory "/var/www/html/fodername/public/"> - for laravel (public)
Options Indexes FollowSymLinks
Options -ExecCGI
AllowOverride all
Require all granted
</Directory>
<VirtualHost 192.168.3.100>
  ServerName www.example.com
  DocumentRoot /srv/www/www.example.com/htdocs
  ServerAdmin webmaster@example.com
  ErrorLog /var/log/apache2/www.example.com_log
  CustomLog /var/log/apache2/www.example.com-access_log common
  <Directory "/srv/www/www.example.com/htdocs">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment