LAMP in Linux Mint 20.3

You want to develop a Web site on your local computer. It is easy using Linux Mint as the operating system. Here is the best approach.

I exclude some of the LAMP all in one approaches. XAMPP is supposed to be the easiest way to install Apache, MySQL, and PHP but there are endless painful stories about XAMPP because it breaks easily and is so non standard that it is too difficult to change. There is a LAMP package for Linux with a configuration closer to standard configuration but the default standard setup has some pain points you can avoid with a few extra steps during installation.

My examples use Nginx instead of Apache but you can use either. I switched to Nginx a few years ago when Apache screwed up one option. Translating the first ten Apache .htacess configuration files to Nginx was a deep learning experience. After that, I found Nginx easier.

Who uses local Web servers?

Web developers are tired of all the painful problems with online development. You have to choose slow or expensive hosting then put up with all the dropouts. There is no broadband access at the beach after your swim. Backup is either slow or painful or expensive and often has more than one of those problems.

You have a notebook, coffee and a place to sit under a shady tree. That notebook can be your whole Web development data centre.

What do you need for local Web development?

Any notebook with a 15 inch screen and an SSD for storage is sufficiently big, fast, and reliable enough. If you have a rotating rust magnetic disk, it will break when you bump it. Upgrade to SSD. Anything less that 15 inch is too small for side by side comparisons. Have a plug in llarger screen for editing photographs.

Linux and Windows both run the same software for development. Linux is easier to configure. Linux is easier when you copy your Web site up to any server because all the Web servers run Linux.

Linux Mint is the best operating system for notebooks and desktops. A Raspberry Pi model 4 can be used with the Raspberry Pi OS and almost exactly the same setup.

The Web server will be Apache or Nginx. I prefer Nginx. Most example configurations are for Apache, the best choice for beginners. After the initial setup and configuration, you will not see a difference outside of replacing Apache .htacess files with an Nginx equivalent (which is usually smaller because Nginx has better defaults).

When do you use local development on your notebook?

90% of development is in the alpha stage when you can run everything locally. You then move to the beta stage where you show a few people the Web site on your notebook. Part way through the beta stage, you will want to show many people and you will copy the local test to a hosted Web site. You can continue to develop locally and just copy the locally tested changes to the hosted site for a wider range of reviews.

Where do you put everything on your notebook?

The default for Web development is to put everything in the system directory and fight with access problems. I put all the configuration files, code, and Web sites in my user directory where everything can be combined into easy projects and regular backups.

The Apache/Nginx software and a simple configuration file go in the system directory. You set up a web directory in your home directory instead of using the difficult default Web directory inside the system directory.

Choose MariaDB, not MySQL. The MariaDB software and databases go in the system directory. Moving the database to your Web directory is difficult. It is easier to leave the database in the default sport and regularly export the data from the databases into your local web directory for inclusion in your project backups.

The PHP software and a simple configuration file go in the system directory. The simple configuration file includes a detailed configuration file from your web directory.

PHPMyAdmin is the easiest database administration software. Linux Mint and other operating systems can install a version locked up in the system directory. I find it easier to just configure a local Web site named phpmyadmin.local and install PHPMyAdmin as the only software in the Web site.

You will probably use Git for code management on some projects. Git can be configured to work inside your local projects. Your code editors do not care where the code lives.

Why use a Web directory in your home directory?

Backing up a default Web site project is a painful mess of file copies to bring everything into one project. Moving the code and Web site out of the default location into your home directory and into your development project directory means everything is in the one place and in the one backup. All you need is a regular export of the database data to the right project directory.

I currently have 79 local test Web sites in project directories. Project Cel is in directory /home/me/web/cel. Cel contains subdirectory access-logs with the Apache/Nginx access logs for the Cel Web site. Subdirectory public_html contains the Web site. File apache.conf/nginx.conf contains the Apache/Nginx configuration file for the Cel Web site. There are files for code, for documentation, for test data and from screen shots I use with A-B testing.

Way to get the Web site out of the system directory?

After you install Nginx/Apache, MariaDB, and PHP through the Linux Mint Software Manager, change the configuratiin as shown here, translating the home directory to yours and the project name to yours.

In your home directory, create directory web.

PHP

Linux Mint 20.3 installs PHP 7.4. You can use a PPA to install PHP 8. The changes are similar all the way from PHP 7.0 up.

The PHP configuration is in directory /etc/php. The PHP 7.4 config is in /etc/php/7.4. Nginx uses PHP through FPM so open directory /etc/php/7.4/fpm.

The fpm directory contains file php.ini that you will probably change and file php-fmp.conf that we leave alone. You can browse the directory using File Manager and open any of the files in a text editor to see what they contain. You will not be able to change them without using the sudo command. php.ini is where you can increase the maximum file upload size and similar settings.

Directory 7.4/conf.d contains files updated when you install new PHP modules. You rarely need to change anything in conf.d.

Directory 7.4/pool.d contains file www.conf. You can open the file in sudo xed then save the file as me.conf. While editing me.conf, change www to me. You will change the following lines.

[www]
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data

Change to:

[me]
user = me-data
group = me-data
listen.owner = me-data
listen.group = me-data

After any changes to the PHP configuration, restart the PHP server with the following command.

sudo service php7.4-fpm restart

Nginx/Apache

Apache lets you choose two ways to run PHP. Nginx works fine with one standard way. My example here is the Nginx way as I no longer have the Apache example.

In directory web, create an empty file named nginx.conf.

Nginx is configured in file /etc/nginx/nginx.conf. Edit the file using something like the command sudo xed /etc/nginx/nginx.conf. Change the following section.

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Change the section to the following with me replaced by your user name.

include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
include /home/me/web/nginx.conf;

You can set up an example project. I will use Cel. Create directory cel in directory web.

In directory cel, create file nginx.config plus subdirectories named access-logs and public_html.

Place the following lines in file cel/nginx.conf.

# Included by /home/me/web/nginx.conf;
# sudo service php7.4-fpm restart
# sudo service nginx restart
server {
 listen       80;
 server_name  cel.local;
 access_log  /home/me/web/cel/access-logs/access.log;
 error_log /home/me/web/cel/access-logs/error.log;
 root /home/me/web/cel/public_html;
 # Choose No redirect or Redirect.
 # No redirect.
 index  index.php;
 # Redirect unknown requests to the index.php file.
 #try_files $uri $uri/ /index.php?$args;
 # Include PHP 7 config.
 include /home/me/web/nginx.php7.conf;
}

Now to create file web/nginx.php7.conf. You only need this once for most PHP based projects.

# Included by /home/me/web/*/nginx.conf for PHP 7 sites.
# This is included within server { }.
# Note the fastcgi_pass path has to match listen in /etc/php/7.4/fpm/pool.d/me.conf.
# location could be ~* \.php$ to ignore the file name case.
# Some software uses case.
    location ~ \.php$ {
        try_files $uri /index.php =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.me.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_read_timeout 300;
    }

In web/nginx.config, include the following line changed for your project.

include /home/me/web/cel/nginx.conf;

You can restart nginx at any time to reread the configuration changes using the following command.

sudo service nginx restart

Host name

I call my Web sites by the project name. Cel becomes cel.local as defined in the cel/nginx.conf file. The OS needs to know the name. In Linux, you edit /etc/hosts, using sudo xed /etc/hosts, to add the following line. The line can stay there forever unused after the project is finished. You do not have to restart anything to use the new entry as the file is read for every request.

127.0.1.1 cel.local

In your Web browser, you access the Web site by entering the URL cel.local. The Web browser will pass the entry to the OS and the OS will pass the request to port 80. Remember port 80 from the Apache/Nginx configuration. There is no need to switch from HTTP to HTTPS as the .local domains are not visible outside your computer.

PHP 8 or later

There is a PPA for installing later versions of PHP. The later versions will set up new subdirectories in /etc/php. You can make similar changes replacing 7.4 with 8.0 or whatever. You can also install other versions and run them in parallel selecting a different version for each Web site.

Worth the effort?

The faster development is worth the effort. No waiting for the Internet to resend. No slowdowns when people in your area increase their Internet use. Your SSD is far faster than the overloaded disk in the hosting server. You can set up as many test servers as you want without hauling out the credit card for more hosting. The initial configuration is a couple of hours work and testing. After that initial investment, you are ahead every day.

Your backup of the project directory, cel in this example, is a complete backup of all code, configuration, documentation, HTML, test data, and everything else. The only manual bit is exporting the database data to your directory for inclusion in the backup and the export can be automated.

You can archive and remove a project. The only change is removing, or commenting out, the include in web/nginx.conf then restarting nginx or a reboot. The entry in hosts can stay there unused or commented out.

Whatever you do on a computer, replacing the system magnetic disk with a good SSD is the best upgrade you can make.

Memory is not important for Web development. You will need lots of memory for editing photographs and the Web process will use only a fraction of that memory. Chrome and similar Web browsers use more memory than a Web server.

Processor speed is not important unless you use one of those monster Javascript libraries. If you do use a Javascript gorilla, all your Web site visitors will suffer the same slowdown.

Screen size and quality is more important when editing photographs. A good keyboard is always a good idea.

Backup. Backup. Backup backups. I recommend Backintime for Linux when you have everything in your home directory. I recommend at least three separate disks with them stored in different locations to allow for accidents.