Symfony Built-in Web Server on an external environment - php

I configured a web service locally using FOSRestBundle, and it's working perfectly. The only thing i need to write to start my service is
php bin/console server:run xxx.xxx.x.xx:port
and it's all set.
But then i was reading some documentation about the symfony web server, and this information is confusing me:
The built-in web server is meant to be run in a controlled
environment. It is not designed to be used on public networks.
How i'm suppose to use this in my cloud environment? If i want to make this public, how should i start my REST service without using this built-in server?
What is equivalent to the "server:run" command? If i just put the code there, it will not work. I need to start the server for my REST API.

The equivaluent of the server:run command in a production environment is a bit more involved. The web server on a public facing machine has more responsibility than a local development server, and so it needs a bit more configuration.
For production (or even staging) purposes, use a production-ready web server like Apache or Nginx.
I'll share a default setup for apache, on a Debian-based (Ubuntu) system here, and detail a few common pitfalls to avoid.
Install LAMP components
ssh into your machine, and run this command to make sure that apache, php, mysql, etc, are all installed!
First, get the most recent data from the repositories:
sudo apt-get update
The LAMP components that you'll need to run symfony on:
sudo apt-get install apache2 mysql-server libapache2-mod-auth-mysql php5-mysql php5 libapache2-mod-php5 php5-mcrypt
Be sure to store down the root MySQL pass if it asks you for one. (in the event you didn't have it installed, yet.)
Configure MySQL
sudo mysql_install_db to initialize MySQL's system/help tables, etc.
sudo /usr/bin/mysql_secure_installation to drop test tables, reload privilege tables.
Jump into the MySQL shell and create a user & db for your app.
mysql -u root -p, and supply your root pw when prompted.
mysql> CREATE USER 'otuyh'#'localhost' IDENTIFIED BY 'password'; will create your otuyh user with the password password.
mysql> CREATE DATABASE otuyh_app; will create the database your app needs to run on.
mysql> GRANT ALL PRIVILEGES ON otuyh_app.* TO 'otuyh'#'localhost';
mysql> FLUSH PRIVILEGES;
Configure Apache (the production version of server:run)
Clone or upload your project to /var/www/html.
Edit /etc/apache2/sites-enabled/000-default.conf and change the line:
DocumentRoot /var/www/html to DocumentRoot /var/www/html/web
This tells apache which directory to serve requests from the web folder of your app.
Also, alter the line ServerName white-macbook to ServerName example.com where example.com is the domain name pointed at your server.
Finally, add an AllowOverride All line in there, too, to explicitly state that your .htaccess file can also add directives for the web server.
Drop an .htaccess file in your web/ folder that looks something like this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Explicitly disable rewriting for front controllers
RewriteRule ^app_dev.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
Alter app_dev to just app, when you're ready to change the environment to production.
Make sure you restart the apache2 service after having altered it's config: sudo service restart apache2.
Run your regular sf2 schema creation, composer install, etc.
And then, make sure that the apache user that serves the files up indeed has access to them!
Additionally, after running composer * or cache:clear as a user other than the one serving files, write permissions can get mixed up, and www-data will need this privilege to write logs, and get cached files. A quick solution:
chown -R your-unix-user:www-data ./app
find ./app/cache -type d -exec chmod 775 {} \;
find ./app/logs; -type d -exec chmod 775 {} \;
find ./app/cache -type f -exec chmod 664 {} \
find ./app/logs -type f -exec chmod 664 {} \

Like mentioned the built in Webserver is for Development only.
It is slow and open to all kinds of attacks.
You can run your Service like any other application with a Webserver.
There is a guide on the Symfony site that shows you how to do that.
http://symfony.com/doc/current/deployment.html
Or if you have root access to your cloud server you can configure your Webserver
https://symfony.com/doc/current/setup/web_server_configuration.html

Related

Laravel not rendering sample blog, permissions to directories already given

I'm testing Laravel on a Arch Linux ARM, to which I'm connecting through SSH.
Going throughout the Laravel's docs
I created a new blog. So far so good.
The Docs mention the following:
After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.
After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run. If you are using the Homestead virtual machine, these permissions should already be set.
This post says that 'writable by my webserver' means that it must be writable by the user/group which is running apache. I was able to get the group that apache is running from using
ps -ef | grep httpd | grep -v grep.
The user turned out to be http. I checked both /etc/group and /etc/password and the user and group exist with the same name. So I changed the permissions of both bootstrap/cache and the directories inside storage with
sudo chown http bootstrap/cache/
sudo chown -R http storage
sudo chgrp http bootstrap/cache/
sudo chgrp -R http storage
And gave writting permissions to the http group, I realize that this is overkill, since permissions are additive but I still had to try.
sudo chmod g+w bootstrap/cache/
sudo chmod -R g+w storage/
I've read plenty of threads and most of them mention Virtual Hosts so I thought I might as well attempt that, I went throughout the Arch Apache wiki
If you want to have more than one host, uncomment the following line in /etc/httpd/conf/httpd.conf:
Include conf/extra/httpd-vhosts.conf
To test the virtual hosts on your local machine, add the virtual names to your /etc/hosts file:
127.0.0.1 domainname1.dom
<VirtualHost *:80>
DocumentRoot "/srv/httpd/blog/public"
ServerName www.mytest.com
</VirtualHost>
And on my local Ubuntu computer I have <my_Linux_ARM_IP> www.mytest.com on /etc/hosts/
So, whenever I type www.mytest.com on my local computer I get.
I changed the DocumentRoot to test a simple and pure index.html file and it rendered just fine, so I figure the issue is with Laravel.
I was able to manage all this on a local WAMP system beforehand, write the vhosts, change the hosts file so that localhost points to the vhost. And blog rendered just fine.
So it's not Laravel but Apache and the LAN connection? I'm not sure where to go next. Should I read more Apache docs? Should I read more Laravel docs? Should I read more networking stuff? If so, could someone provide some good resources?
Thank you.
Thank you #langbox.
I just followed the steps of this wiki, I chose to use libphp and now it renders correctly.

apache temporarily VirtualHost by script start

Let's say I have a local apache2 and I need a script that runs the apache2 with a special injected VirtualHost.
More presice:
I have a xampp Installation (PHP 7.3.10, MariaDB 10.4.8, mysqlnd 5.0.12-dev, Apache 2.4.41) on Windows 10.
Several PHP projects are located all over the hard drive. Until now I modify the ./apache/conf/extra/httpd-vhosts.conf file before starting the apache2 service on every project switch. With that I can leave the project folders where there are without copying them in the xampp installation folder.
Now I am curious if I can write a script for each project (bat or sh, GitShell is installed) that will run mariadb normally and apache2 with a temporarily VirtualHost for that specific project folder as DocumentRoot.
What I currently can do:
Lay down a httpd-vhosts.conf file and a startProject.sh script in a project folder. Running the script will copy the configuration file in the xampp installation and then start the apache2 service.
That is working, but only one apache2 process can start and not multiple projects at once.
What I want:
I want to use a parameter that specifies a VirtualHost by parameter when starting apache2.
Maybe by saying apache2 to use a special config file rather then the file in the own project structure.
Or I want to use a parameter by starting the apache2 process that specifies a alternatie DocumentRoot Folder for a already specified VirtualHost.
Currently this is my approach:
#!\bin\bash
BASEDIR=$(dirname "$0")
xamppPath="C:\xampp\v7.3.10"
cd $xamppPath
"apache\bin\httpd.exe" -S "$BASEDIR\httpd-vhosts.conf" &
But -S only prints out the VirtualHosts, not setting the configuration file.
How can I achieve running a project with a script somewhere on my harddrive without overriding the existing configuration file in the xampp installation folder?
Easier: build a couple of congratulation files, use option -f to specify an effective configuration file, not -S.
Can be more complicated: user -D name and use <IfDefine name> in the configuration file to "select" sections of it to apply to a particular start.
Note: I use apachectl, not directly httpd to start/stop Apache.
In the xampp package there is no direct apachectl file, so a use of -D <name> is not possible for me. It would be the perfect solution, but unfortunately not possible.
Now I have to override the httpd-vhosts.conf file from every project I want to start it. Therefore, I am using a modified script solution:
#!\bin\bash
BASEDIR=$(dirname "$0")
xamppPath="C:\xampp\v7.3.10"
cp -f "$BASEDIR\httpd-vhosts.conf" "$xamppPath\apache\conf\extra\httpd-vhosts.conf"
sleep 1
cd $xamppPath
"apache\bin\httpd.exe" &
It is not the perfect solution, but it works.
Two apache services at the same time with two different projects is not possible, but until now not necessary.

Laravel move project from Ubuntu Server to Local XAMPP

I have a Laravel project that I copied from my Ubuntu server and now I am trying to run it my local machine (XAMPP on Mac) I have been struggling with this for a few days now and I feel like I am going insane.
When I paste my project in XAMPP htdocs folder I get this error:
View [welcome] not found
Which php artisan cache:clear makes that go away, then I get this error:
The bootstrap/cache directory must be present and writable
Then I do this, php artisan cache:clear which gives me a new error:
Class view does not exist
Then after that no matter what I do either in terminal or viewing the web browser, I always get the error
Class view does not exist
Then I have tried composer update still the same error.....what am I doing wrong?
This has been a nightmare.
Last time i checked Laravel doesnt run on XAMPP but rather on the PHP installed when installing XAMPP so the project can be saved anywhere on the computer.
Given this being the fact, you will need to just have an active PHP installation and then you copy only the relevant files of the project onto the new computer (such files that you will get when you push your project onto GitHub). It doesn`t come with cache issues then all you need to do afterwards is to
php artisan key:generate
then composer install or composer update to get the vendor packages from online
My money right now is on picking the relevant files and reinstall with them
According to my own installation when changing the computer this is the list you will have to copy
I just tried to reproduce your issue on my mac. So i have installed XAMPP with the PHP version 7.1.25 which is the equivalent version of my local PHP version
So I installed the XAMPP and started server.
Downloaded my laravel project folder from my ubuntu server and copied it to htdocs (XAMPP)
When i tried to run http://localhost/myproject/public it shows the exception like
There is no existing directory at "/Applications/XAMPP/xamppfiles/htdocs/myproject/storage/logs" and its not buildable: Permission denied
Then i gave full permission to the storage folder
chmod -R 777 storage
And changed ownership for the files inside myproject folder.
Here i just checked the ownership of the dashboard directory which is running perfectly and given the same user ownership of myproject directory.
chown -R root:admin .
Then following commands
composer install
php artisan cache:clear
php artisan view:clear
php artisan route:clear
After this my laravel code runs perfectly.
Class view does not exist
is probably a ownership issue of the directory
For me (when developing on xampp, what I do for all my projects) - I'd not recommend to put your stuff in the htdocs folder. Laravel expects to not be hosted on a subfodler e.g. (localhost/my-project). So you should set up a virtual host in order to make it work easily (e.g. my-project.test) which is a bit annoying.
Simple solution is using the php artisan serve command in order to simply setup a local server on port 8000.
Don't forget to start xampp for the mysql server.
Some typical tips were already mentioned:
delete vendor folder & run composer install (install composer if you haven't)
run composer dump-autoload
run php artisan key:generate
ofcourse don't forget the migration php artisan migrate
and clear your full cache php artisan cache:clear
Usually you do not need to set any file permissions afaik
chmod -R 777 storage/
If you have a different user for apache2 (usually www-data), also do:
chown -R www-data storage/
You could also check if it runs with the built-in server:
php artisan serve
You can create .htaccess file and add below data into .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
After create .htaccess file, set laravel root path in terminal and run below command in terminal
php artisan serve
Since your Apache is already serving then you have permission problems only. And since you're using Mac, the default user name and group in Apache conf is _www for Mac and not www-data that is for Ubuntu. You can see it in the httpd.conf file :
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User _www
Group _www
</IfModule>
Now, use terminal, get in your project directory let's say cd /var/www/laravel-project1 and make sure that the group _www (or the user too in some case of your App environment logic` has access (read and write) to :
All public directory and sub-directories containing assets if you have.
sudo chgrp -R _www public
sudo chmod -R 774 public
Storage directory and sub-directories specified here (storage/framework -> all, storage/logs -> all, storage/app -> public directory only), and bootstrap/cache directory and files
sudo chgrp -R _www storage/framework storage/logs storage/app/public bootstrap/cache
sudo chmod -R 774 storage/framework storage/logs storage/app/public bootstrap/cache
That should get rid of all of your permissions problem to access pages.
BUT now if on using the page, sessions and logs files that are created you get other problems, there might be a last problem of permission which is called UMask, which tell Apache or Web Server like Nginx what permission to assign to newly created directory or files for the user _www. By default Apache umask is 0002, which give 0775 for directory and 0664 for new file. If ever umask value was changed to 0022 like it's the default in Nginx, then the equivalent permissions 0755 or 0644 will not be sufficient for your Apache group _www to write in the directories that have group _www. So you can either change umask to 0002 or change owner to _www :
sudo chown -R _www public storage/framework storage/logs storage/app/public
So that depends on your configs.
I ran into the same problem as you, but not moving from ubuntu to mac, it was from windows to linux, I was in a total mess, but only git rescued me, it might give you a bit of pain, but it is going to save you in the future.
Here is the steps you need to do.
Create empty repository on the mac using this command git init --bare.
Clone the repository to the ubuntu using git clone.
Copy your laravel code to the clone you made in step 2.
Push the files from the ubuntu to the MAC.
Test the project.
The directory you will create in the mac, it can be inside the htdocs of the xammp.
I know it might be painful task to do, but it is quite worth it.
Sources for more information:
git-scm
Getting Git on Server
If you need more help, I'll be more than happy to discuss it with you.
Note: The following works for Laravel 5.x but also 4.2, not tested with other versions
Why not using Git?
(If you are not familiar with it, have a look at the official website, there are also tons of tutorials on the web)
Usually, copy-pasting entire projects is not a good idea, because of some file/directory permissions and other not-so-good stuff.
That's what I did to move my project from Windows to my Ubuntu Server:
Put your project on a git repository (GitHub, GitLab, or whatever), the .gitignore files provided with the Laravel apps are, in most cases, good enough
On your new machine, clone your repository
Do a
composer dump-autoload
composer install
To migrate your db, do
php artisan migrate
and if you have seeding, do this
php artisan db:seed
Then, if you have problem with file/folder permissions, do not EVER do a chmod -R 777 path/, if you have to do this to solve your problem, you are doing something wrong.
This command grants all privileges to anyone to all the files and folders in the path folder.
In your case, you have to do the following:
First, find which username is apache using to run the server (usually it's www-data)
ps aux | egrep '(apache|httpd)'
Then, change the project directory owner to apache's user (example for www-data apache user)
sudo chown -R www-data:www-data /var/htdocs/your-project/
Set folders permissions
sudo find /var/htdocs/your-project/ -type d -exec chmod 755 {} \;
Set files permissions
sudo find /var/htdocs/your-project/ -type f -exec chmod 644 {} \;
To fix the bootstrap/cache and storage/ permission problem, do
sudo chown -R www-data:www-data /var/htdocs/your-project/
Laravel 5.x
sudo chmod -R ug+rwx /var/htdocs/your-project/storage /var/htdocs/your-project/bootstrap/cache
Laravel 4.2
sudo chmod -R ug+rwx /var/htdocs/your-project/storage /var/htdocs/your-project/app/bootstrap/cache
Then, you should be good.
Doing that way, you can easily move your project from a machine to another without struggling with permission fixes or anything.
More info and source for files and folders permissions command-line instructions, see Laravel 5 Files Folders Permission and Ownership Setup
Go to your project folder add run these commands from terminal
sudo chmod -R 777 your_projrct/storage
sudo chmod -R 777 your_projrct/bootstrap/cache
sudo chown -R :www-data your_project
sudo chmod -R g+s your_project
then php artisan key:generate and composer install
when user/group www-data are unkown; most Linux distributions use apache:
chown -R apache:apache dirname
while on OSX, this would be user/group _www:
chown -R _www:_www dirname
adding the current user to group _www might make life easier, in general.
To isolate your issues:
Get your code into a repository(bitbucket or github)
Clone the repository into your local environment
Run composer install
Run php artisan serve. This way you rule out xampp as an issue.
In your browser go to localhost:8000
If you already have your entire codebase on your local box(including the vendor folder) then skip steps 1 and 2. Step 3 wouldn't hurt, but you can probably skip that too.
Once you get everything working, switch to xampp.
First of all:
composer update
composer dumpautoload
php artisan cache:clear
And then just configure a virtual host
1. Create a local domain for your app
Edit hosts file and redirect all requests from your domain to 127.0.0.1:
127.0.0.1 lara.vel
2. Configure a Virtual Host
Edit \xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost lara.vel:80>
DocumentRoot "C:\xampp\htdocs\laravel\public"
ServerAdmin laravel.dev
<Directory "C:\xampp\htdocs\laravel">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Done! Open up your domain in your browser, You'll see your project there!
The other way is simple, Just run artisan serve.
Please vote up if you found this answer useful.
I think if you copied the project from Ubuntu so it is a permissions problem.
first get users on your Mac machine by typing this in terminal
users
then copy the user you just got for example (username) and use it in this command
sudo chown -R username project-directory
then check for yourproject-directory/bootstrap/cache if it not exists, go create it. else run this command:
sudo chmod -R guo+w project-directory/bootstrap/cache
then
sudo chmod -R guo+w project-directory/storage
then clear composer autoload and cache and config using artisan command
composer dump-autoload
php artisan cache:clear
php artisan view:clear
php artisan route:clear
now try to run the project if the problem still exists,
you need to check config/app.php if it contains Illuminate\View\ViewServiceProvider::class
the view service provider.
if it is not there, so add it

Setting up Laravel, getting PDO and permission problems

So on my linux AWS instance, I am trying to install a laravel application and am running into an awful amount of permission problems.
By default, when I cloned my project into var/www/, the owner was Root. I changed the owner to apache, and added ec2-user to apache. From what I've read, this seems correct.
From there, I tried to run php composer.phar install, which resulted in a permissions error unless I ran it sudo, and then the error was that "Class 'PDO' not found in /var/www/Mumble/app/config/database.php".
So from there, it looked like PDO wasn't installed, so I used yum to install it, which got me the typical laravel error log, but it is now saying "could not find driver". Looking at php info, pdo is configured for mysqli. Could that be my problem? Does anybody know of some places I could look for resources?
First things first, the latest version of laravel is 4.3, compatible with PHP >= 5.4 (Source: Laravel Installation).
The bad news is, the yum package of apache on AWS comes with PHP 5.3. Check your php version using php phpinfo() in your ssh console.
If you have the proper PHP version, you will have to make sure PHP is running underneath the user apache.
Go to /etc/php.ini and search for the parameter user=. Make sure it says user=apache, and group=apache.
If all of this checks out, your final step is to make sure that your /app/storage directories are on a 777 permission with owner being apache. Only laravel uses these for internal purposes, so it's alright.
I suspect you need to give the permission to access, is it accessible the /var/www directory?
if not try this,
sudo chmod -R 777 /var/www
for secure permission use 775 for the directories.
perhaps permission you need to set:
# Set group to www-data
sudo chgrp www-data /var/www
# Make it writable for the group
sudo chmod 775 /var/www
# Set GID to www-data for all sub-folders
sudo chmod g+s /var/www
# Add your username to www-data group
sudo usermod -a -G www-data username
# Finally change ownership to username
sudo chown username /var/www/
# Your account shouldn't have any more permission issues
Note: please read about the file permission before you go further .

Lamp Server 403 Forbidden

Hi I just installed ubuntu alongside my Win 7 and I have been using xampp and am very familiar with it, but I just installed lamp and am using the apache2, php, and mysql from terminal and I copied a web folder over from my xampp side and it is saying that I do not have permission to access that file.
I know that on my pc I had some htaccess files but on Ubuntu I am yet to figure out how to view those. Is this a product of those .htaccess files or something else?
That happens if you install WAMP/XAMMP on system partition.
so you copied the files form windows to linux?
sounds like a classic file permissions problem.
per default the www folder is in /var/www, so you can simply set the owner of this folder to the apache user which is called www-data, run this in terminal:
sudo chown -R www-data:www-data /var/www
but now you will no longer be able to write to those files yourself, because they are owned by www-data. checkout this answer for more details and how to get write access: https://askubuntu.com/a/51337
or you make it writeable for everyone (which is a bad idea): sudo chmod -R 777 /var/www
about editing .htaccess files:
I don't use linux with a graphical user interface, but you can edit them with the terminal editor of your choice, on ubuntu you have nano installed by default:
nano /var/www/.htaccess
or if you want a more advanced editor, I suggest vim https://help.ubuntu.com/community/VimHowto
vim /var/www/.htaccess

Categories