Welcome Guest, Not a member yet? Register   Sign In
How to run codeigniter4 in shared hosting ?
#1

Hello, 
I am learning framework from the beginning and I would like to use Codeigniter for my projects. As I am working with a shared hosting I would like to know how to install Codeigniter4 in shared hosting properly. I have read the user guide and also search through the forum but there is no good solution. Some people saying to move the public folder contents to the root directory and some suggestions to create an index.php file in the root and point it to the public/index.php folder. But there is no proper guideline. Can anyone give me a proper guideline to how I can run my application in the shared hosting? I saw the Codeigniter3 user guide, and it was super easy for the beginner. 
There should be some proper guideline for the beginner. 
Can anyone help?
Reply
#2

(This post was last modified: 03-24-2020, 11:01 AM by captain-sensible.)

well not only do i have shared hosting but i also am using domains via "add on domains" and " sub domains"  as part of my hosting.



I have a web up using CI4 but won't paste the url since i had a previous post deleted ,which i assumed was due to some sort of ban on promoting webs.

Anyway there was not that  much to do really.  In my control panel for hosting there is an option to set the "document root ". When i look at my hosting and directories, generally the document  root is the same as the  directory name.

But with CI4 because the bootstrap index.php is inside the "public" directory of your app; you need to set the
document root of your domain to domain/public

For App.php located at app/Config/App.php  you need to set the base url (line 24 of APP.php) to your domain.

eg on my local host is was :
public $baseURL = 'http://127.0.0.2';

so i changed  it to:

public $baseUrl= 'http://myDomainName.com';


Then the next issue is moving from http to https


For that i changed :
public $baseUrl= 'http://myDomainName.com';

to : $baseUrl= 'https://myDomainName.com';

then in the .htaccess file located in public directory I changed :
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

to:

RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Now on my hosting they have a "Lets Encrypt" facility
So i basically set up SSL using lets Encrypt and found every thing worked.When i looked at the .htaccess file i noted "lets Encrypt" had done some over writing .

BAsically to go from localhost to live thats all i did
Reply
#3

(03-24-2020, 10:53 AM)Ithinkscaptain-sensible Wrote: well not only do i have shared hosting but i also am using domains via "add on domains" and " sub domains"  as part of my hosting.



I have a web up using CI4 but won't paste the url since i had a previous post deleted ,which i assumed was due to some sort of ban on promoting webs.

Anyway there was not that  much to do really.  In my control panel for hosting there is an option to set the "document root ". When i look at my hosting and directories, generally the document  root is the same as the  directory name.

But with CI4 because the bootstrap index.php is inside the "public" directory of your app; you need to set the
document root of your domain to domain/public

For App.php located at app/Config/App.php  you need to set the base url (line 24 of APP.php) to your domain.

eg on my local host is was :
public $baseURL = 'http://127.0.0.2';

so i changed  it to:

public $baseUrl= 'http://myDomainName.com';


Then the next issue is moving from http to https


For that i changed :
public $baseUrl= 'http://myDomainName.com';

to : $baseUrl= 'https://myDomainName.com';

then in the .htaccess file located in public directory I changed :
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

to:

RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Now on my hosting they have a "Lets Encrypt" facility
So i basically set up SSL using lets Encrypt and found  every thing worked.When i looked at the .htaccess file i noted "lets Encrypt" had done some over writing .

BAsically to go from localhost to live thats all i did

But I think this is not the solution. What if I want to do it in my main domain? Every hosting provider does not provides SSH access.
Reply
#4

(This post was last modified: 03-24-2020, 11:50 AM by captain-sensible.)

But I think this is not the solution. What if I want to do it in my main domain? Every hosting provider does not provides SSH access.

if its your main domain no problem , but you still  going to have to set document root to public . When say you are developing in a directory called 'mydev' make sure you go inside that directory ,zip every thing and load that up to your hosting space . I am not actually talking about getting access to server via SSH what i mean is set up a certificate so as to enable https in url rather than http. You can of course still use http but google is penalizing those webs that do not have https. https of course is encrypted

in summary i have found there is no big deal with going from local development to live
Reply
#5

Hey
there is nothing or not much in here because CI has nothing to do with server administration.
I strongly recommend to search the web for (what you probably mean and want to do) virtual hosting.
There are plenty of good sources out there, even a few almost ready to go configuration examples you can modify for your needs. I'd strongly recommend to NOT TO put your public folder into the root folder of the application.
So far I can judge it are your abilities to deal with access rights and the like not sufficient to deal with such a delicate task.

Here I put you my localhost configuration which is easy to transfer onto a distant server.
The naming can be different depending on what kind of OS you are using.

httpd.conf

Code:
#
# /etc/httpd/conf/httpd.conf
#

# --------------------------------------------------------------------------- #
# --- CGI ------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# IMPORTANT: If you do choose to place your CGI scripts in a directory already
#            accessible from the web, DO NOT USE 'ScriptAlias'.
#            Instead, use <Directory>, SetHandler, and Options like in:
#
#            <Directory "/usr/local/apache2/htdocs/cgi-bin">
#                SetHandler cgi-script
#                Options ExecCGI
#            </Directory>

# (Example) configuration:
LoadModule cgid_module modules/mod_cgid.so
ScriptSock /var/run/cgid.sock

<IfModule cgid_module>
</IfModule>

<IfModule alias_module>
    <Directory "/srv/http/cgi-bin">
        SetHandler cgi-script
        Options ExecCGI
    </Directory>
</IfModule>

# --------------------------------------------------------------------------- #
# --- Dynamic Shared Object (DSO) Support ----------------------------------- #
# --------------------------------------------------------------------------- #
# NOTE: Further modules can be found in '/etc/httpd/modules'.
# --- Authentication --- #
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so

# --- Authorization --- #
LoadModule access_compat_module modules/mod_access_compat.so

# --- (Content) Filter --- #
LoadModule negotiation_module modules/mod_negotiation.so

# --- Logging/Status --- #
LoadModule log_config_module modules/mod_log_config.so
LoadModule logio_module modules/mod_logio.so
LoadModule status_module modules/mod_status.so

# --- Multithreading and processes --- #
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# --- Others --- #
LoadModule env_module modules/mod_env.so
LoadModule filter_module modules/mod_filter.so
LoadModule include_module modules/mod_include.so
LoadModule mime_module modules/mod_mime.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule version_module modules/mod_version.so

#--- PHP --- #
LoadModule php7_module modules/libphp7.so
AddHandler php7-script .php

# --- URL, header and file control/manipulation/redirections --- #
LoadModule alias_module modules/mod_alias.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule headers_module modules/mod_headers.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule userdir_module modules/mod_userdir.so

# --------------------------------------------------------------------------- #
# --- (Default) Directory Security Model ------------------------------------ #
# --------------------------------------------------------------------------- #
# NOTE: AllowOverride are types of directives allowed in .htaccess files
#       Defaults to 'all'. For security and performance reasons, do not set
#       AllowOverride to anything other than None in your <Directory /> block.

<Directory />
    AllowOverride None
    Options None
    Order Deny,Allow
    Deny from all
</Directory>

<Directory /srv/http/>
    AllowOverride All
    Options All -Indexes
    Order Allow,Deny
    Allow from all
</Directory>

Alias /public "/srv/http/public/"
<Directory /srv/http/public/>
    AllowOverride None
    Options None +SymLinksIfOwnerMatch +Indexes +MultiViews
    IndexOptions Charset=UTF-8 DescriptionWidth=64 FoldersFirst HTMLTable SuppressIcon VersionSort
    Require all granted
</Directory>

# --- Symfony Test Enfironment --- #
<Directory /srv/http/symfony.tld/myProject/public/>
    AllowOverride All
    Require all granted
</Directory>

<Directory /srv/http/symfony.tld/myProject/bundles>
    FallbackResource disabled
</Directory>

# Files description(s)
# NOTE: only for files, folder are not implemented in apache.
AddDescription "Contact" public/README
AddDescription "PGP Key" public/pubkey

Alias /error "/srv/http/error/"
<Directory /srv/http/error/>
    AllowOverride None
    Options None
    Require all granted
</Directory>

# Prevent .htaccess and .htpasswd remote access.
<Files ".ht*">
    Require all denied
</Files>

# --------------------------------------------------------------------------- #
# --- Error handling -------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# Error responses come in three flavors:
# plain text, local redirects and external redirects.

#ErrorDocument 400 /error/bad_request.html
#ErrorDocument 401 /error/unauthorized.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/not_found.html
#ErrorDocument 500 /error/internal_server_error.html
#ErrorDocument 502 /error/bad_gateway.html
#ErrorDocument 503 /error/service_unavailable.html
#ErrorDocument 505 /error/http_version_not_supported.html
#ErrorDocument 511 /error/network_authentication_requiered.html

# --------------------------------------------------------------------------- #
# --- Global configuration -------------------------------------------------- #
# --------------------------------------------------------------------------- #
AccessFileName .htaccess
AddDefaultCharset UTF-8
DefaultLanguage en
DocumentRoot /srv/http/

# NOTE: ServerRoot should be specified before this directive is used, otherwise
#       the default value of ServerRoot is used to set the base directory.
#DefaultRuntimeDir /etc/httpd

EnableMMAP Off
EnableSendfile Off
#HostnameLookups Off
#KeepAlive On
KeepAliveTimeout 5

Listen 80

#MaxKeepAliveRequests 500
#MaxRanges 200
PidFile /var/run/apache.pid
ServerRoot /etc/httpd
#Timeout 60

# --------------------------------------------------------------------------- #
# --- Module configuration -------------------------------------------------- #
# --------------------------------------------------------------------------- #
# Set the file Apache will serve if a directory is requested.
<IfModule dir_module>
    DirectoryIndex index.html index.php /index.php
</IfModule>

<IfModule headers_module>
    RequestHeader unset Proxy early
</IfModule>

<IfModule mime_module>
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType audio/ogg .ogg .ogv
    AddType text/html .shtml
    AddEncoding x-compress .Z
    AddEncoding x-gzip .gz .tgz
    AddHandler cgi-script .cgi
    AddHandler type-map var
    AddOutputFilter INCLUDES .shtml
    TypesConfig conf/mime.types
</IfModule>

#<IfModule !mpm_prefork_module>
#LoadModule cgid_module modules/mod_cgid.so
#</IfModule>

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

#<IfModule mpm_worker_module>
#    StartServers          8
#    MaxClients          150
#    MinSpareThreads      25
#    MaxSpareThreads      75
#    ThreadsPerChild      25
#    MaxRequestsPerChild   0
#</IfModule>

# Configure mod_proxy_html to understand HTML4/XHTML1.
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

#<IfModule ssl_module>
#SSLRandomSeed startup builtin
#SSLRandomSeed connect builtin
#</IfModule>

<IfModule unixd_module>
User http
Group http
</IfModule>

# --------------------------------------------------------------------------- #
# --- Logging --------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
CustomLog "/var/log/httpd/access.log" common
ErrorLog /var/log/httpd/error.log
LogLevel warn

<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>

# --------------------------------------------------------------------------- #
# --- Supplemental configuration -------------------------------------------- #
# --------------------------------------------------------------------------- #
# Configuration files in conf/extra/ directory can be included to add extra
# features/to modify default configuration of the server.

# Fancy directory listings
#Include conf/extra/httpd-autoindex.conf

# Language settings
Include conf/extra/httpd-languages.conf

# Multi-language error messages
#Include conf/extra/httpd-multilang-errordoc.conf

# php
Include conf/extra/php7_module.conf

# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf

# User home directories
#Include conf/extra/httpd-userdir.conf

# Various default settings
Include conf/extra/httpd-default.conf

# --------------------------------------------------------------------------- #
# --- ssl/tls --------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
#LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
#LoadModule ssl_module modules/mod_ssl.so
#Include conf/extra/httpd-ssl.conf

# --------------------------------------------------------------------------- #
# --- vhosts ---------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
Include conf/vhosts/codeigniter.conf
Include conf/vhosts/drupal.conf
Include conf/vhosts/ph7cms.conf
Include conf/vhosts/symfony.conf


The CI vhost file:

Code:
#
# /etc/httpd/conf/vhosts/codeigniter.conf
#

# --------------------------------------------------------------------------- #
# --- http port 80 ---------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# --- default landing page port 80 --- #
<VirtualHost *:80>
DocumentRoot /srv/http/public/
HostnameLookups Off
ServerName localhost.public:80
#Redirect "https://localhost.public:443"
</VirtualHost>

# --- http://localhost.codeigniter.tld:80 --- #
<VirtualHost *:80>
CustomLog /var/log/httpd/80/codeigniter.tld.log combined
DirectoryIndex index.html index.php
DocumentRoot /srv/http/sites/codeigniter.tld/myProject/public/
ErrorLog /var/log/httpd/80/codeigniter.tld.error.log
HostnameLookups Off
LogLevel error
RewriteEngine On
RewriteOptions inherit
#ServerAdmin
#ServerAlias www.localhost.codeigniter.tld *.localhost.codeigniter.tld
ServerName localhost.codeigniter.tld:80
ServerSignature Email
UseCanonicalName On
#Redirect "https:localhost.codeigniter.tld:443*
</VirtualHost>

# --------------------------------------------------------------------------- #
# --- tls/ssl port 443 ------------------------------------------------------ #
# --------------------------------------------------------------------------- #
# Global options
#SSLCipherSuite "HIGH:MEDIUM:!SSLv3:!kRSA"                               <TODO>
#SSLProxyCipherSuite HIGH:MEDIUM:!SSLv3:!kRSA                            <TODO>
#SSLHonorCipherOrder On
#SSLProtocol all -SSLv3
#SSLProxyProtocol all -SSLv3
#SSLPassPhraseDialog  builtin
#SSLSessionCache "dbm:/run/httpd/ssl_scache"
#SSLSessionCache "shmcb:/run/httpd/ssl_scache(512000)"
#SSLSessionCacheTimeout 300
#SSLUseStapling On
#SSLStaplingCache "shmcb:/run/httpd/ssl_stapling(32768)"
#SSLStaplingStandardCacheTimeout 3600
#SSLStaplingErrorCacheTimeout 600

# --- default landing page port 443 --- #
#<VirtualHost *:443>
#DocumentRoot /srv/http/public/
#ServerName localhost.public:443
#SSLEngine on
#</VirtualHost>

# --- http://localhost.codeigniter.tld:443 --- #
#<VirtualHost *:443>
#CustomLog /var/log/httpd/443/codeigniter.tld.log combined
#DirectoryIndex index.html index.php
#DocumentRoot /srv/http/sites/codeigniter.tld/myProject/public/
#ErrorLog /var/log/httpd/443/codeigniter.tld.error.log
#HostnameLookups Off
#LogLevel error
#RewriteEngine On
#RewriteOptions inherit
#ServerAdmin
#ServerAlias www.localhost.codeigniter.tld *.localhost.codeigniter.tld
#ServerName  localhost.codeigniter.tld:443
#ServerSignature Email
#SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
#SSLCACertificateFile
#SSLCACertificatePath
#SSLCARevocationPath /srv/ssl/revoke/codeigniter.tld/revoke.crl
#SSLCARevocationFile
#SSLCARevocationCheck chain
#SSLCertificateChainFile
#SSLCertificateFile /srv/ssl/                                            <TODO>
#SSLCertificateKeyFile /srv/ssl/                                         <TODO>
#SSLEngine on
#SSLRandomSeed startup file:/dev/urandom 512
#SSLRandomSeed connect file:/dev/urandom 512
#SSLVerifyClient require
#SSLVerifyDepth  10
#UseCanonicalName On
#</VirtualHost>

Don't forget to modify your /etc/hosts file:

Code:
#
# /etc/hosts
#

# IP             Hostname              Alias

# --- localhost --- #
127.0.0.1        localhost             localhost
127.0.0.1        localhost.localdomain localhost
127.0.1.1        Arch-Linux

# --- Virtual Hosts --- #
127.0.0.1        localhost.codeigniter.tld
127.0.0.1        localhost.drupal.tld
127.0.0.1        localhost.ph7CMS.tld
127.0.0.1        localhost.symfony.tld

# --- Redirections/blocking --- #
#127.0.0.1      http://google.com

# --- IPv6 --- #
::1             ip6-localhost
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters


Hope it helps and I understood your question right.
Reply
#6

This thread may be useful especially how to reset your own application and system paths instead using default paths:

https://forum.codeigniter.com/thread-75640.html
Reply
#7

Before this thread get's out of hand (with providing incorrect answers), what software does your hosting provider use for your shared hosting. Cpanel? Not pointing finger here, but some posts in this thread does not help @shahadathossain at all.

The content of /public should be in your shared hosting providers /public_html or sometimes named /public. Normally this are the directory your FTP-client automatically enters. This are the folder generally refereed to as document root / root directory.

Every folder and files not being inside /public should be placed one directory up. Normally you can press .. (folder named ..) in your FTP-client and in that folder you should place everything else (app, system, tests, writable etc).

CodeIgniter 4 have just been released, therefor there are no tutorial for everything. And shared hosting all differ so we can't give explanations on all hosting providers. With CI4 vs CI3 we have taken security one step further, as in CI3 index.php where alongside your application and system, meaning you could drop all those files directly when connecting with your FTP-client. But that's not the case anymore, as it's not a good practice considering the security issues that can occur.
Reply
#8

(03-25-2020, 12:00 PM)jreklund Wrote: Before this thread get's out of hand (with providing incorrect answers), what software does your hosting provider use for your shared hosting. Cpanel? Not pointing finger here, but some posts in this thread does not help @shahadathossain at all.
 if i have done anything to mislead @shahadathossain or if i have not understood his issue i have no problem in being told in what way. Where i am coming from is from a point of using shared hosting and successfully   having codeigniter 4.0.0-rc3 work on live hosting with https .

Thus my contribution is from a standpoint of getting it working live
Reply
#9

(03-25-2020, 12:00 PM)jreklund Wrote: Before this thread get's out of hand (with providing incorrect answers), what software does your hosting provider use for your shared hosting. Cpanel? Not pointing finger here, but some posts in this thread does not help @shahadathossain at all.

The content of /public should be in your shared hosting providers /public_html or sometimes named /public. Normally this are the directory your FTP-client automatically enters. This are the folder generally refereed to as document root / root directory.

Every folder and files not being inside /public should be placed one directory up. Normally you can press .. (folder named ..) in your FTP-client and in that folder you should place everything else (app, system, tests, writable etc).

CodeIgniter 4 have just been released, therefor there are no tutorial for everything. And shared hosting all differ so we can't give explanations on all hosting providers. With CI4 vs CI3 we have taken security one step further, as in CI3 index.php where alongside your application and system, meaning you could drop all those files directly when connecting with your FTP-client. But that's not the case anymore, as it's not a good practice considering the security issues that can occur.

Yes, my hosting provider is using Cpanel. I understand the security concern here. But the thing is, its actually a bit complex for a beginner to start with Codeigniter4 deployment to the live server where Codeigniter3 was super easy. So, now do I have to put the "public" folder contents to the "public_html" folder and keep other files/folders outside the public_html folder. Is that correct?
Reply
#10

That's correct. If you upload everything as is (renaming /public into /public_html), you will end up with the following structure in Cpanel, based upon /.
/public_html are always your main domains document root, and that can't be changed. Only subdomain and addon domains have that ability (through Cpanel itself).


Code:
|  .gitignore
|  composer.json
|  contributing.md
|  env
|  LICENSE
|  license.txt
|  phpunit.xml.dist
|  README.md
|  spark

+---app             
+---public_html
|      .htaccess
|      favicon.ico
|      index.php
|      robots.txt
|     
+---system
+---tests
+---writable


And that can also cause som clutter, if you want to have another website later on (subdomains / addon domains) with different Document root.
And here you can actually create /subdomain.domain.com/public as the document root. And upload the CI folder into /subdomain.domain.com and it will work out of the box.

My recommendation are always placing codeigniter4 files inside another folder, so that you don't clutter /, the folder name codeigniter4 can be whatever you want. May be the domain name.


Code:
+---codeigniter4
|  |  .gitignore
|  |  composer.json
|  |  contributing.md
|  |  env
|  |  LICENSE
|  |  license.txt
|  |  phpunit.xml.dist
|  |  README.md
|  |  spark
|  | 
|  +---app
|  +---system
|  +---tests           
|  +---writable
|             
\---public_html
        .htaccess
        favicon.ico
        index.php
        robots.txt


But for this trick to work, you need to open your /public_html/index.php and change into the following:
PHP Code:
$pathsPath FCPATH '../codeigniter4/app/Config/Paths.php'
Reply




Theme © iAndrew 2016 - Forum software by © MyBB