Tuesday, 10 April 2018

How to install LEMP on centos 6 ( Nginx, PHP, Mysql )

How to install LEMP on centos 6 ( Nginx, PHP, Mysql )

LEMP stack

LEMP stack is nothing but a group of open source softwares combined together to make a webserver . LEMP refers to the first letters of Linux , Nginx ( Engine x ) , Mysql and PHP .
About Nginx, It is a lightweight webserver which is more stable and secure, it serves static content 50 times faster than Apache.

Install LEMP on centos 6.5

Let’s start the installation .

Nginx installation

Step 1 » Install Nginx repository package . you could find the latest package here ( Nginx linux packages )
[root@krizna ~]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm[or]
Create a file /etc/yum.repos.d/nginx.repo and add the following lines to the file.
Step 2 » Update the repository and install nginx.
[root@krizna ~]# yum check-update
[root@krizna ~]# yum install nginx

Step 3 » After the installation . Let’s start the service .
[root@krizna ~]# service nginx startIssue the below command to start service automatically while reboot.
[root@krizna ~]# chkconfig nginx on
These are the default directories and files
Default document root directory: /usr/share/nginx/html
Default configuration file: /etc/nginx/nginx.conf
Default Virtual host config directory: /etc/nginx/conf.d/
Default Virtual host config file: /etc/nginx/conf.d/default.conf
Step 4 » Now the nginx service has started . By default, Nginx will listen on port 80 which has to be excluded in iptables .
Check the listening 80 port using netstat command
[root@krizna ~]# netstat -ntl | grep :80
Install lemp on centos 6
Now open /etc/sysconfig/iptables file and add the following line.
-A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPTWhich should be before reject line looks below
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
and restart iptables service
[root@krizna ~]# service iptables restart
Step 5 » Now open your browser and goto http://serverip ( http://192.168.1.15). you could see the default nginx page like below.
Install lemp on centos 6
If you are not getting the page . try to stop iptables using the below commands and try again .
[root@krizna ~]# service iptables stopTo stop permanently .[root@krizna ~]# chkconfig iptables offDisabling iptables is not recommended ( Check this Centos iptables )

Mysql installation

Step 6 » Install mysql server and client using the below command.
[root@krizna ~]# yum install mysql mysql-server
Step 7 » Start mysql service and enable automatic start during bootup.
[root@krizna ~]# service mysqld start
[root@krizna ~]# chkconfig mysqld on

Step 8 » Use below command to secure mysql installation and for creating root password. This will prompt to enter current password, just press enter and create a new password and proceed the installation by giving yes to all.
[root@krizna ~]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

Step 9 » Test your mysql connection using the created password.
[root@krizna ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 11
Server version: 5.1.71 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>

PHP installation

PHP-FPM is an alternative PHP FastCGI implementation .
Step 9 » Install php-fpm and php-mysql.
[root@krizna ~]# yum install php-fpm php-mysql
Step 10 » Open /etc/php-fpm.d/www.conf which is config file for php-fpm and change user and group from apache to nginx.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Step 11 » Start php-fpm service and enable auto start during startup.
[root@krizna ~]#service php-fpm start
[root@krizna ~]#chkconfig php-fpm on

Step 12 » Create a virtual host /etc/nginx/conf.d/phptest.conf file and add the below code .
Step 13 » Create info.php in the path /usr/share/nginx/html as mentioned in the Vhost config file and add the below lines .
Step 14 » Allow port 8080 in the iptables ( Step 4 ) and open your browser and goto http://serverip:8080/info.php ( http://192.168.1.15:8080/info.php ) . you could the get PHP info page like below.
Install lemp on centos 6
Step 15 » Now we can check database connectivity from PHP . create another file dbtest.php in the same location and add the below code to the file and replace your mysql password in the code.
Step 16 » Now access http://serverip:8080/dbtest.php .if everything is perfect you could get “Congrats!” message as stated in the code.
Install lemp on centos 6

With SSL

Step 17 » This is the example code with SSL enabled .
All the best.

How to Install a LAMP (Apache, MySQL, PHP) stack on CentOS 6.7

How to Install a LAMP (Apache, MySQL, PHP) stack on CentOS 6.7

In this How-To, we will walk you through the installation of a LAMP stack on a CentOS 6.7 based server. Although we are writing this article in the context of CentOS 6.7, a Linux, Apache, Mysql, PHP(LAMP) server is a common installation stack capable of being hosted on many different Operating Systems. Examples of such are Debian and Debian based distributions like Ubuntu, or RHEL and RHEL based distributions such as Fedora or Scientific Linux. You’ll see these installations occurring on a variety of hosting platforms such as shared web hosting, VPS hosting, dedicated hosting and cloud hosting.
In the case of this article, we’ll be utilizing the YUM package manager associated with the RHEL distribution CentOS.

Prerequisites

A server with CentOS 6.7  installed will take care of the Linux aspect of the LAMP stack install.  

Installing Apache on CentOS 6.7

Install Apache with the following command to begin the install:
sudo yum install httpd
Start Apache with the following command:
sudo service httpd start
We can now verify Apache is working by opening your browser and entering the URL http://your-server's-address. you should get a blue Apache 2 test page similar to the image below.
Note: If you do not know your IP address, run the following command:
ip addr show eth0
An example of running the command: ip addr show eth0 and getting 192.168.100.10 for the IP address.
An example of running the command: ip addr show eth0 and getting 192.168.100.10 for the IP address.
In our example we would put http://192.168.100.10 into our browser’s address bar.

Apache 2 Test page
Apache 2 Test page

Installing MySQL on CentOS 6.7

Install MySQL with the following command to begin the install:
sudo yum install mysql-server
Start the service with the following command
sudo service mysqld start
Set root MySQL password with the following command:
/usr/bin/mysql_secure_installation
Note: You will be prompted with a series of questions. Simply type Y for yes on all of them, see the screen shot below:
Sample mysql_secure_installation
Sample mysql_secure_installation

Installing PHP on CentOS 6.7

Install PHP with the following command to begin the install:
sudo yum install php php-mysql
Create a test PHP file in the following directory with the following command:
sudo vi /var/www/html/info.php
Insert the following code in the empty space then save and exit:
<?php
phpinfo();
?>
Restart apache so all the changes take effect:
sudo service httpd restart
Test your page in your browser with the following hyperlink changed with your IP address:
http://YOUR.IP.ADD.RESS/info.php
Sample info.php
Sample info.php
It is a good idea to remove your php.info file as it can be used to aid an attacker to compromise your server. You can do that with the following command:
sudo rm /var/www/html/info.php
If you would like Apache and MySQL to start on boot, run the following commands:
sudo chkconfig httpd on
sudo chkconfig mysqld on
Congratulations! You have just installed a LAMP stack on your CentOS 6.7 Server. Thank you for following along in this How-To and check back with us for any new updates.

How to Install Tomcat 7.0.82 Server on CentOS/RHEL 7/6/5

Install Apache Tomcat 7.0 on RHEL 6 / CentOS 6

Here is the step by step guide to install Apache Tomcat 7.0 on RHEL 6 / CentOS 6.
Open Terminal.
Switch to root user.

Install OpenJDK:

Tomcat requires to have java installed on your machine, you can either have Oracle JDK or OpenJDK installed on your machine. Here i used the openjdk, lets install openjdk by issuing the following command.
[root@server ~]# yum install java-1.7.0
You can also verify it, by issuing the following command.
[root@server ~]#  java -version
java version “1.7.0_51”
OpenJDK Runtime Environment (rhel-2.4.4.1.el6_5-x86_64 u51-b02)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

Download & Setup Apache Tomcat:

Download the latest version of the Apache Tomcat from the website and save it on your home directory.
[root@server ~]# wget http://mirror.nexcess.net/apache/tomcat/tomcat-7/v7.0.52/bin/apache-tomcat-7.0.52.tar.gz
Download the MD5 file.
[root@server ~]# wget --no-check-certificate https://www.apache.org/dist/tomcat/tomcat-7/v7.0.52/bin/apache-tomcat-7.0.52.tar.gz.md5
Verify the md5 check sum of the downloaded Tomcat archive with check sum provided by Apache Tomcat.
[root@server ~]# cat apache-tomcat-7.0.52.tar.gz.md5
3d780939cc4fce1661e7411bbd640c09 *apache-tomcat-7.0.52.tar.gz
 
[root@server ~]# md5sum apache-tomcat-7.0.52.tar.gz
3d780939cc4fce1661e7411bbd640c09  apache-tomcat-7.0.52.tar.gz
If both matches, extract the the tomcat on to your desired (/var/local) directory.
 tar -zxvf apache-tomcat-7.0.26.tar.gz -C /var/local/

Controlling Apache Tomcat:

Apache Tomcat can be  started and stopped by the script which comes with package, start the Apache Tomcat.
[root@server ~]# /var/local/apache-tomcat-7.0.26/bin/startup.sh
you will get the following out.
[root@server ~]# /var/local/apache-tomcat-7.0.52/bin/startup.sh
Using CATALINA_BASE:   /var/local/apache-tomcat-7.0.52
Using CATALINA_HOME:   /var/local/apache-tomcat-7.0.52
Using CATALINA_TMPDIR: /var/local/apache-tomcat-7.0.52/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /var/local/apache-tomcat-7.0.52/bin/bootstrap.jar:/var/local/apache-tomcat-7.0.52/bin/tomcat-juli.jar
Tomcat started.
You can verify the service running, by default tomcat runs on port no 8080
[root@server ~]# netstat -antup | grep 8080
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      2476/java
Open the web browser and point to http://ipaddress:8080
Tomcat Home
Stop the Apache Tomcat.
[root@server ~]# /var/local/apache-tomcat-7.0.26/bin/shutdown.sh

Managing the Apache Tomcat:

Tomcat can be managed through the web-manager, the following can be done through that.
Deploy new application, deploy new application on specified context, list the active or in active applications, start and stop the web applications.
Web manager is password protected, requires user name and password to access. Only the user with the “manager-gui” role is allowed to access, these users and roles are defined in tomcat-users.xml. By default “manager-gui” role not defined that file, we need to add it manually.
vi /var/local/apache-tomcat-7.0.52/conf/tomcat-users.xml
Place the following two lines just above the last line.
<role rolename=”manager-gui”/>
<user username=”tomcat” password=”tomcat” roles=”manager-gui”/>
Stop and Start the tomcat, now tomcat user have access to the web manager.
Tomcat  Manager
same like, you can define the other roles.
That’s All.

Installing JBoss 7.1.1.Final on CentOS 6.x

Installing JBoss 7.1.1.Final on CentOS 6.x

INTRODUCTION
In this tutorial, we will demonstrate how to download, install, start and stop a JBoss 7.1.1.Final server on CentOS 6.x. We use OpenJDK 6 for this tutorial. This is the same installation procedure we use to create our Open Source Architect JBoss 7.1.1.Final CentOS ami.

This tutorial consists of the following steps:

  • Step 1: JDK installation and verification
  • Step 2: Download JBoss and the installation procedure
  • Step 3: Create the appropriate user
  • Step 4: Start our new JBoss server and verify that the server has started properly
  • Step 5: Stop the new JBoss server and verify that the server has shutdown properly
STEP 1: JDK INSTALLATION AND VERIFICATION
The first step before installing JBoss AS 7, is to install a JDK. Any JDK can be used, such as Sun JDK, OpenJDK, IBM JDK, or JRocket etc. We chose Open JDK 6 for this tutorial, because it is the new Java reference implementation starting with Java 7.
NOTE: JDK 7 and above can also be used with JBoss. A JRE is also sufficient to run JBoss 7, however a JRE does not include some of the additional feature of a JDK.
Installing OpenJDK:
Issue the following command to install the JDK:
$ su -c "yum install java-1.6.0-openjdk-devel"
CONFIRMING THE INSTALL:
Issue the following command to confirm that the proper version of the JDK is on your classpath:
$ java -version
NOTE: For our installation, we are not defining a explicit JAVA_HOME for JBoss AS 7. The default works in this situation, because we don’t have multiple java versions installed. For most production environments with multiple versions of Java, it is recommended to set the JAVA_HOME in the standalone.conf or domain.conf files.
STEP 2: DOWNLOAD JBOSS AND THE INSTALLATION PROCEDURE
The next step is to download the appropriate version of JBoss AS 7. We will download the .zip version of JBoss AS 7, and install it using the unzip utility.
DOWNLOADING JBOSS AS 7.1.1.FINAL:
Issue the following wget to download jboss-as-7.1.1.Final.zip:
wget http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.zip
NOTE: jboss-as-7.1.1.Final.zip can also be downloaded with your favorite browser from the http://www.jboss.org/jbossas/downloads/ page.
INSTALLING JBOSS AS 7.1.1.FINAL:
Next, we issue the following unzip command to finally install jboss-as-7.1.1.Final in the /usr/share directory:
$ unzip jboss-as-7.1.1.Final.zip -d /usr/share
Alternatively, any directory can be chosen for the JBoss 7 installation.
STEP 3: CREATE THE APPROPRIATE USER
Now that JBoss AS 7, is installed, we need to make sure that we create a user with the appropriate privileges. It is never a good idea to run JBoss as root for various reasons.
CREATE THE NEW USER:
We create a new user called jboss by issuing the following command:
$ adduser jboss
Alternatively, any username can be used.
CHANGE OWNERSHIP OF THE INSTALLATION DIRECTORY:
We need to assign the appropriate ownership to the installation directory for the newly created jboss user by issuing the command:
$ chown -fR jboss.jboss /usr/share/jboss-as-7.1.1.Final/
SWITCH USER TO THE JBOSS USER:
We switch to the jBoss user, so that this new installation can be administered properly. It is not recommended to administer JBoss as root.
$ su jboss
CHANGE DIRECTORY TO THE JBOSS BIN DIRECTORY:
Now, lets change directories to the JBoss bin directory. This dorectory contains the necessary scripts to start, stop and manage your JBoss installation.
$ cd /usr/share/jboss-as-7.1.1.Final/bin/
ADD A JBOSS MANAGEMENT USER:
The final step before we start JBoss, is to add a management user. This is an internal JBoss management user, necessary to access the new JBoss management console.
$ ./add-user.sh
You should see the following message on the console after executing the command:
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
(a): a
We select “a”, next you should see the following message:
Enter the details of the new user to add.
Realm (ManagementRealm) :
Username : jboss
Password :
Re-enter Password :
* hit enter for Realm to use default, then provide a username and password
We select the default value for the Realm (ManagementRealm), by hitting enter, and select “jboss” as our username. By default, we supply “jb0ss” as our password, of course, you can provide any password you prefer here.
STEP 4: START THE JBOSS AS 7 SERVER:
Once the appropriate JBoss users are created, we are now ready to start our new JBoss AS 7 server. With JBoss AS 7, a new standalone and domain model has been introduced. In this tutorial, we focus on starting up a standalone server. The domain server will be part of a future tutorial.
STARTUP A JBOSS 7, STANDALONE INSTANCE:
A standalone instance of JBoss 7 can be starting by executing:
$ ./standalone.sh -Djboss.bind.address=0.0.0.0 -Djboss.bind.address.management=0.0.0.0&
NOTE: By default, JBoss 7 will only bind to localhost. This does not allow any remote access to your jboss server. For our amazon aws installation, we define the jboss.bind.address property as 0.0.0.0 and jboss.bin.address.management property to 0.0.0.0 as well. This allows us to access the remote JBoss amazon instance over the internet. We could have also defined the hostname of the ami or the ip address. However, unless an elastic ip is used, this value can change. This is why we opted for 0.0.0.0.
You should see the following messages on the console after executing the command for a successful startup:
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /usr/share/jboss-as-7.1.1.Final
JAVA: java
JAVA_OPTS: -server -XX:+UseCompressedOops -XX:+TieredCompilation -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Djboss.server.default.config=standalone.xml
=========================================================================
17:51:31,102 INFO [org.jboss.modules] JBoss Modules version 1.1.1.GA
17:51:31,501 INFO [org.jboss.msc] JBoss MSC version 1.0.2.GA
17:51:31,602 INFO [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final “Brontes” starting
17:51:33,705 INFO [org.xnio] XNIO Version 3.0.3.GA
17:51:33,721 INFO [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
17:51:33,755 INFO [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
17:51:33,795 INFO [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
17:51:33,891 INFO [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
17:51:33,914 INFO [org.jboss.as.configadmin] (ServerService Thread Pool — 26) JBAS016200: Activating ConfigAdmin Subsystem
17:51:33,949 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool — 31) JBAS010280: Activating Infinispan subsystem.
17:51:34,077 INFO [org.jboss.as.webservices] (ServerService Thread Pool — 48) JBAS015537: Activating WebServices Extension
17:51:34,081 INFO [org.jboss.as.security] (ServerService Thread Pool — 44) JBAS013101: Activating Security Subsystem
17:51:34,105 INFO [org.jboss.as.security] (MSC service thread 1-1) JBAS013100: Current PicketBox version=4.0.7.Final
17:51:34,121 INFO [org.jboss.as.osgi] (ServerService Thread Pool — 39) JBAS011940: Activating OSGi Subsystem
17:51:34,128 INFO [org.jboss.as.naming] (ServerService Thread Pool — 38) JBAS011800: Activating Naming Subsystem
17:51:34,501 INFO [org.jboss.as.connector] (MSC service thread 1-1) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
17:51:34,583 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool — 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
17:51:34,944 INFO [org.jboss.as.naming] (MSC service thread 1-2) JBAS011802: Starting Naming Service
17:51:35,043 INFO [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-1) JBoss Web Services – Stack CXF Server 4.0.2.GA
17:51:35,844 INFO [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-1) Starting Coyote HTTP/1.1 on http–0.0.0.0-8080
17:51:35,886 INFO [org.jboss.as.mail.extension] (MSC service thread 1-2) JBAS015400: Bound mail session [java:jboss/mail/Default]
17:51:36,543 INFO [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on /0.0.0.0:9999
17:51:36,637 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-1) JBAS015012: Started FileSystemDeploymentService for directory /usr/share/jboss-as-7.1.1.Final/standalone/deployments
17:51:36,791 INFO [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on /0.0.0.0:4447
17:51:36,811 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
17:51:36,895 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://0.0.0.0:9990
17:51:36,896 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final “Brontes” started in 6182ms – Started 133 of 208 services (74 services are passive or on-demand)
TEST YOUR JBOSS 7 INSTALLATION:
A good indication of a successful startup is that you can login to the JBoss admin console. Type in the following in your browser window, where yourip should be replaced with the address of your amazon ami:
http://yourip::9990/
This should provide you access to the new admin console, which will be the topic of a future tutorial.
STEP 5: STOP THE JBOSS AS 7 SERVER:
After successfully starting up JBoss 7, lets demonstrate how to shut your JBoss server down in this section.
SHUTDOWN A JBOSS 7 INSTANCE:
To shutdown your JBoss 7 server, execute the following command:
$ ./jboss-cli.sh --connect command=:shutdown
Just like most of our tutorials, if you want to test the final result of this tutorial, |opensourcearchitect| has provided a complete JBoss 7 installation as an ami here:
us-east-1 ebs 64bit ami-5de44e34
This ami was created following this tutorial, and serves as the end result for your reference or use.

Install and Configure MySQL on CentOS 6.7

yum install mysql mysql-server
service mysqld start /usr/bin/mysql_secure_installation
The MySQL Secure Installation process will walk you through initial security changes that should be put into place for your new MySQL instance.
Note: Chose a secure password and limit root access to localhost only.
Sample mysql_secure_installation
mysql_secure_installation
Edit the /etc/my.cf file to match the following, limiting the server only to listen to local connections:
[mysqld]
bind-address = 127.0.0.1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
We’re going to restart all services at the end, so let’s move on.