How to create own Packagist mirror site on Ubuntu 18.04

peter279k

peter279k

Posted on June 24, 2020

How to create own Packagist mirror site on Ubuntu 18.04

Introduction

If you're a modern PHP developer, you may heard about the packagist.org and composer.

The Packagist is a PHP package repositories and it stores many composer-based packages.

What about Composer? It's a package manager for PHP and it can help PHP developers to be easy to manage all kinds of PHP packages via Composer.

And its functionalities are very similar with NPM (a Node.js package manager).

If you use a lot for installing PHP packages via Composer, you may know about this issue.

This issue is about the main Packagist of Asia mirror has the bandwidth problem. (For now, it has been fixed.)

And it causes many PHP developers cannot install some PHP packages via composer install, composer require and composer update at that moment.

I also notice this issue and try to find another mirror to replace.

However, most of mirror locations are listed on official Packagist Org and this are far away from me.

And sometimes these locations cannot save my time because it takes slow time during composer package installation.

To resolve this issue, I want to setup my own Packagist mirror sites in my country.

And I also use packagist-mirror to accomplish my above issue because this mirror setup is recommended by packagist.org.

In this tutorial, I will show you how to use my customized Bash script to setup our own Packagist mirror site on Ubuntu 18.04 hosting system.

Perquisites

Here are some required stuffs before we get started with setting up our amazing Packagist mirror site:

  • One VPS (virtual private server) with at least one core CPU, 2GB RAM and a clean Ubuntu 18.04 operating system.
  • A registered domain, it's nice to let developers be easy to know mirror site and use it.
  • wget command is available now on your VPS.
  • It's nice to have a .env file on current home directory.
  • It's nice to have the nginx-default and nginx.conf files are located on current user home directory.

Installation

If you've looked at the packagist-mirror repository, you may find it has many kinds of ways to install the Packagist mirror site.

Including Docker container building, hosting setup and so on.

In this tutorial, I will use the hosting way to install the Packagist mirror site and I also provide a automated Bash script to help developers to setup Packagist mirror happily :).

Step 1: Download a packagist_mirror_installer.sh script

It's easy to use following command to download a packagist_mirror_installer.sh:

wget https://gist.githubusercontent.com/peter279k/cab67ba6207ec291747cc3cc6071a519/raw/46495d8dad37e769f4065f46925ff7b8d22b1e7e/packagist_mirror_installer.sh
Enter fullscreen mode Exit fullscreen mode

After script downloading has been completed, it can change this Bash script permission into 755.

That is, owner can read, write and execute this script, and group owner or other users can read and execute the script.

Step 2: Run this packagist_mirror_installer.sh script

After setting above permission is successful, executing following command to start Packagist mirror site building:

./packagist_mirror_installer.sh
Enter fullscreen mode Exit fullscreen mode

And it will present following captured picture:

update_upgrade

It's doing for update and installing required packages.

By default, the system locale will setup for en_US.UTF-8.

If developers want to change locale, it will set specific locale manually.

When installing required packages has been done, it will present following pictures to let developers select current timezone:

timezone_area

timezone_country

After selecting timezone has been done, it will present following picture to tell developers setting a host name for this mirror site:

ask_set_host_name

If developers don't set any host name and type enter, script will try to use hostname to get current host name on this hosting operating system. And ask you to ensure set this host/domain name.

If the host name is configured correctly, developers can type Y or y to continue the next step.

Copy nginx.conf and nginx sites default files

After setting the host name is successful, the script will try to find the nginx.conf and nginx-default files on current user home directory.

If it doesn't find any Nginx configuration files, it will ask you to skip this copy work and do next step.

If developers type Y or y, it will skip above steps. Otherwise terminate this script with exit code 1.

nginx_conf_ask

After doing above steps about copying Nginx configuration files, it will present following picture to tell whether developers to delete all files/folders on /var/www/html/* directory.

If typing Y or y, it should delete all of the things on /var/www/html directory.

ask_var_www_html

After manipulating the /var/www/html folder, it will ask developers whether updating Packagist repository:

packagist_repo

If developers type Y or y, it will use git pull origin master command to update Packagist repository right now. Otherwise skipping this step.

After updating Packagist repositories step, it will present following picture:

composer_nginx_start

It will do following steps for above captured picture:

  • Download Composer.
  • Using composer install to install some packages for packgist-mirror repository.
  • Asking developers whether executing the composer update -n to update dependencies for above repository.

After doing some steps, it will present following picture to tell developers whether syncing Packagist mirror:

sync_mirror

If developers type Y or y, it will use php bin/mirror create -vvv command to update Packagist repository right now. Otherwise skipping this step.

After syncing or skip Packagist mirror syncing step, it will present following picture:

install_certbot

It does some steps:

  • Update package mirror for Ubuntu operating system and install required packages for cert-bot. This can help developers to install a Let's Encrypt SSL certificate to secure mirror website easily.
  • Asking developers to determine setup SSL certificate with cert-bot:

ask_certbot_setting

If developers type Y or y, it will do cert-bot command interactively.

And help developers to setup and configure a SSL certificate quickly.

Editing DotEnv file

Editing .env file is very simple, and developers can scratch this file from .env.example file.

And it's very clear for every developer to know each DotEnv values have the comment and let us know setting functionality easily.

Customizing index page

If developers want to customize index page, it's easy to edit the index.html.php file.

Summary

After doing above Bash script correctly, it will have this page.

Enjoy it and have your own Packagist mirror right now :)!

💖 💪 🙅 🚩
peter279k
peter279k

Posted on June 24, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related