{"id":97,"date":"2019-08-31T10:00:00","date_gmt":"2019-08-31T10:00:00","guid":{"rendered":"https:\/\/hostup.org\/blog\/?p=97"},"modified":"2021-01-24T20:51:56","modified_gmt":"2021-01-24T20:51:56","slug":"install-nginx-with-php-and-mariadb-ubuntu","status":"publish","type":"post","link":"https:\/\/hostup.se\/en\/blog\/install-nginx-with-php-and-mariadb-ubuntu\/","title":{"rendered":"Install NGINX with PHP 7.3 and MariaDB on Ubuntu"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Step 1: Install Nginx<\/h2>\n\n\n\n<p>To install the current version of Nginx, simply follow the documentation on its offical website seen <a href=\"https:\/\/nginx.org\/en\/linux_packages.html#Ubuntu\" target=\"_blank\" rel=\"noopener\">here<\/a> or do the following commands:<\/p>\n\n\n\n<p>Firstly we need to install ca-certificates so we can verify that we downloaded the correct file from a trusted source.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install curl gnupg2 ca-certificates lsb-release<\/pre>\n\n\n\n<p>Then we need to add Nginx mainline package to our repositories<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"deb http:\/\/nginx.org\/packages\/mainline\/ubuntu `lsb_release -cs` nginx\" \\     | sudo tee \/etc\/apt\/sources.list.d\/nginx.list<\/pre>\n\n\n\n<p>Now verify that we downloaded the correct file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -fsSL https:\/\/nginx.org\/keys\/nginx_signing.key | sudo apt-key add - <\/pre>\n\n\n\n<p>If it prints out <strong>OK!<\/strong> then you are good to go!<\/p>\n\n\n\n<p>Now that we have downloaded &amp; verified its authnicity lets install it!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt update<\/pre>\n\n\n\n<p>Install Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install nginx<\/pre>\n\n\n\n<p>That\u2019s it! You have now installed the latest release of Nginx. You  should now start it!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start nginx.service<\/pre>\n\n\n\n<p>and make it start on reboot. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable nginx.service<\/pre>\n\n\n\n<p>Visit your servers IP address in your webbrowser. You should now see something along these lines if it is working correctly.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"262\" src=\"https:\/\/hostup.org\/blog\/wp-content\/uploads\/2021\/01\/image-29.png\" alt=\"welcome to nginx, default index.html\" class=\"wp-image-99\" srcset=\"https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2021\/01\/image-29.png 580w, https:\/\/hostup.se\/en\/blog\/wp-content\/uploads\/2021\/01\/image-29-300x136.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><figcaption>Default index.html in NGINX<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Good to know:<\/h3>\n\n\n\n<p>Your files are located by default in NGINX in <strong>\/usr\/share\/nginx\/html<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install MariaDB<\/h2>\n\n\n\n<p>To install the server &amp; client:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install mariadb-server mariadb-client<\/pre>\n\n\n\n<p>Enable it on restart:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable mysql.service<\/pre>\n\n\n\n<p>Start MariaDB:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start mysql.service<\/pre>\n\n\n\n<p>Setup MariaDB:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo mysql_secure_installation<\/pre>\n\n\n\n<p>You will now be prompted with a configuration setup file. Please read  through it and configure it to your liking. It is recommended to disable logins that are not from localhost.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enter current password for root (enter for none): <strong>press Enter<\/strong><\/li><li>Set root password? [Y\/n]:&nbsp;<strong>Y<\/strong><\/li><li>New password:&nbsp;<strong>Enter password<\/strong><\/li><li>Re-enter new password: <strong>Repeat password<\/strong><\/li><li>Remove anonymous users? [Y\/n]: <strong>Y<\/strong><\/li><li>Disallow root login remotely? [Y\/n]: <strong>Y<\/strong><\/li><li>Remove test database and access to it? [Y\/n]: &nbsp;<strong>Y<\/strong><\/li><li>Reload privilege tables now? [Y\/n]: &nbsp;<strong>Y<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Create your Database<\/h2>\n\n\n\n<p>After this you may create or import your database from another source.<\/p>\n\n\n\n<p>Login into MySQL:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo mysql -u root -p<\/pre>\n\n\n\n<p>You can create your database by running the following commands. We will call it <strong>joomla<\/strong> although you are free to call it whatever you want.<\/p>\n\n\n\n<p>Create a new user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE USER 'changeme'@'localhost' IDENTIFIED BY 'password_here';<\/code><\/pre>\n\n\n\n<p>Assign the user to the joomla database<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL ON changeme.* TO 'changeme'@'localhost' IDENTIFIED BY 'password_here' WITH GRANT OPTION;<\/code><\/pre>\n\n\n\n<p>Now you have created a database with its own user and password. However if you want to import a database from another source you need to follow this additional step:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing SQL file<\/h3>\n\n\n\n<p>First lets go to the root directory<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd<\/pre>\n\n\n\n<p>After this you need to upload your database dump to here. I prefer using SFTP for this. <a href=\"https:\/\/filezilla-project.org\/download.php?type=client\" target=\"_blank\" rel=\"noopener\">Filezilla<\/a> is my favorite SFTP software.<\/p>\n\n\n\n<p>If your username is root on the VPS then go to \/root directory in the SFTP. Once there upload your .sql file.<\/p>\n\n\n\n<p>To import a database:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mysql -u username -p database_name &lt; file.sql<\/code> <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Install PHP 7.3<\/h2>\n\n\n\n<p>To add the repositories for PHP:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install software-properties-common<\/pre>\n\n\n\n<p>To add the 2nd repository:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo add-apt-repository ppa:ondrej\/php<\/pre>\n\n\n\n<p>Run this to download your added repositories:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt update<\/pre>\n\n\n\n<p>We can now install PHP now that we have all of the required repositories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install php7.3-fpm php7.3-common php7.3-mysql php7.3-gmp php7.3-curl php7.3-intl php7.3-mbstring php7.3-xmlrpc php7.3-gd php7.3-xml php7.3-cli php7.3-zip<\/pre>\n\n\n\n<p>It is recommended to raise the <strong>memory limit<\/strong> and disable <strong>cgi.fix_pathinfo<\/strong>. Your PHP configuration is located in<code><strong>\/etc\/php\/7.3\/cli\/php.ini<\/strong><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/php\/7.3\/cli\/php.ini<\/pre>\n\n\n\n<p>Press <bawt-key>CTRL<\/bawt-key> +  <bawt-key>W<\/bawt-key> and search for  <strong>;cgi.fix_pathinfo=1<\/strong><\/p>\n\n\n\n<p> Replace it with <strong>cgi.fix_pathinfo=0<\/strong><\/p>\n\n\n\n<p>Save &amp; exit by pressing <bawt-key>CTRL<\/bawt-key> +  <bawt-key>X<\/bawt-key> followed by <bawt-key>Y<\/bawt-key><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Configure Nginx<\/h2>\n\n\n\n<p>Add nginx to www-data group<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo usermod -a -G www-data nginx<\/pre>\n\n\n\n<p>Change owner of directory to www-data<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown -R www-data \/usr\/share\/nginx\/html<\/pre>\n\n\n\n<p>Go into your <strong>default.conf<\/strong> file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/nginx\/conf.d\/default.conf<\/pre>\n\n\n\n<p>Replace your existing configuration file with the one below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen       80;\n    server_name  localhost;\n\n    root   \/usr\/share\/nginx\/html;\n    index  index.php index.html index.htm;\n\n   location \/ {\n    if ($request_uri ~ ^\/(.*)\\.html$) {\n        return 302 \/$1;\n        }\n    }\n\n    error_page   500 502 503 504  \/50x.html;\n    location = \/50x.html {\n        root   \/usr\/share\/nginx\/html;\n    }\n        location ~ \\.php$ {\n        fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n        fastcgi_pass   unix:\/var\/run\/php\/php7.3-fpm.sock;\n        fastcgi_index  index.php;\n        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        include        fastcgi_params;\n    }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running multible websites<\/h3>\n\n\n\n<p>Alternatively if you are running mulitble websites with <strong>different content<\/strong> on them you need to do a little more. If you only have one website, or multible domains pointing to the same website\/content you can skip this step. Otherwise, here is an example configuration of two websites running different content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen       80;\n    server_name  website1.com www.website1.com;\n\n    root   \/usr\/share\/nginx\/html\/website1.com;\n    index  index.php index.html index.htm;\n\n   location \/ {\n    if ($request_uri ~ ^\/(.*)\\.html$) {\n        return 302 \/$1;\n        }\n    }\n\n    error_page   500 502 503 504  \/50x.html;\n    location = \/50x.html {\n        root   \/usr\/share\/nginx\/html;\n    }\n        location ~ \\.php$ {\n        fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n        fastcgi_pass   unix:\/var\/run\/php\/php7.3-fpm.sock;\n        fastcgi_index  index.php;\n        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        include        fastcgi_params;\n    }\n\n}\n\nserver {\n    listen       80;\n    server_name  website2.com www.website2.com;\n\n    root   \/usr\/share\/nginx\/html\/website2.com;\n    index  index.php index.html index.htm;\n\n   location \/ {\n    if ($request_uri ~ ^\/(.*)\\.html$) {\n        return 302 \/$1;\n        }\n    }\n\n    error_page   500 502 503 504  \/50x.html;\n    location = \/50x.html {\n        root   \/usr\/share\/nginx\/html;\n    }\n        location ~ \\.php$ {\n        fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n        fastcgi_pass   unix:\/var\/run\/php\/php7.3-fpm.sock;\n        fastcgi_index  index.php;\n        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        include        fastcgi_params;\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>As you can see, both the <strong>server_name<\/strong> and the root directory are different. Replace &#8220;website1.com, website2.com&#8221; with your respective domain names.<\/p>\n\n\n\n<p>You need to create a directory for them both as shown in the root directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir \/usr\/share\/nginx\/html\/website1.com<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir \/usr\/share\/nginx\/html\/website2.com<\/pre>\n\n\n\n<p>If you want to add https to your website(s) then follow this tutorial:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-hostup-blog wp-block-embed-hostup-blog is-provider-hostup\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/hostup.org\/blog\/how-to-secure-your-nginx-server\/\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Step 1: Install Nginx To install the current version of Nginx, simply follow the documentation on its offical website seen here or do the following commands: Firstly we need to install ca-certificates so we can verify that we downloaded the correct file from a trusted source. sudo apt install curl gnupg2 ca-certificates lsb-release Then we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":146,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,10],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-tutorials"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":1,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions\/100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/media\/146"}],"wp:attachment":[{"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostup.se\/en\/blog\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}