magento 2 and composer
{ "require": { "magento/community-edition": "*@dev" }, "repositories": [ { "packagist": false }, { "type": "vcs", "url": "https://github.com/magento/magento2.git" } ] }This is all we need. Now let’s install Magento with the composer install –no-dev command. This will download Magento 2 to the vendor directory. Now what? Magento 2 has a pub directory which contains files supposed to be exposed to the web. You can just copy them all to your web root but there is room for experimentation. I always try to add as little number of files to my project as necessary, so my initial idea was to symlink all of them except those which have to be modified. In the project root, which only contains the vendor directory, composer.json and composer.lock files, the public directory is added and configured as a web root. At this point, it’s a matter of analyzing which files of Magento’s pub directory have to be copied and which can be symlinked. Here is the file listing:
errors/ media/ opt/ static/ cron.php get.php index.php static.phpCopying the media directory is obvious. At this stage, I decided to symlink the opt and errors directories as I was not yet planning to do any modifications there. At the moment of writing, the pub/index.php file was broken so I had to copy it to my public directory and fix the issues. The remaining .php files, which are cron.php, get.php and static.php can be safely symlinked.
/path/to/my/project/public$ ln -s ../vendor/magento/community-edition/pub/opt/ opt /path/to/my/project/public$ ln -s ../vendor/magento/community-edition/pub/errors/ errors /path/to/my/project/public$ ln -s ../vendor/magento/community-edition/pub/cron.php cron.php /path/to/my/project/public$ ln -s ../vendor/magento/community-edition/pub/get.php get.php /path/to/my/project/public$ ln -s ../vendor/magento/community-edition/pub/static.php static.phpBecause index.php has been moved, the bootstrap path inside it has to be modified from
require __DIR__ . '/../app/bootstrap.php';to
require __DIR__ . '/../vendor/magento/community-edition/app/bootstrap.php';The only remaining directory in pub is called static. This directory only contains a .htaccess file which redirects all static files requests to static.php. Theoretically, this directory can be symlinked too, but as long as my server runs nginx, the .htaccess file has no purpose. Ignoring this directory, the following rewrite-rule was added to the nginx configuration file of my host:
location /static { rewrite ^/static/(.*)$ /static.php?resource=$1? last; }The complete configuration file looks like this:
server { listen 80; server_name hostname.tld; root /path/to/my/project/public; index index.php; location /static { rewrite ^/static/(.*)$ /static.php?resource=$1? last; } location ~ .php/ { rewrite ^(.*.php)/ $1 last; } location ~ \.php$ { try_files $uri =404; expires off; fastcgi_read_timeout 900s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; ## Magento 2 Developer mode fastcgi_param MAGE_MODE "developer"; } }Note that there is also a .htaccess file in the pub directory. The rule-sets in it are optional and transferring them to nginx configuration is outside the scope of this tutorial. Basically everything will work without them. Finally, the contents of the .gitignore file are compiled which is of course a matter of preferences; there are some holy wars whether to include the vendor directory and/or composer.lock into the repository. Here is the list of files I choose to ignore:
/vendor/ /public/cron.php /public/get.php /public/static.php /public/errors /public/media/ /public/opt
Your greatest possible competitive advantage can be your clients and the interactions they have with… Read More
Digital marketing KPIs are measurable values that marketing teams use to track progress toward desired… Read More
In today's digital age, fraud poses a significant threat to businesses of all sizes. As… Read More
Financial crimes continue to evolve and proliferate in our increasingly digital, global economy. From complex… Read More
In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More
In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More
View Comments
You could also add additional scripts into your composer to automate all the tasks you need to do after composer download and install Magento 2. For example you could add the followings:nn
n{n "scripts": {n "post-install-cmd": [n "DiglinMagento2Composer::postInstall"n ]n }n}n
nnIn your PSR-4 PHP class file /Diglin/Magento2/Composer.phpnnnnamespace DiglinMagento2;nuse ComposerScriptEvent;nnclass Composern{n public static function postInstall(Event )n { n // Do your stuffn }n}n
I am wondering if other .htaccess files in the project need to be taken into consideration when running on nginx.
As I have written the `.htaccess` from `pub/static` directory has to be transformed into nginx rules. The only other one from `pub` directory is located in its root and I already mentioned that the directives inside it are more or less tweaks.nnnThe rest of .htaccess files outside of `pub` dir to my understanding make no sense as they lie outside of web-server scope. And this is one of main advantages of using composer.
Yes, I think you are right. So configuring Magento 2 on nginx requires only one .htacess to be "transformed". Thanks.
Could you do an update of this post ? The composer install seems to be broken. I get an error when running composer install : "The requested package magento/community-edition could not be found in any version, there may be a typo in the package name."nTried to change package to project-community-edition and got "- Installation request for magento/project-community-edition *@dev -> satisfiable by magento/project-community-edition[dev-master].nnnnnnnnnn - magento/project-community-edition dev-master requires zendframework/zend-stdlib 2.0.3 -> no matching package found."nnnThanks in advance !
We are aware of the situation and will be working on it!
Thanks ! Looking forward for the update. Being the top hit for "magento 2 composer install", I think many others are too :)