In last post we have successfully installed nginx web server. Today we will learn how to install mysql and php on nginx sever.
To install mysql
To install mysql server on ubuntu 14.x, we will have to run following command
apt-get install mysql-server |
Above command will install mysql server on ubuntu server. While installing it will ask root password for mysql server, we need to put strong password to keep it secure.
After installing mysql-server its always good idea to secure mysql server. To secure mysql server run following command and follow instructions.
sudo mysql_secure_installation |
Now you have successfully install Mysql server on ubuntu, Now lets install php to make it complete running LEMP(Linux Nginx Mysql PHP) server.
Install PHP-FPM
To run php on nginx server we need to install php-fpm on server. To install php5 on server run following command:
sudo apt-get install php5-fpm php5-mysql |
Above command will install php5 fpm and php mysql extension (which helps to connect mysql with php). Now we need to configure nginx server to execute php files using php-fpm
Configure nginx to execute php files using php fpm
To configure nginx to execute php files on server we need to edit nginx server configuration file. Default nginx server configuration file on ubuntu sever is
/etc/nginx/sites-available/default |
we need to edit this file, to allow nginx to execute php files. we need to add following code to above file under server brackets
1 2 3 4 5 6 7 8 | location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } |
After this, we also need to allow nginx to excute index.php file default using directory index. To enable this, we need to add the index.php file in directory index like below
index index.php index.html index.htm; |
After this configuration, your “default” configuration file will look like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | server { listen 80; # allow server to recieve all port 80 request root /usr/share/nginx/html; #setup root of nginx server, You can change to anything index index.php index.html index.htm; #setup directory index. It allow to search index.php and index.html and index.htm file to search as default file. server_name mydomain.com www.mydomain.com; #setup alias as domain name location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; # pass the PHP scripts to FastCGI server listening on the php-fpm socket location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } |
Now as we have configured nginx to excute php file. we need to restart nginx server to enable these changes. to restart nginx server run below command
/etc/inti.d/nginx restart |
or
service nginx restart |
To test, if our php files are being execute or not, We’ll create a “index.php” in /usr/share/nginx/html and will add following lines
1 | <!--?php phpinfo(); ?--> |
Now we can test it by visiting following url in your browser:
http://{mydomain.com}/ |
we will see output like below
Great, Now we have successfully created our LEMP Server. Next we’ll learn how to setup mail server on ubuntu.