Set Up Development Environment in Ubuntu 22.04 – LAMP and More

Set Up Development Environment in Ubuntu 22.04 – LAMP and More

Are you an aspiring developer looking to set up your development environment in Ubuntu 22.04? Look no further! In this guide, we will walk you through the process of setting up a powerful development environment using the LAMP (Linux, Apache, MySQL, PHP) stack. We will also cover additional tools like Xdebug, Fail2ban, SSL, and firewall to ensure the security and smooth functioning of your development environment. So let’s get started!

Prerequisites

Before we begin, make sure you have a fresh installation of Ubuntu 22.04. Once you have that, open up a terminal and let’s get started with the setup process.

Step 1: Update and Upgrade

The first step is to update and upgrade your system. Run the following commands in your terminal:

sudo apt update
sudo apt dist-upgrade

These commands will update your package lists and upgrade any existing packages to their latest versions. This ensures that your system is up to date and ready for the installation of other components.

Step 2: Install Essential Tools

Next, we will install some essential tools that will come in handy during the setup process. Run the following command:

sudo apt install sudo curl htop mc net-tools tree

This command will install sudo (if it’s not already installed), curlhtopmc, and net-tools. These tools will make it easier for you to manage your system and perform various tasks.

Step 3: Add User to Sudoers

By default, the sudo command is only available to the root user. To add your user to the sudoers list and allow them to run commands with root privileges, run the following command:

sudo usermod -aG sudo YOUR_USER

Replace YOUR_USER with your actual username. This command will add your user to the sudo group, allowing them to execute commands as a superuser.

Step 4: Install Apache2

Now, let’s install the Apache web server. Apache is a widely used web server that will serve as the backbone of our development environment. Run the following command:

sudo apt install apache2 apache2-utils

This command will install Apache and its utilities. Once the installation is complete, Apache will be up and running on your system.

Step 5: Activate Apache2 Modules

To enhance the functionality of Apache, we need to activate some essential modules. Run the following command to activate the required modules:

sudo a2enmod rewrite ssl headers http2 vhost_alias

These modules will enable features like URL rewriting, SSL support, HTTP/2 protocol, and virtual host aliasing. Enabling these modules will enhance the performance and security of your web server.

Step 6: Configure Apache2 SSL

To secure your web server and enable SSL encryption, we need to generate a self-signed SSL certificate. Run the following commands to generate the certificate:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt

These commands will create a self-signed SSL certificate and place it in the appropriate directories. Note that this is a self-signed certificate and should only be used for development purposes. For production environments, consider using a trusted SSL certificate.

Step 7: Update Apache2 Configuration

To optimize the Apache configuration for your development environment, we need to make some changes to the apache2.conf file. Run the following command to open the file in a text editor:

sudo nano /etc/apache2/apache2.conf

Once the file is open, locate the <Directory /var/www/> section and modify it as follows:

<Directory /var/www/>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

These changes will disable directory listing, allow the use of .htaccess files for directory-specific configurations, and grant access to all users. Save the file and exit the text editor.

Step 8: Install MariaDB

Next, we will install MariaDB, a fork of MySQL, which is a popular relational database management system. Run the following command to install MariaDB:

sudo apt install mariadb-server mariadb-client

This command will install MariaDB and its client component. Once the installation is complete, you will be prompted to set the root password and perform some additional security configurations. Follow the on-screen instructions to secure your MariaDB installation.

Step 9: Create a Database User

After installing MariaDB, it’s recommended to create a dedicated database user for your applications. To create a database user with root access, run the following commands:

sudo mysql -u root -p

Enter your root password when prompted. Once you are in the MariaDB console, execute the following commands to create a user and grant them full privileges:

CREATE USER 'user'@'%' IDENTIFIED BY 'userpass';
GRANT ALL ON *.* TO 'user'@'%' WITH GRANT OPTION;

Replace 'user' and 'userpass' with your desired username and password. These commands will create a user with the specified credentials and grant them all privileges on all databases. This user can be used to authenticate your applications with the database.

Step 10: Add Repo for PHP

To install the latest version of PHP, we need to add a repository that provides the latest PHP packages. Run the following command to add the repository:

sudo add-apt-repository ppa:ondrej/php

This command will add the repository maintained by Ondřej Surý, which provides updated and custom versions of PHP for Ubuntu.

Step 11: Install PHP

Now, let’s install PHP along with some commonly used modules. Run the following command to install PHP and the required modules:

sudo apt install php8.1 php8.1-{fpm,mysql,intl,apcu,zip,dev,curl,cli,imagick,mbstring,gd,bcmath,redis,memcached,xml}

This command will install PHP 8.1 and its corresponding modules. If you prefer a different version of PHP, you can modify the version number accordingly.

Step 12: Enable PHP-FPM for Apache2

To enable PHP-FPM (FastCGI Process Manager) for Apache2, run the following commands:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm

These commands will enable the necessary modules and configurations to use PHP-FPM with Apache2. PHP-FPM improves the performance and scalability of PHP by utilizing separate worker processes.

Step 13: Install Xdebug

Xdebug is a powerful debugging and profiling tool for PHP. To install Xdebug, run the following command:

sudo apt install php8.1-xdebug

This command will install Xdebug for PHP 8.1. If you are using a different version of PHP, adjust the package name accordingly.

Step 14: Configure Xdebug

To configure Xdebug, we need to create a configuration file. Run the following command to create the file:

sudo nano 80-tuning.ini

Paste the following code into the file:

; Maximum amount of memory a script may consume
memory_limit = 1024M

; Maximum execution time of each script, in seconds
max_execution_time = 60

; Maximum size of POST data that PHP will accept.
post_max_size = 1024M

; Maximum allowed size for uploaded files.
upload_max_filesize = 1024M

; Xdebug settings
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9000

; Xdebug OLD settings
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.log_level=0

Save the file and exit the text editor.

Step 15: Create Xdebug Configuration Script

To automate the process of creating symbolic links for the Xdebug configuration file, we will create a shell script. Run the following command to create the script file:

sudo nano create-symlink-80-tuning.sh

Paste the following code into the file:

#!/bin/bash

if [ -f "80-tuning.ini" ]; then
    echo "Copying PHP tuning to /etc/php/80-tuning.ini"
    cp 80-tuning.ini /etc/php/80-tuning.ini
else
    echo "80-tuning.ini - File not found!"
    return 1
fi

echo "Creating symlinks to /etc/php/80-tuning.ini"
ln -s /etc/php/80-tuning.ini /etc/php/7.1/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/7.1/cli/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/7.3/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/7.3/cli/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/7.4/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/7.4/cli/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.0/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.0/cli/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.1/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.1/cli/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.2/fpm/conf.d/80-tuning.ini
ln -s /etc/php/80-tuning.ini /etc/php/8.2/cli/conf.d/80-tuning.ini

echo "Restarting PHP-FPM..."
systemctl restart php7.1-fpm
systemctl restart php7.3-fpm
systemctl restart php7.4-fpm
systemctl restart php8.0-fpm
systemctl restart php8.1-fpm
systemctl restart php8.2-fpm

echo "Done!"

Save the file and exit the text editor. Then, make the script executable by running the following command:

sudo chmod +x create-symlink-80-tuning.sh

Finally, execute the script with the following command:

sudo ./create-symlink-80-tuning.sh

This script will copy the Xdebug configuration to the appropriate directory and create symbolic links for various PHP versions. It will also restart the corresponding PHP-FPM services.

Step 16: Install Composer (Optional)

If you are working with PHP projects that require external dependencies, it’s highly recommended to install Composer. Composer is a dependency management tool for PHP that allows you to easily install and update packages. To install Composer, follow the official documentation at https://getcomposer.org/download/.

Step 17: Configure Security Settings

To enhance the security of your development environment, we will configure the firewall and enable fail2ban. Run the following commands to install and configure these security tools:

sudo apt install ufw
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw allow mysql
sudo ufw enable
sudo apt install fail2ban
sudo nano /etc/fail2ban/jail.local

In the jail.local file, add the following lines to enable fail2ban for MySQL authentication:

[mysqld-auth]
enabled = true

Save the file and exit the text editor. These settings will enable the firewall, allow SSH, HTTP, HTTPS, and MySQL traffic, and enable fail2ban for MySQL authentication.

Step 18: Configure Virtual Hosts and SSL

To organize and manage multiple websites on your development environment, it’s recommended to set up virtual hosts. Virtual hosts allow you to host multiple websites on a single Apache server by assigning different domain names or subdomains to each site. We will also configure SSL to secure the websites.

To configure virtual hosts and SSL, follow these steps:

  1. Create a new configuration file for your virtual host. Replace websiteName with your desired domain name or subdomain. Run the following command to create the configuration file:sudo nano /etc/apache2/sites-available/websiteName.conf
  2. Paste the following configuration into the file:
   <VirtualHost *:80>
       DocumentRoot "/var/www/example.com"
       ServerName example.com
       <Directory "/var/www/example.com">
           Options -Indexes +FollowSymLinks +MultiViews
           Require all granted
           AllowOverride All
       </Directory>
       <FilesMatch ".+\.ph(ar|p|tml)$">
           SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
       </FilesMatch>
   </VirtualHost>

   <VirtualHost *:443>
       DocumentRoot "/var/www/example.com"
       ServerName example.com
       SSLEngine on
       SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
       SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

       <Directory "/var/www/example.com">
           Options -Indexes +FollowSymLinks +MultiViews
           Require all granted
           AllowOverride All
       </Directory>
       <FilesMatch ".+\.ph(ar|p|tml)$">
           SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
       </FilesMatch>
   </VirtualHost>

In this example, we assume that your website’s files are located at /var/www/example.com. Replace this path with the actual path to your website’s root directory.

The configuration includes both HTTP (<VirtualHost *:80>) and HTTPS (<VirtualHost *:443>) sections. For the SSL section, we assume you have already generated the SSL certificate and key files and placed them at /etc/ssl/certs/apache-selfsigned.crt and /etc/ssl/private/apache-selfsigned.key, respectively. Modify these paths if necessary.

  1. Save the file and exit the text editor.
  2. Enable the virtual host by creating a symbolic link:sudo a2ensite websiteName.conf
  3. Restart Apache to apply the changes:sudo systemctl restart apache2

Now you can access your website by navigating to http://skypebridge.localhost for HTTP and https://skypebridge.localhost for HTTPS.

Repeat these steps for each virtual host you want to set up, ensuring that each configuration file has a unique name and points to the appropriate document root directory and SSL certificate files.

With this configuration, your websites will be accessible over both HTTP and HTTPS secured connections.

Step 19: Install and Configure SSL Certificates

If you want to secure your websites with trusted SSL certificates, you can obtain them from a certificate authority (CA) or use Let’s Encrypt, a free and open certificate authority. Let’s Encrypt provides automated tools to obtain and renew SSL certificates. Follow the steps below to set up SSL certificates using Let’s Encrypt:

  1. Install the Let’s Encrypt client, Certbot:sudo apt install certbot python3-certbot-apache
  2. Obtain SSL certificates for your virtual hosts (replace example.com with your domain/subdomain):sudo certbot --apache -d example.com -d www.example.com
  3. Follow the prompts to provide the necessary information and agree to the Terms of Service.
  4. Certbot will automatically configure Apache with the SSL certificates and enable HTTPS for your virtual hosts.
  5. Certbot will also set up a monthly cron job to renew the SSL certificates automatically.

With SSL certificates installed, your websites will be accessible over secure HTTPS connections.

Step 20: Set Up Remote MySQL Access (Optional)

If you want to access your MySQL/MariaDB database remotely, for example, from a database management tool on your local machine, you can configure your database server to allow remote access.

Follow these steps to set up remote MySQL access:

  1. Edit the MySQL/MariaDB configuration file:sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
  2. Locate the bind-address directive and comment it out or change its value to your server’s IP address:# bind-address = 127.0.0.1
  3. Save the file and exit the text editor.
  4. Restart MySQL/MariaDB to apply the changes:sudo systemctl restart mariadb

Now you will be able to connect to your remote MySQL/MariaDB server from your local machine using a database management tool like MySQL Workbench or phpMyAdmin.

Please note that enabling remote access to your database server can pose security risks. Make sure to secure your database server by choosing strong passwords and restricting access to trusted IP addresses.

Step 21: Enable PHP Error Logging

To debug PHP errors and warnings and troubleshoot issues in your application, it’s helpful to enable error logging in PHP.

To enable PHP error logging, follow these steps:

  1. Edit the PHP configuration file for the desired PHP version:sudo nano /etc/php/8.1/fpm/php.ini
  2. Find the error_reporting directive and set it to the desired error reporting level. For example, to log all errors, warnings, and notices, use the following value:error_reporting = E_ALL
  3. Find the display_errors directive and set it to Off:display_errors = Off
  4. Find the log_errors directive and set it to On:log_errors = On
  5. Specify the path to the log file using the error_log directive. For example:error_log = /var/log/php/php_error.log
  6. Save the file and exit the text editor.
  7. Restart PHP-FPM to apply the changes:sudo systemctl restart php8.1-fpm

PHP errors will now be logged to the specified file. You can then check the log file for any errors or warnings encountered during the execution of your PHP scripts.

Step 22: Set Up Git Version Control

Git is a popular version control system that allows you to track changes in your codebase and collaborate with others. To set up Git, follow these steps:

  1. Install Git:sudo apt install git
  2. Configure your Git username and email:git config --global user.name "Your Name" git config --global user.email "[email protected]"
  3. Generate an SSH key (if you don’t already have one) to securely authenticate with remote Git repositories:ssh-keygen -t rsa -b 4096 -C "[email protected]"
  4. Add your SSH key to the SSH agent:eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
  5. Copy the SSH key to your clipboard:cat ~/.ssh/id_rsa.pub
  6. Go to your Git hosting provider (e.g., GitHub, GitLab) and add the SSH key to your account settings.

Now you can use Git to track changes in your projects, collaborate with others, and push your code to remote repositories.

Step 23: Install Code Editors and Development Tools

Having a reliable code editor and development tools can greatly enhance your productivity and coding experience. Here are a few popular options:

To learn how to install Visual Studio Code on Ubuntu 22.04 using the terminal, you can check out our detailed blog post VS Code Installation Guide for Ubuntu via Terminal. This guide provides step-by-step instructions for installing Visual Studio Code and getting started with the editor.

Visual Studio Code offers a wide range of features and extensions that make it a powerful tool for coding in various programming languages. Whether you are a beginner or an experienced developer, having a solid code editor can significantly improve your development workflow.

So head over to our blog post and follow the instructions to install Visual Studio Code on your Ubuntu 22.04 system.

Step 24: Install and Configure Redis

Redis is an in-memory data structure store that can be used as a caching layer or as a messaging broker in your applications. To install Redis, follow these steps:

  1. Install Redis:sudo apt install redis-server
  2. Start the Redis service:sudo systemctl start redis-server
  3. Enable Redis to start on boot:sudo systemctl enable redis-server
  4. Configure Redis to allow remote connections (if needed). Open the Redis configuration file:sudo nano /etc/redis/redis.conf
  5. Find the bind directive and change its value to your server’s IP address or comment it out to allow connections from any IP:# bind 127.0.0.1
  6. Save the file and exit the text editor.
  7. Restart the Redis service to apply the changes:sudo systemctl restart redis-server

Redis is now installed and accessible on your system. You can use the Redis server for caching or as a messaging broker in your applications.

Conclusion

Congratulations! You have successfully set up your development environment in Ubuntu 22.04 using the LAMP stack. You now have a powerful web server (Apache), a reliable database management system (MariaDB), the latest version of PHP with essential modules, debugging capabilities with Xdebug, security measures with Fail2ban and a firewall, and optional tools like Composer and Node.js/npm. With this setup, you can start developing and testing your web applications with ease.

Remember to always keep your system and applications up to date and follow security best practices to ensure a safe and secure development environment. Happy coding!

Note: This guide assumes a basic understanding of Linux commands and concepts. If you encounter any issues during the setup process, refer to the official documentation of the respective tools or seek assistance from the developer community.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *