Laravel API calls failing in Production - vuejs2

I'm currently working on a VueJS+Laravel 6 project.
Everything is working ok in Localhost, but in Production, although the web routes work well, the API calls fail.
I have no errors in the Apache log and Laravel log.
Am I missing anything that I should update related to Laravel passport?
I simly run php artisan passport:install in production.
This is what I install in the Ubuntu server
apt-get install zip unzip fail2ban python php7.4-common php7.4-cli php7.4-gd php7.4-mysql php7.4-curl php7.4-intl php7.4-mbstring php7.4-bcmath php7.4-imap php7.4-xml php7.4-zip apache2 libapache2-mod-php mysql-server ffmpeg cron npm nodejs
I tried not to install fail2ban, but it didn't help.
This is my virtualhost
<VirtualHost *:80 *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/myproject/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/myproject>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I set up the following rights:
chown -R www-data.www-data ./ && chmod -R 755 ./ && chmod -R 777 storage && chmod -R 777 bootstrap/cache/
And these are the artisan commands I run
php artisan key:generate && php artisan migrate:fresh --seed && php artisan storage:link && php artisan passport:install && php artisan config:cache

Well... I was apparently missing to run sudo a2enmod rewrite
Leaving it here for a case it helps someone else in the future

Related

How to run npm serve with https?

I just had a index.html in my working directory, and typed npx serve.
I can access http://localhost:3000 but I want to host https://localhost:3000.
How to run 'serve' with https mode?
What option do I need?

Elasticbeanstalk npm: command not found

My project was working since 2 years with no problem.
But this morning, It becomes to return error.
sudo: npm: command not found
Here's my .ebextensions file
I'm using Amazon Linux 2 with PHP.
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
# Point the app root to the public/ folder.
- namespace: aws:elasticbeanstalk:container:php:phpini
option_name: document_root
value: /p
ublic
# Set here your php.ini `memory_limit` value.
- namespace: aws:elasticbeanstalk:container:php:phpini
option_name: memory_limit
value: 256M
container_commands:
00_copy_env_file:
command: "mv /tmp/.env /var/app/staging/.env"
01_install_composer_dependencies:
command: "sudo php /usr/bin/composer.phar install --no-dev --no-interaction --prefer-dist --optimize-autoloader"
cwd: "/var/app/staging"
02_install_node_dependencies:
command: "sudo npm install"
cwd: "/var/app/staging"
03_build_node_assets:
command: "npm run dev"
cwd: "/var/app/staging"
04_link_storage_folder:
command: "php artisan storage:link"
cwd: "/var/app/staging"
05_run_migrations:
command: "php artisan migrate --force"
cwd: "/var/app/staging"
leader_only: true
06_run_migrations:
command: "php artisan key:generate"
cwd: "/var/app/staging"
07_run_migrations:
command: "php artisan passport:install"
cwd: "/var/app/staging"
What's change on elasticbeanstalk on AWS. Whats wrong with my configuration?
All my websites are down at the same time, this morning.
edit:
.platform/hooks/prebuild/install_latest_node_js.sh file is below.
#!/bin/sh
# Install Latest Node
# Some Laravel apps need Node & NPM for the frontend assets.
# This script installs the latest Node 12.x alongside
# with the paired NPM release.
sudo yum remove -y nodejs npm
sudo rm -fr /var/cache/yum/*
sudo yum clean all
curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install nodejs -y
# Uncomment this line and edit the Version of NPM
# you want to install instead of the default one.
# npm i -g npm#6.14.4
Try this command:
sudo apt-get install npm

npm run serve on vue does not serve to localhost

I am trying to get a grasp on Vue, because frameworks, frameworks, frameworks. I installed vue with npm using npm install -g #vue/cli which worked, then i made my project folder using vue create myproj, choosing all default options. When I tried to run with npm run serve this was my output:
App running at:
- Local: http://x86_64-conda_cos6-linux-gnu:8080/
- Network: http://x86_64-conda_cos6-linux-gnu:8080/
Which doesn't connect in browser. As far as I understand, it should have localhost or 127.0.0.1 followed by the port numbers. The port is not being used by anything else. I checked my hosts:
(base) alan#skynet:~/dev/myproj$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 skynet
I followed the steps in this similar question: https://forum.vuejs.org/t/npm-run-serve-is-not-defaulting-to-localhost/88007/13 and added a vue.config.js with the following:
module.exports = {
devServer: {
host: '127.0.0.1',
port: 8080,
public: 'localhost:8080',
}
}
but it only changed my network. I have not changed my package.json or any of the default files that come with vue create. I even deleted the directory, uninstalled and reinstalled vue in the same location and that did not resolve. This other SO thread Not running at local when "npm run serve" command in vue project was also similar, but I do not have a .bash_profile and I'm not sure if I can just throw in HOST="localhost" anywhere into my .profile file, as I'm relatively new to Linux and don't want to break things. I'm just trying to get the initial setup off the ground so I can move on to actually doing vue stuff.
I'm not sure if what I did worked, but it now works. See https://github.com/vuejs/vue-cli/issues/4081#issuecomment-694904714

Vagrant VirtualBox Local Dev Env, Ubuntu, Yarn, Vue CLI 3 - Issues

I am trying to setup a local development environment consisting of:
Ubuntu server vagrant box
My existing vuejs project created using Vue CLI 3 and passed to vagrant via synced_folder
Then run yarn run serve and access this on my host using port forwarding on the vagrant box.
Background:
I have developed a vue CLI 3 project on my Ubuntu 16.04 laptop which is working well, however, I want to move this inside of a vagrant box to keep my local machine tidy. I currently use yarn run serve which works well. I want to be able to run this command inside a new vagrant development environment.
Summary of Problems/Issues:
the vue command is not found after installing its dependencies
permission issues spat out by yarn when attempting to run yarn run serve inside vagrant box
there is an fsevents#1.2.4 message when yarn global add #vue/cli
Provisioning the local dev environment:
The Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "webserver_dev" do |webserver_dev|
webserver_dev.vm.box = "ubuntu/xenial64"
webserver_dev.vm.network "private_network", ip: "192.168.33.10"
webserver_dev.vm.network "forwarded_port", guest: 80, host: 8888
webserver_dev.vm.network "forwarded_port", guest: 8080, host: 8080
webserver_dev.vm.hostname = "develop.dev"
webserver_dev.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]
webserver_dev.ssh.forward_agent = true
webserver_dev.vm.provider "virtualbox" do |vb|
vb.memory = "1824"
vb.cpus = "2"
end
end
end
Provisioning of the vagrant box: ubuntu/xenial64 (virtualbox, 20180802.0.0):
sudo apt update && sudo apt upgrade
sudo apt install build-essential libssl-dev -y
# install node and npm:
cd ~
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
# install yarn
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
# Show installed versions
yarn -v (outputs 1.9.4)
node -v (outputs v10.9.0)
npm -v (outputs 6.2.0)
Problems/Issues Output:
When I navigate to my existing vue project folder and run yarn run serve inside vagrant ssh I get the following error:
yarn run v1.9.4
$ vue-cli-service serve
/bin/sh: 1: vue-cli-service: Permission denied
error Command failed with exit code 126.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
When I run sudo yarn run serve
(I shouldn't have to run this as root anyway but:)
yarn run v1.9.4
$ vue-cli-service serve
/bin/sh: 1: vue-cli-service: Permission denied
error Command failed with exit code 126.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Running vue --version
vagrant#cc:~$ vue --version
No command 'vue' found, did you mean:
Command 'vpe' from package 'texlive-latex-extra' (universe)
vue: command not found
Output from running yarn global add #vue/cli
As shown in the official vue-cli installation documentation
NOTE: The fsevents#1.2.4 message I get. Could this what is causing the problems?
vagrant#cu:~$ yarn global add #vue/cli
yarn global v1.9.4
[1/4] Resolving packages...
[2/4] Fetching packages...
[----------------------------------------------------------------------------------------------------------------------------------------] 0/617(node:7694) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
info fsevents#1.2.4: The platform "linux" is incompatible with this module.
info "fsevents#1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "#vue/cli#3.0.1" with binaries:
- vue
Done in 58.25s.
Summary:
Has anyone out there achieved a local development environment where they are successfully able to run yarn run serve inside it and access the result on their host machine?
I would be very interested to see how other developers approach their local development for vue js projects which also have other services requiring reverse proxys (eg node js app running on a different port).
I have spent an awful lot of time trying to set this up to no avail. Maybe these tools just don't play well together. If you think you could help I would be very grateful. Thanks
Temporary Workaround: - inspired by this post
After further troubleshooting I found the problem is certainly a permissions related issue (not related to vue-cli).
I think that as my vagrant is using virtualbox, there is a virtualbox issue with symbolic links from the synced folder from my host, which may change permissions and stops chmod command from having any effect on files. In my case the execute flag from node_modules/.bin directory was not-executable.
For anyone with similar issues here is my current workaround to this issue (Do yourself a favour and read https://github.com/hashicorp/vagrant/issues/713 I wish I'd found it earlier!):
1) copy the projects package.json to: the yarn global directory: /home/vagrant/.config/yarn/global/
cp /var/www/project/package.json /home/vagrant/.config/yarn/global/
2) Install the projects dependencies globally in the yarn users directory:
cd /home/vagrant/.config/yarn/global
yarn install
3) Now returning to the project and running yarn run serve works as it uses the node_modules from /home/vagrant/.config/yarn/global/node_modules/.bin/ which has the correct executable permissions.
cd /var/www/project/package.json
yarn run serve
Example of cause of the issue:
1) Change directory to your project and ls -l to see permissions:
cd /var/www/project/node_modules/.bin
ls -la
Outputs:
lrw-rw-rw- 1 vagrant vagrant 18 Aug 29 00:21 which -> ../which/bin/which
2) Attempt to make file executable:
chmod 777 ./which (adding sudo doesn't make this work either)
Outputs:
lrw-rw-rw- 1 vagrant vagrant 18 Aug 29 00:21 which -> ../which/bin/which
OLD ANSWER - DIDN'T WORK: The solution I am now using is taken from here: Source
Adding this to the Vagrantfile enables symbolic links to work properly.
I am using ubuntu on my host and guest machines so can't be sure this will work for Mac and Windows.
config.vm.provider "virtualbox" do |v|
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant", "1"]
end
Further Reading:
https://github.com/hashicorp/vagrant/issues/713
You need to use sudo when installing vue-cli.
The vue-cli documentation at https://cli.vuejs.org/guide/installation.html states that:
To install the new package, use one of the following commands. You
need administrator privileges to execute these unless npm was
installed on your system through a Node.js version manager (e.g. n or
nvm).
npm install -g #vue/cli
# OR
yarn global add #vue/cli

Chef script ressource: how does the user context work?

I'm trying to install pm2 and pm2-logrotate as non root user (specifically, using nginx user), with Chef 12
I installed pm2 globally
[root#~] npm install -g pm2
Then I registered pm2 startup script to run as nginx user
[root#~] pm2 startup -u nginx --hp /home/nginx systemd
Then I want to install pm2-logrotate for the pm2 process running as nginx user
The following works: pm2-logrotate is correctly installed, and the logs under /home/nginx/.pm2/logs are rotated
script "pm2_logrotate" do
interpreter 'bash'
code <<-EOH
su nginx -c "pm2 install pm2-logrotate#2.4.0"
EOH
end
But if I try to install pm2-logrotate by specifying the "nginx" user for the script ressource, I get an error
script "pm2_logrotate" do
interpreter 'bash'
user "nginx"
code <<-EOH
pm2 install pm2-logrotate#2.4.0
EOH
end
Error
[PM2][Module] Calling [NPM] to install pm2-logrotate#2.4.0 ...
==> default: Error: EACCES: permission denied, mkdir '/root/.pm2/modules/pm2-logrotate'
Why is there a different behaviour ? Thanks
The problem is that when Chef changes the user to nginx, it doesn't export some of the environment variables that bash sets on startup, in this case probably $HOME. You can fix this with:
script "pm2_logrotate" do
interpreter 'bash'
user "nginx"
environment 'HOME' => Dir.home(user)
code <<-EOH
pm2 install pm2-logrotate#2.4.0
EOH
end