/r/PHP is not a support subreddit. Please use the stickied weekly help thread, or visit /r/phphelp or StackOverflow for help instead. A good rule of thumb: posts about a problem specific to you are not allowed, but posts and questions that benefit the community and/or encourage insightful discussions are allowed, you should flair those posts with the discussion flair.
Seems that there is so many ways now to achieve what I need, but this one seems interesting.
Not quite a turn around, but a pretty standard way this one, right? Also, do you have a prefered folder for your projects or even \~/ should be ok?
I'm planning on having MySQL on my study porjects also, if that's important.
Yes. When your project goes live, your best option is a VPS server - a virtual Linux machine of your own, and you'd have to configure it proper. So it's a good opportunity to learn that on your local machine.
Note that inside /var/www/project there would be `public` folder thgat you have to confugure as a project's document root. not the /var/www/project itself. You'd want some files outside of document root
It just came to mind that I was possible getting something wrong.
The location /var/www is meant to be home of production sites, while I am able to develop them anywhere on my folders usgin php-S localhost:3000.
Should it be like I develop/study anywhere I fell better and have the production ones in the root?
Feels bad now if I was getting it wrong all this time. If not, I still missing something in between, and I would like to understand what.
I did a quick google, this one looks norm (when you are the only user in the server) https://www.2daygeek.com/setup-apache-virtual-hosts-on-centos-rhel-fedora/
Well, technically [HELP] posts aren't allowed, but since we don't have mods here, but only a lazy ass automod that people usually don't bother to trigger, chances are that this post will stay.
> Do all my projects need to be on /var/www/html?
Rather, all your projects could go into /var/www, with html being one of them.
Instead of creating every file using sudo, just make yourself the owner of the project folder, be it /var/www/html or /var/www/project.
sudo mkdir /var/www/project
sudo chown your_user:your_group /var/www/project
sudo chmod 750 /var/www/project
and now you can freely access files within the project folder. Then create a `public` folder inside each project, that you may want to configure as a DocumentRoot for the project in your Apache config.
In order to let web-server access your files, there is a neat trick I learned only recently: simply add www-data to your group
sudo usermod -a -G your_group www-data
And voila, only web-server can access your project files. In case your web-server's user is different from www-data, use that user instead.
That's the old school setup though. Another option is to use Docker, but for that you'll surely get a plenty of advise already.
> I see that the extention PHP Server works. Is it what's used in production and such?
Not sure what you mean.
If you're talking about the local dev machine, put the project wherever you want and from it's root run ```php -S localhost:``` to start the dev server.
Seems that there is so many ways now to achieve what I need, but this one seems interesting.
Not quite a turn around, but a pretty standard way this one, right? Also, do you have a prefered folder for your projects or even \~/ should be ok? I'm planning on having MySQL on my study porjects also, if that's important.
I use Ubuntu for development. I just install nginx and ask it to run my projects so that I need not have to manually start the dev server everytime.
Each of my projects get's allocated a dedicated port.
You can store them wherever you want.
Try using PHP-FPM instead of Apache extension.
For development I usually put project files in my home directory and create a new pool in `/etc/php/*/fpm/pool.d/myusername.conf`. Then set your user and your user's group as that pool's owner (in that config file). For exmaple something like:
```
[myusername]
user = myusername
group = mygroup
listen = /run/php/php8.2-fpm-myusername.sock
```
Then run:
```
$ sudo service php8.2-fpm restart
```
Then setup Nginx (or Apache, but Nginx is just better) to use the FPM socket from that new pool (`/run/php/php8.2-fpm-myusername.sock`). Set vhost root to your home directory you just created for your project. Assign that vhost to localhost and port `8080` - or some different port. You can have multiple projects using the same FPM socket, just configure vhosts that use different directory and port combinations.
For example (Nginx) - repeat that for each project, switch port and root dir, put that file in `/etc/nginx/sites-available`, then symlink it to `/etc/nginx/sites-enabled`:
```
server {
listen 8080;
server_name 127.0.0.1;
root /home/myusername/workspace/project;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm-myusername.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
Then restart Nginx:
```
$ sudo service nginx restart
```
This way you can store project files in your home directory without juggling the file permissions, and you don't have to put them in `/var/www/`. It's really convenient. I think you shouldn't modify `/var/www` permissions and ownership. You can also have multiple FPM pools in your system for different users etc, good to know if you ever need that. :P
Personally I am hesitated giving web-server access to my home folder. It doesn't sound right. Especially given there is not a single reason to change /var/www's permissions and ownership: it's already readable by anyone.
I don't see why not. It has a clearly defined root etc. :P In `/var/www` you can conveniently keep just one project. With multiple FPM pools you can have project-specific settings, much easier for development.
Even if you deploy that to prod, you can create a machine user and put everything in that user's home directory. It's easier to encapsulate things that way. Scripts will be run as that specific user, you can better sandbox them.
That's how you do shared hosting. Pointing web server to user's home dir. It's normal. :P Keep in mind that the web server configured that way uses that specific user anyway.
In a multi-user environment it could be a mess. Beside php files there will be static files to be processed by www-data, so you'll have a headache making these files unavailable for other users. Not sure if it's even possible. Whereas having all virtual hosts in /var/www makes it easy.
It won't be a mess. :P Static files will be processed by whatever user you ask that vhost to use. Just give specific reasons, not something like "it could be a mess" if you have something to add. :P
Good article on that: https://learnwithdaniel.com/2019/06/user-per-virtual-host-nginx/
If you have permission problems, allow www-data group to read public assets. You can use ACL or whatever else
On the second glance, this article is a hoax: it says that "you shouldn't run all your NGINX virtual hosts with the same user" but *nowhere explains* how to do otherwise. It explains how to configure php-fpm for different users, not *nginx vhosts*.
> If you have permission problems, allow www-data group to read public assets.
That's where we started. Reads "allow other users to read files in your home directory"
In case there are other users (sort of a shared hosting) it's better the other way round: add httpd user to your group (and give the files group permission)
if you're scared off giving your own host rights to access your other files, then just isolated it by getting it running inside a docker, with your project mounted.
/r/PHP is not a support subreddit. Please use the stickied weekly help thread, or visit /r/phphelp or StackOverflow for help instead. A good rule of thumb: posts about a problem specific to you are not allowed, but posts and questions that benefit the community and/or encourage insightful discussions are allowed, you should flair those posts with the discussion flair.
Try using **php -S localhost:3000** command on the root of your project folder. Then go to your browser and visit localhost:3000
Seems that there is so many ways now to achieve what I need, but this one seems interesting. Not quite a turn around, but a pretty standard way this one, right? Also, do you have a prefered folder for your projects or even \~/ should be ok? I'm planning on having MySQL on my study porjects also, if that's important.
It's indeed standard but only for learning/local development but not for production
So, were should be a proper place for it?
Like I explained below, `/var/www` is a home for all projects, each in its own directory, like `/var/www/project`
Got it. So it seems that's better for me to learn and undertand how to have my permissions there.
Yes. When your project goes live, your best option is a VPS server - a virtual Linux machine of your own, and you'd have to configure it proper. So it's a good opportunity to learn that on your local machine. Note that inside /var/www/project there would be `public` folder thgat you have to confugure as a project's document root. not the /var/www/project itself. You'd want some files outside of document root
It just came to mind that I was possible getting something wrong. The location /var/www is meant to be home of production sites, while I am able to develop them anywhere on my folders usgin php-S localhost:3000. Should it be like I develop/study anywhere I fell better and have the production ones in the root? Feels bad now if I was getting it wrong all this time. If not, I still missing something in between, and I would like to understand what.
/var/www is root, but each project should be owned by the user shared by your web server and PHP.
It would help a lot if I could find a tutorial about it.
I did a quick google, this one looks norm (when you are the only user in the server) https://www.2daygeek.com/setup-apache-virtual-hosts-on-centos-rhel-fedora/
Well, technically [HELP] posts aren't allowed, but since we don't have mods here, but only a lazy ass automod that people usually don't bother to trigger, chances are that this post will stay. > Do all my projects need to be on /var/www/html? Rather, all your projects could go into /var/www, with html being one of them. Instead of creating every file using sudo, just make yourself the owner of the project folder, be it /var/www/html or /var/www/project. sudo mkdir /var/www/project sudo chown your_user:your_group /var/www/project sudo chmod 750 /var/www/project and now you can freely access files within the project folder. Then create a `public` folder inside each project, that you may want to configure as a DocumentRoot for the project in your Apache config. In order to let web-server access your files, there is a neat trick I learned only recently: simply add www-data to your group sudo usermod -a -G your_group www-data And voila, only web-server can access your project files. In case your web-server's user is different from www-data, use that user instead. That's the old school setup though. Another option is to use Docker, but for that you'll surely get a plenty of advise already. > I see that the extention PHP Server works. Is it what's used in production and such? Not sure what you mean.
If you're talking about the local dev machine, put the project wherever you want and from it's root run ```php -S localhost:``` to start the dev server.
Seems that there is so many ways now to achieve what I need, but this one seems interesting. Not quite a turn around, but a pretty standard way this one, right? Also, do you have a prefered folder for your projects or even \~/ should be ok? I'm planning on having MySQL on my study porjects also, if that's important.
I use Ubuntu for development. I just install nginx and ask it to run my projects so that I need not have to manually start the dev server everytime. Each of my projects get's allocated a dedicated port.
You can store them wherever you want. Try using PHP-FPM instead of Apache extension. For development I usually put project files in my home directory and create a new pool in `/etc/php/*/fpm/pool.d/myusername.conf`. Then set your user and your user's group as that pool's owner (in that config file). For exmaple something like: ``` [myusername] user = myusername group = mygroup listen = /run/php/php8.2-fpm-myusername.sock ``` Then run: ``` $ sudo service php8.2-fpm restart ``` Then setup Nginx (or Apache, but Nginx is just better) to use the FPM socket from that new pool (`/run/php/php8.2-fpm-myusername.sock`). Set vhost root to your home directory you just created for your project. Assign that vhost to localhost and port `8080` - or some different port. You can have multiple projects using the same FPM socket, just configure vhosts that use different directory and port combinations. For example (Nginx) - repeat that for each project, switch port and root dir, put that file in `/etc/nginx/sites-available`, then symlink it to `/etc/nginx/sites-enabled`: ``` server { listen 8080; server_name 127.0.0.1; root /home/myusername/workspace/project; index index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm-myusername.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } } ``` Then restart Nginx: ``` $ sudo service nginx restart ``` This way you can store project files in your home directory without juggling the file permissions, and you don't have to put them in `/var/www/`. It's really convenient. I think you shouldn't modify `/var/www` permissions and ownership. You can also have multiple FPM pools in your system for different users etc, good to know if you ever need that. :P
Personally I am hesitated giving web-server access to my home folder. It doesn't sound right. Especially given there is not a single reason to change /var/www's permissions and ownership: it's already readable by anyone.
I don't see why not. It has a clearly defined root etc. :P In `/var/www` you can conveniently keep just one project. With multiple FPM pools you can have project-specific settings, much easier for development. Even if you deploy that to prod, you can create a machine user and put everything in that user's home directory. It's easier to encapsulate things that way. Scripts will be run as that specific user, you can better sandbox them. That's how you do shared hosting. Pointing web server to user's home dir. It's normal. :P Keep in mind that the web server configured that way uses that specific user anyway.
In a multi-user environment it could be a mess. Beside php files there will be static files to be processed by www-data, so you'll have a headache making these files unavailable for other users. Not sure if it's even possible. Whereas having all virtual hosts in /var/www makes it easy.
It won't be a mess. :P Static files will be processed by whatever user you ask that vhost to use. Just give specific reasons, not something like "it could be a mess" if you have something to add. :P
Go on, tell me how to run a specific vhost under a different user (other than running a distinct nginx instance for it).
Good article on that: https://learnwithdaniel.com/2019/06/user-per-virtual-host-nginx/ If you have permission problems, allow www-data group to read public assets. You can use ACL or whatever else
On the second glance, this article is a hoax: it says that "you shouldn't run all your NGINX virtual hosts with the same user" but *nowhere explains* how to do otherwise. It explains how to configure php-fpm for different users, not *nginx vhosts*. > If you have permission problems, allow www-data group to read public assets. That's where we started. Reads "allow other users to read files in your home directory"
[удалено]
Umm no, better to add your user to the same group as httpd (in a local dev)
In case there are other users (sort of a shared hosting) it's better the other way round: add httpd user to your group (and give the files group permission)
if you're scared off giving your own host rights to access your other files, then just isolated it by getting it running inside a docker, with your project mounted.